|
|
@ -4,10 +4,12 @@ |
|
|
|
|
|
|
|
#include "hardware.h" |
|
|
|
#include "shelf.h" |
|
|
|
#include "wifi.h" |
|
|
|
|
|
|
|
using namespace std; |
|
|
|
|
|
|
|
extern Shelf* shelf; |
|
|
|
extern Wifi wifi; |
|
|
|
|
|
|
|
MyMQTT::MyMQTT(void) |
|
|
|
: host {mqtt_server}, |
|
|
@ -33,6 +35,26 @@ void MyMQTT::onMqttConnectWrapper(bool sessionPresent) { |
|
|
|
mqtt_ptr->onMqttConnect(sessionPresent); |
|
|
|
} |
|
|
|
|
|
|
|
void MyMQTT::onMqttDisconnectWrapper(AsyncMqttClientDisconnectReason reason) { |
|
|
|
// static_cast<MyMQTT*>(parm)->onMqttConnect(NULL);
|
|
|
|
mqtt_ptr->onMqttDisconnect(reason); |
|
|
|
} |
|
|
|
|
|
|
|
void MyMQTT::reconnectTask(void* parameters) { |
|
|
|
while (!wifi.connected()) { |
|
|
|
Serial.print("."); |
|
|
|
vTaskDelay(1000 / portTICK_PERIOD_MS); |
|
|
|
} |
|
|
|
|
|
|
|
Serial.println("MQTT reconnecting!"); |
|
|
|
this->connect(); |
|
|
|
vTaskDelete(NULL); |
|
|
|
} |
|
|
|
|
|
|
|
void MyMQTT::cTaskWrapper(void* parameters) { |
|
|
|
static_cast<MyMQTT*>(parameters)->reconnectTask(NULL); |
|
|
|
} |
|
|
|
|
|
|
|
void MyMQTT::onMqttMessage(char* topic, char* payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) { |
|
|
|
Serial.print("incoming: "); |
|
|
|
Serial.println(topic); |
|
|
@ -59,16 +81,30 @@ void MyMQTT::onMqttConnect(bool sessionPresent) { |
|
|
|
subscribe(); |
|
|
|
} |
|
|
|
|
|
|
|
void MyMQTT::onMqttDisconnect(AsyncMqttClientDisconnectReason reason) { |
|
|
|
Serial.println("Disconnected from MQTT."); |
|
|
|
|
|
|
|
xTaskCreate( |
|
|
|
&cTaskWrapper, /* Task function. */ |
|
|
|
"mqttReconnectTask", /* String with name of task. */ |
|
|
|
1024, /* Stack size in words. */ |
|
|
|
this, /* Parameter passed as input of the task */ |
|
|
|
1, /* Priority of the task. */ |
|
|
|
&mqttTaskHandle); /* Task handle. */ |
|
|
|
} |
|
|
|
|
|
|
|
void MyMQTT::connect(void) { |
|
|
|
mqtt_ptr = this; |
|
|
|
Serial.print("MQTT connecting"); |
|
|
|
|
|
|
|
mqttClient.onConnect(&onMqttConnectWrapper); |
|
|
|
mqttClient.onDisconnect(&onMqttDisconnectWrapper); |
|
|
|
mqttClient.onSubscribe(&onMqttSubscribe); |
|
|
|
mqttClient.onMessage(&onMqttMessageWrapper); |
|
|
|
|
|
|
|
mqttClient.setServer(host, port); |
|
|
|
mqttClient.setCredentials(username, password); |
|
|
|
mqttClient.setKeepAlive(60); |
|
|
|
//#if ASYNC_TCP_SSL_ENABLED
|
|
|
|
// mqttClient.setSecure(true);
|
|
|
|
//#endif
|
|
|
@ -84,6 +120,10 @@ void MyMQTT::connect(void) { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
bool MyMQTT::connected(void) { |
|
|
|
return mqttClient.connected(); |
|
|
|
} |
|
|
|
|
|
|
|
void MyMQTT::subscribe(void) { |
|
|
|
uint16_t dispenseIdSub = mqttClient.subscribe("sensor/vendingmachine/dispense", 0); |
|
|
|
mqttClient.publish("sensor/vendingmachine/alive", 0, true, "test"); |
|
|
|