diff --git a/code/espurna/mqtt.ino b/code/espurna/mqtt.ino index dd07cf88..fb19854e 100644 --- a/code/espurna/mqtt.ino +++ b/code/espurna/mqtt.ino @@ -94,7 +94,7 @@ void _mqttOnConnect(bool sessionPresent) { void _mqttOnDisconnect(AsyncMqttClientDisconnectReason reason) { - DEBUG_MSG("[MQTT] Disconnected!\n"); + DEBUG_MSG("[MQTT] Disconnected\n"); // Send disconnect event to subscribers for (unsigned char i = 0; i < _mqtt_callbacks.size(); i++) { diff --git a/code/espurna/web.ino b/code/espurna/web.ino index c24dd109..4cf37b23 100644 --- a/code/espurna/web.ino +++ b/code/espurna/web.ino @@ -14,6 +14,7 @@ Copyright (C) 2016-2017 by Xose PĂ©rez #include #include #include +#include AsyncWebServer server(80); AsyncWebSocket ws("/ws"); @@ -24,6 +25,7 @@ typedef struct { } ws_ticket_t; ws_ticket_t _ticket[WS_BUFFER_SIZE]; +Ticker deferred; // ----------------------------------------------------------------------------- // WEBSOCKETS @@ -75,7 +77,12 @@ void _wsParse(uint32_t client_id, uint8_t * payload, size_t length) { DEBUG_MSG("[WEBSOCKET] Requested action: %s\n", action.c_str()); if (action.equals("reset")) ESP.reset(); - if (action.equals("reconnect")) wifiDisconnect(); + if (action.equals("reconnect")) { + + // Let the HTTP request return and disconnect after 100ms + deferred.once_ms(100, wifiDisconnect); + + } if (action.equals("on")) relayStatus(relayID, true); if (action.equals("off")) relayStatus(relayID, false); diff --git a/code/espurna/wifi.ino b/code/espurna/wifi.ino index e7b78193..0db7a93b 100644 --- a/code/espurna/wifi.ino +++ b/code/espurna/wifi.ino @@ -148,11 +148,6 @@ void wifiSetup() { #endif - // Disconnect from MQTT server if no WIFI - if (code != MESSAGE_CONNECTED) { - if (mqttConnected()) mqttDisconnect(); - } - // Configure mDNS if (code == MESSAGE_CONNECTED) {