Browse Source

Fix disconnect callback for sync library

fastled
Xose Pérez 7 years ago
parent
commit
7cc4841eca
1 changed files with 44 additions and 21 deletions
  1. +44
    -21
      code/espurna/mqtt.ino

+ 44
- 21
code/espurna/mqtt.ino View File

@ -63,7 +63,10 @@ bool mqttConnected() {
} }
void mqttDisconnect() { void mqttDisconnect() {
if (mqtt.connected()) mqtt.disconnect();
if (mqtt.connected()) {
DEBUG_MSG_P("[MQTT] Disconnecting\n");
mqtt.disconnect();
}
} }
bool mqttForward() { bool mqttForward() {
@ -483,7 +486,11 @@ boolean mqttDiscover() {
#endif // MDNS_SUPPORT #endif // MDNS_SUPPORT
void mqttSetup() { void mqttSetup() {
#if MQTT_USE_ASYNC #if MQTT_USE_ASYNC
DEBUG_MSG_P(PSTR("[MQTT] Using ASYNC MQTT library\n"));
mqtt.onConnect([](bool sessionPresent) { mqtt.onConnect([](bool sessionPresent) {
_mqttOnConnect(); _mqttOnConnect();
}); });
@ -519,11 +526,16 @@ void mqttSetup() {
mqtt.onPublish([](uint16_t packetId) { mqtt.onPublish([](uint16_t packetId) {
DEBUG_MSG_P(PSTR("[MQTT] Publish ACK for PID %d\n"), 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) { mqtt.setCallback([](char* topic, byte* payload, unsigned int length) {
_mqttOnMessage(topic, (char *) payload, length); _mqttOnMessage(topic, (char *) payload, length);
}); });
#endif
#endif // MQTT_USE_ASYNC
mqttConfigure(); mqttConfigure();
mqttRegister(_mqttCallback); mqttRegister(_mqttCallback);
@ -532,32 +544,43 @@ void mqttSetup() {
void mqttLoop() { 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(); 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
} }

Loading…
Cancel
Save