From ea6aa78d608806f0fb587ec47b81dd160af68b90 Mon Sep 17 00:00:00 2001 From: rjwats Date: Sat, 1 Feb 2020 20:21:18 +0000 Subject: [PATCH] Fix issue under ESP32 where there is a delay re-configuring WiFi When re-configuring we must wait for it to disconnect and stop before reconnecting. --- lib/framework/WiFiSettingsService.cpp | 23 +++++++++++++++++++---- lib/framework/WiFiSettingsService.h | 4 +++- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/lib/framework/WiFiSettingsService.cpp b/lib/framework/WiFiSettingsService.cpp index 3526332..53f844c 100644 --- a/lib/framework/WiFiSettingsService.cpp +++ b/lib/framework/WiFiSettingsService.cpp @@ -18,6 +18,8 @@ WiFiSettingsService::WiFiSettingsService(AsyncWebServer* server, FS* fs, Securit WiFi.onEvent( std::bind(&WiFiSettingsService::onStationModeDisconnected, this, std::placeholders::_1, std::placeholders::_2), WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED); + WiFi.onEvent(std::bind(&WiFiSettingsService::onStationModeStop, this, std::placeholders::_1, std::placeholders::_2), + WiFiEvent_t::SYSTEM_EVENT_STA_STOP); #elif defined(ESP8266) _onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected( std::bind(&WiFiSettingsService::onStationModeDisconnected, this, std::placeholders::_1)); @@ -54,7 +56,8 @@ void WiFiSettingsService::readFromJsonObject(JsonObject& root) { // Turning off static ip config if we don't meet the minimum requirements // of ipAddress, gateway and subnet. This may change to static ip only // as sensible defaults can be assumed for gateway and subnet - if (_settings.staticIPConfig && (_settings.localIP == INADDR_NONE || _settings.gatewayIP == INADDR_NONE || _settings.subnetMask == INADDR_NONE)) { + if (_settings.staticIPConfig && + (_settings.localIP == INADDR_NONE || _settings.gatewayIP == INADDR_NONE || _settings.subnetMask == INADDR_NONE)) { _settings.staticIPConfig = false; } } @@ -79,11 +82,17 @@ void WiFiSettingsService::onConfigUpdated() { } void WiFiSettingsService::reconfigureWiFiConnection() { - // disconnect and de-configure wifi - WiFi.disconnect(true); - // reset last connection attempt to force loop to reconnect immediately _lastConnectionAttempt = 0; + +// disconnect and de-configure wifi +#ifdef ESP32 + if (WiFi.disconnect(true)) { + _stopping = true; + } +#elif defined(ESP8266) + WiFi.disconnect(true); +#endif } void WiFiSettingsService::readIP(JsonObject& root, String key, IPAddress& _ip) { @@ -136,6 +145,12 @@ void WiFiSettingsService::manageSTA() { void WiFiSettingsService::onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) { WiFi.disconnect(true); } +void WiFiSettingsService::onStationModeStop(WiFiEvent_t event, WiFiEventInfo_t info) { + if (_stopping) { + _lastConnectionAttempt = 0; + _stopping = false; + } +} #elif defined(ESP8266) void WiFiSettingsService::onStationModeDisconnected(const WiFiEventStationModeDisconnected& event) { WiFi.disconnect(true); diff --git a/lib/framework/WiFiSettingsService.h b/lib/framework/WiFiSettingsService.h index 321bcf9..a303f86 100644 --- a/lib/framework/WiFiSettingsService.h +++ b/lib/framework/WiFiSettingsService.h @@ -6,7 +6,7 @@ #define WIFI_SETTINGS_FILE "/config/wifiSettings.json" #define WIFI_SETTINGS_SERVICE_PATH "/rest/wifiSettings" -#define WIFI_RECONNECTION_DELAY 1000 * 60 +#define WIFI_RECONNECTION_DELAY 1000 * 30 class WiFiSettings { public: @@ -41,7 +41,9 @@ class WiFiSettingsService : public AdminSettingsService { unsigned long _lastConnectionAttempt; #ifdef ESP32 + bool _stopping; void onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info); + void onStationModeStop(WiFiEvent_t event, WiFiEventInfo_t info); #elif defined(ESP8266) WiFiEventHandler _onStationModeDisconnectedHandler; void onStationModeDisconnected(const WiFiEventStationModeDisconnected& event);