From 2eb34e83f806c0f703ab119ffaf141b8590cf15b Mon Sep 17 00:00:00 2001 From: Rick Watson Date: Wed, 5 Jun 2019 00:05:16 +0100 Subject: [PATCH] Access Point only comes back up if WiFi is properly disconnected on failures. --- src/WiFiSettingsService.cpp | 26 ++++++++++++++++++++------ src/WiFiSettingsService.h | 7 +++++++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/WiFiSettingsService.cpp b/src/WiFiSettingsService.cpp index 7a2da4e..9fe8f7e 100644 --- a/src/WiFiSettingsService.cpp +++ b/src/WiFiSettingsService.cpp @@ -1,6 +1,12 @@ #include -WiFiSettingsService::WiFiSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) : AdminSettingsService(server, fs, securityManager, WIFI_SETTINGS_SERVICE_PATH, WIFI_SETTINGS_FILE) {} +WiFiSettingsService::WiFiSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) : AdminSettingsService(server, fs, securityManager, WIFI_SETTINGS_SERVICE_PATH, WIFI_SETTINGS_FILE) { +#if defined(ESP8266) + _onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(std::bind(&WiFiSettingsService::onStationModeDisconnected, this, std::placeholders::_1)); +#elif defined(ESP_PLATFORM) + WiFi.onEvent(std::bind(&WiFiSettingsService::onStationModeDisconnected, this, std::placeholders::_1, std::placeholders::_2), WiFiEvent_t::SYSTEM_EVENT_AP_STADISCONNECTED); +#endif +} WiFiSettingsService::~WiFiSettingsService() {} @@ -56,7 +62,7 @@ void WiFiSettingsService::onConfigUpdated() { } void WiFiSettingsService::reconfigureWiFiConnection() { - // disconnect and de-configure wifi and software access point + // disconnect and de-configure wifi WiFi.disconnect(true); // reset last connection attempt to force loop to reconnect immediately @@ -106,8 +112,16 @@ void WiFiSettingsService::manageSTA() { } // attempt to connect to the network WiFi.begin(_ssid.c_str(), _password.c_str()); - } else { - Serial.println("Retrying WiFi connection."); - WiFi.reconnect(); - } + } } + +#if defined(ESP8266) +void WiFiSettingsService::onStationModeDisconnected(const WiFiEventStationModeDisconnected& event) { + WiFi.disconnect(true); +} +#elif defined(ESP_PLATFORM) +void WiFiSettingsService::onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) { + WiFi.disconnect(true); +} +#endif + diff --git a/src/WiFiSettingsService.h b/src/WiFiSettingsService.h index a631258..e846728 100644 --- a/src/WiFiSettingsService.h +++ b/src/WiFiSettingsService.h @@ -41,6 +41,13 @@ class WiFiSettingsService : public AdminSettingsService { IPAddress _dnsIP1; IPAddress _dnsIP2; +#if defined(ESP8266) + WiFiEventHandler _onStationModeDisconnectedHandler; + void onStationModeDisconnected(const WiFiEventStationModeDisconnected& event); +#elif defined(ESP_PLATFORM) + void onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info); +#endif + void readIP(JsonObject& root, String key, IPAddress& _ip); void writeIP(JsonObject& root, String key, IPAddress& _ip); void reconfigureWiFiConnection();