From 7cc4841eca82e5ae21d15390bcf45e40d46a6323 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Tue, 29 Aug 2017 07:52:22 +0200 Subject: [PATCH] Fix disconnect callback for sync library --- code/espurna/mqtt.ino | 65 +++++++++++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 21 deletions(-) diff --git a/code/espurna/mqtt.ino b/code/espurna/mqtt.ino index 153b4afe..d5590487 100644 --- a/code/espurna/mqtt.ino +++ b/code/espurna/mqtt.ino @@ -63,7 +63,10 @@ bool mqttConnected() { } void mqttDisconnect() { - if (mqtt.connected()) mqtt.disconnect(); + if (mqtt.connected()) { + DEBUG_MSG_P("[MQTT] Disconnecting\n"); + mqtt.disconnect(); + } } bool mqttForward() { @@ -483,7 +486,11 @@ boolean mqttDiscover() { #endif // MDNS_SUPPORT void mqttSetup() { + #if MQTT_USE_ASYNC + + DEBUG_MSG_P(PSTR("[MQTT] Using ASYNC MQTT library\n")); + mqtt.onConnect([](bool sessionPresent) { _mqttOnConnect(); }); @@ -519,11 +526,16 @@ void mqttSetup() { mqtt.onPublish([](uint16_t packetId) { DEBUG_MSG_P(PSTR("[MQTT] Publish ACK for PID %d\n"), packetId); }); - #else + + #else // not MQTT_USE_ASYNC + + DEBUG_MSG_P(PSTR("[MQTT] Using SYNC MQTT library\n")); + mqtt.setCallback([](char* topic, byte* payload, unsigned int length) { _mqttOnMessage(topic, (char *) payload, length); }); - #endif + + #endif // MQTT_USE_ASYNC mqttConfigure(); mqttRegister(_mqttCallback); @@ -532,32 +544,43 @@ void mqttSetup() { void mqttLoop() { - if (!_mqttEnabled) return; + #if MQTT_USE_ASYNC - if (WiFi.status() == WL_CONNECTED) { + if (!_mqttEnabled) return; + if (WiFi.status() != WL_CONNECTED) return; + if (mqtt.connected) return; - if (!mqtt.connected()) { + static unsigned long last = 0; + if (millis() - last > MQTT_RECONNECT_DELAY) { + last = millis(); + mqttConnect(); + } - #if not MQTT_USE_ASYNC - if (_mqttConnected) { - _mqttOnDisconnect(); - _mqttConnected = false; - } - #endif + #else // not MQTT_USE_ASYNC - static unsigned long last = 0; - if (millis() - last > MQTT_RECONNECT_DELAY) { - last = millis(); - mqttConnect(); - } + if (WiFi.status() != WL_CONNECTED) return; + + if (mqtt.connected()) { - #if not MQTT_USE_ASYNC - } else { mqtt.loop(); - #endif + + } else { + + if (_mqttConnected) { + _mqttOnDisconnect(); + _mqttConnected = false; + } + + if (_mqttEnabled) { + static unsigned long last = 0; + if (millis() - last > MQTT_RECONNECT_DELAY) { + last = millis(); + mqttConnect(); + } + } } - } + #endif }