Hendrik Langer
7 years ago
5 changed files with 61 additions and 39 deletions
@ -1,55 +1,78 @@ |
|||||
#include "MyMQTT.h" |
#include "MyMQTT.h" |
||||
|
|
||||
#include <MQTTClient.h> |
#include <AsyncMqttClient.h> |
||||
#include "hardware.h" |
#include "hardware.h" |
||||
|
|
||||
MyMQTT::MyMQTT(void) |
MyMQTT::MyMQTT(void) |
||||
: host {mqtt_server}, |
: host {mqtt_server}, |
||||
port {mqtt_port}, |
port {mqtt_port}, |
||||
endpoint {mqtt_endpoint}, |
|
||||
username {mqtt_username}, |
username {mqtt_username}, |
||||
password {mqtt_password} |
password {mqtt_password} |
||||
{} |
{} |
||||
|
|
||||
static void onMqttMessage(String &topic, String &payload) { |
extern "C" { |
||||
Serial.println("incoming: " + topic + " - " + payload); |
static void onMqttMessageWrapper(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) { |
||||
|
// static_cast<MyMQTT*>(parm)->onMqttMessage(NULL);
|
||||
|
Serial.println("test"); |
||||
|
mqtt_ptr->onMqttMessage(topic, payload, properties, len, index, total); |
||||
|
} |
||||
|
|
||||
|
static void onMqttSubscribe(uint16_t packetId, uint8_t qos) { |
||||
|
Serial.println("subscribe ack"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void MyMQTT::onMqttConnectWrapper(bool sessionPresent) { |
||||
|
// static_cast<MyMQTT*>(parm)->onMqttConnect(NULL);
|
||||
|
mqtt_ptr->onMqttConnect(sessionPresent); |
||||
|
} |
||||
|
|
||||
|
void MyMQTT::onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) { |
||||
|
Serial.print("incoming: "); |
||||
|
Serial.println(topic); |
||||
|
Serial.println(payload); |
||||
|
} |
||||
|
|
||||
|
void MyMQTT::onMqttConnect(bool sessionPresent) { |
||||
|
Serial.println("CONNECTED! callback works"); |
||||
|
subscribe(); |
||||
} |
} |
||||
|
|
||||
void MyMQTT::connect(void) { |
void MyMQTT::connect(void) { |
||||
|
mqtt_ptr = this; |
||||
Serial.print("MQTT connecting"); |
Serial.print("MQTT connecting"); |
||||
|
|
||||
mqttClient.begin(host, port, net); |
mqttClient.onConnect(&onMqttConnectWrapper); |
||||
mqttClient.onMessage(&onMqttMessage); |
mqttClient.onSubscribe(&onMqttSubscribe); |
||||
while (!mqttClient.connect(host, username, password)) { |
mqttClient.onMessage(&onMqttMessageWrapper); |
||||
|
|
||||
|
mqttClient.setServer(host, port); |
||||
|
mqttClient.setCredentials(username, password); |
||||
|
//#if ASYNC_TCP_SSL_ENABLED
|
||||
|
// mqttClient.setSecure(true);
|
||||
|
//#endif
|
||||
|
|
||||
|
mqttClient.connect(); |
||||
|
|
||||
|
while(!mqttClient.connected()) { |
||||
Serial.print("."); |
Serial.print("."); |
||||
delay(500); |
delay(500); |
||||
} |
} |
||||
Serial.println("MQTT connected"); |
|
||||
subscribe(); |
|
||||
} |
|
||||
|
|
||||
void MyMQTT::loop(void) { |
Serial.println("MQTT connected"); |
||||
auto available = (size_t)net.available(); |
|
||||
if (available > 0) { |
|
||||
Serial.println("data available\n"); |
|
||||
} |
|
||||
mqttClient.loop(); |
|
||||
|
|
||||
if(!mqttClient.connected()) { |
|
||||
Serial.println("MQTT connection lost."); |
|
||||
connect(); |
|
||||
} |
|
||||
} |
} |
||||
|
|
||||
void MyMQTT::subscribe(void) { |
void MyMQTT::subscribe(void) { |
||||
mqttClient.subscribe("sensor/vendingmachine/dispense"); |
uint16_t dispenseIdSub = mqttClient.subscribe("sensor/vendingmachine/dispense", 0); |
||||
// mqttClient.subscribe("sensor/vendingmachine/light");
|
uint16_t dispenseIdSub2 = mqttClient.subscribe("sensor/esp100/#", 0); |
||||
|
mqttClient.publish("sensor/vendingmachine/alive", 0, true, "test"); |
||||
} |
} |
||||
|
|
||||
void MyMQTT::publish(char* topic, char* payload) { |
void MyMQTT::publish(char* topic, char* payload) { |
||||
mqttClient.publish(topic, payload); |
|
||||
} |
} |
||||
|
|
||||
void MyMQTT::publish(char* topic, String payload) { |
void MyMQTT::publish(char* topic, String payload) { |
||||
mqttClient.publish(topic, payload); |
|
||||
} |
} |
||||
|
Loading…
Reference in new issue