diff --git a/src/mesh/http/WiFiAPClient.cpp b/src/mesh/http/WiFiAPClient.cpp index cf27d9302..d5a8fb3e9 100644 --- a/src/mesh/http/WiFiAPClient.cpp +++ b/src/mesh/http/WiFiAPClient.cpp @@ -39,6 +39,14 @@ unsigned long lastrun_ntp = 0; static bool needReconnect = true; // If we create our reconnector, run it once at the beginning +static Periodic *wifiReconnect; + +void triggerReconnect() +{ + needReconnect = true; + wifiReconnect->setIntervalFromNow(1000); +} + static int32_t reconnectWiFi() { const char *wifiName = config.network.wifi_ssid; @@ -74,6 +82,9 @@ static int32_t reconnectWiFi() } else { DEBUG_MSG("NTP Update failed\n"); + WiFi.disconnect(false, true); + needReconnect = true; + return 1000; } } #endif @@ -85,8 +96,6 @@ static int32_t reconnectWiFi() } } -static Periodic *wifiReconnect; - bool isWifiAvailable() { @@ -100,20 +109,10 @@ bool isWifiAvailable() // Disable WiFi void deinitWifi() { - /* - Note from Jm (jm@casler.org - Sept 16, 2020): - - A bug in the ESP32 SDK was introduced in Oct 2019 that keeps the WiFi radio from - turning back on after it's shut off. See: - https://github.com/espressif/arduino-esp32/issues/3522 - - Until then, WiFi should only be allowed when there's no power - saving on the 2.4g transceiver. - */ - DEBUG_MSG("WiFi deinit\n"); if (isWifiAvailable()) { + WiFi.disconnect(true); WiFi.mode(WIFI_MODE_NULL); DEBUG_MSG("WiFi Turned Off\n"); // WiFi.printDiag(Serial); diff --git a/src/mesh/http/WiFiAPClient.h b/src/mesh/http/WiFiAPClient.h index 9729c24b5..f45eb0c5d 100644 --- a/src/mesh/http/WiFiAPClient.h +++ b/src/mesh/http/WiFiAPClient.h @@ -13,6 +13,8 @@ bool initWifi(); void deinitWifi(); +void triggerReconnect(); + bool isWifiAvailable(); uint8_t getWifiDisconnectReason(); diff --git a/src/mqtt/MQTT.cpp b/src/mqtt/MQTT.cpp index 244cf1458..a3ae51ae1 100644 --- a/src/mqtt/MQTT.cpp +++ b/src/mqtt/MQTT.cpp @@ -1,4 +1,5 @@ #include "MQTT.h" +#include "mesh/http/WiFiAPClient.h" #include "MeshService.h" #include "NodeDB.h" #include "PowerFSM.h" @@ -164,6 +165,10 @@ void MQTT::reconnect() sendSubscriptions(); } else DEBUG_MSG("Failed to contact MQTT server...\n"); +#if HAS_WIFI + WiFi.disconnect(false, true); + triggerReconnect(); +#endif } }