From 4b557535e6708079be87b8f24428ec1e3c92c413 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Wed, 11 Jan 2017 23:24:49 +0100 Subject: [PATCH] Defer wifi disconnection from web interface to allow request to return --- code/espurna/mqtt.ino | 2 +- code/espurna/web.ino | 9 ++++++++- code/espurna/wifi.ino | 5 ----- 3 files changed, 9 insertions(+), 7 deletions(-) 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) {