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