From 39df7108a889bf0a07dc98a3c1a920864a59aef2 Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Mon, 2 Aug 2021 11:28:57 -0700 Subject: [PATCH] fix wifi hang when bad password used, cleanup wifi in general --- src/mesh/http/WiFiAPClient.cpp | 59 ++++++++++++++++++---------------- src/mesh/http/WiFiAPClient.h | 2 -- 2 files changed, 32 insertions(+), 29 deletions(-) diff --git a/src/mesh/http/WiFiAPClient.cpp b/src/mesh/http/WiFiAPClient.cpp index 6cccd6acc..13d4cd444 100644 --- a/src/mesh/http/WiFiAPClient.cpp +++ b/src/mesh/http/WiFiAPClient.cpp @@ -1,5 +1,6 @@ #include "mesh/http/WiFiAPClient.h" #include "NodeDB.h" +#include "concurrency/Periodic.h" #include "configuration.h" #include "main.h" #include "mesh/http/WebServer.h" @@ -9,6 +10,8 @@ #include #include +using namespace concurrency; + static void WiFiEvent(WiFiEvent_t event); // DNS Server for the Captive Portal @@ -23,6 +26,32 @@ bool forcedSoftAP = 0; bool APStartupComplete = 0; +static bool needReconnect = true; // If we create our reconnector, run it once at the beginning + +static int32_t reconnectWiFi() +{ + if (radioConfig.has_preferences && needReconnect) { + + const char *wifiName = radioConfig.preferences.wifi_ssid; + const char *wifiPsw = radioConfig.preferences.wifi_password; + + if (!*wifiPsw) // Treat empty password as no password + wifiPsw = NULL; + + if (*wifiName) { + needReconnect = false; + + DEBUG_MSG("... Reconnecting to WiFi access point"); + WiFi.mode(WIFI_MODE_STA); + WiFi.begin(wifiName, wifiPsw); + } + } + + return 30 * 1000; // every 30 seconds +} + +static Periodic *wifiReconnect; + bool isSoftAPForced() { return forcedSoftAP; @@ -155,12 +184,8 @@ void initWifi(bool forceSoftAP) }, WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED); - DEBUG_MSG("JOINING WIFI: ssid=%s\n", wifiName); - if (WiFi.begin(wifiName, wifiPsw) == WL_CONNECTED) { - DEBUG_MSG("MY IP ADDRESS: %s\n", WiFi.localIP().toString().c_str()); - } else { - DEBUG_MSG("Started Joining WIFI\n"); - } + DEBUG_MSG("JOINING WIFI soon: ssid=%s\n", wifiName); + wifiReconnect = new Periodic("WifiConnect", reconnectWiFi); } } @@ -205,7 +230,7 @@ static void WiFiEvent(WiFiEvent_t event) DEBUG_MSG("Disconnected from WiFi access point\n"); // Event 5 - reconnectWiFi(); + needReconnect = true; break; case SYSTEM_EVENT_STA_AUTHMODE_CHANGE: DEBUG_MSG("Authentication mode of access point has changed\n"); @@ -302,26 +327,6 @@ void handleDNSResponse() } } -void reconnectWiFi() -{ - if (radioConfig.has_preferences) { - - const char *wifiName = radioConfig.preferences.wifi_ssid; - const char *wifiPsw = radioConfig.preferences.wifi_password; - - if (!*wifiPsw) // Treat empty password as no password - wifiPsw = NULL; - - if (*wifiName) { - - DEBUG_MSG("... Reconnecting to WiFi access point"); - - WiFi.mode(WIFI_MODE_STA); - WiFi.begin(wifiName, wifiPsw); - } - } -} - uint8_t getWifiDisconnectReason() { return wifiDisconnectReason; diff --git a/src/mesh/http/WiFiAPClient.h b/src/mesh/http/WiFiAPClient.h index 9e2f8ad78..cb27bb4a8 100644 --- a/src/mesh/http/WiFiAPClient.h +++ b/src/mesh/http/WiFiAPClient.h @@ -16,8 +16,6 @@ bool isWifiAvailable(); void handleDNSResponse(); -void reconnectWiFi(); - bool isSoftAPForced(); uint8_t getWifiDisconnectReason();