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.
This commit is contained in:
		| @@ -18,6 +18,8 @@ WiFiSettingsService::WiFiSettingsService(AsyncWebServer* server, FS* fs, Securit | |||||||
|   WiFi.onEvent( |   WiFi.onEvent( | ||||||
|       std::bind(&WiFiSettingsService::onStationModeDisconnected, this, std::placeholders::_1, std::placeholders::_2), |       std::bind(&WiFiSettingsService::onStationModeDisconnected, this, std::placeholders::_1, std::placeholders::_2), | ||||||
|       WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED); |       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) | #elif defined(ESP8266) | ||||||
|   _onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected( |   _onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected( | ||||||
|       std::bind(&WiFiSettingsService::onStationModeDisconnected, this, std::placeholders::_1)); |       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 |   // 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 |   // of ipAddress, gateway and subnet. This may change to static ip only | ||||||
|   // as sensible defaults can be assumed for gateway and subnet |   // 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; |     _settings.staticIPConfig = false; | ||||||
|   } |   } | ||||||
| } | } | ||||||
| @@ -79,11 +82,17 @@ void WiFiSettingsService::onConfigUpdated() { | |||||||
| } | } | ||||||
|  |  | ||||||
| void WiFiSettingsService::reconfigureWiFiConnection() { | void WiFiSettingsService::reconfigureWiFiConnection() { | ||||||
|   // disconnect and de-configure wifi |  | ||||||
|   WiFi.disconnect(true); |  | ||||||
|  |  | ||||||
|   // reset last connection attempt to force loop to reconnect immediately |   // reset last connection attempt to force loop to reconnect immediately | ||||||
|   _lastConnectionAttempt = 0; |   _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) { | 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) { | void WiFiSettingsService::onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) { | ||||||
|   WiFi.disconnect(true); |   WiFi.disconnect(true); | ||||||
| } | } | ||||||
|  | void WiFiSettingsService::onStationModeStop(WiFiEvent_t event, WiFiEventInfo_t info) { | ||||||
|  |   if (_stopping) { | ||||||
|  |     _lastConnectionAttempt = 0; | ||||||
|  |     _stopping = false; | ||||||
|  |   } | ||||||
|  | } | ||||||
| #elif defined(ESP8266) | #elif defined(ESP8266) | ||||||
| void WiFiSettingsService::onStationModeDisconnected(const WiFiEventStationModeDisconnected& event) { | void WiFiSettingsService::onStationModeDisconnected(const WiFiEventStationModeDisconnected& event) { | ||||||
|   WiFi.disconnect(true); |   WiFi.disconnect(true); | ||||||
|   | |||||||
| @@ -6,7 +6,7 @@ | |||||||
|  |  | ||||||
| #define WIFI_SETTINGS_FILE "/config/wifiSettings.json" | #define WIFI_SETTINGS_FILE "/config/wifiSettings.json" | ||||||
| #define WIFI_SETTINGS_SERVICE_PATH "/rest/wifiSettings" | #define WIFI_SETTINGS_SERVICE_PATH "/rest/wifiSettings" | ||||||
| #define WIFI_RECONNECTION_DELAY 1000 * 60 | #define WIFI_RECONNECTION_DELAY 1000 * 30 | ||||||
|  |  | ||||||
| class WiFiSettings { | class WiFiSettings { | ||||||
|  public: |  public: | ||||||
| @@ -41,7 +41,9 @@ class WiFiSettingsService : public AdminSettingsService<WiFiSettings> { | |||||||
|   unsigned long _lastConnectionAttempt; |   unsigned long _lastConnectionAttempt; | ||||||
|  |  | ||||||
| #ifdef ESP32 | #ifdef ESP32 | ||||||
|  |   bool _stopping; | ||||||
|   void onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info); |   void onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info); | ||||||
|  |   void onStationModeStop(WiFiEvent_t event, WiFiEventInfo_t info); | ||||||
| #elif defined(ESP8266) | #elif defined(ESP8266) | ||||||
|   WiFiEventHandler _onStationModeDisconnectedHandler; |   WiFiEventHandler _onStationModeDisconnectedHandler; | ||||||
|   void onStationModeDisconnected(const WiFiEventStationModeDisconnected& event); |   void onStationModeDisconnected(const WiFiEventStationModeDisconnected& event); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user