Skip to content

Commit 2eb34e8

Browse files
committed
Access Point only comes back up if WiFi is properly disconnected on failures.
1 parent e7248c4 commit 2eb34e8

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

src/WiFiSettingsService.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
#include <WiFiSettingsService.h>
22

3-
WiFiSettingsService::WiFiSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) : AdminSettingsService(server, fs, securityManager, WIFI_SETTINGS_SERVICE_PATH, WIFI_SETTINGS_FILE) {}
3+
WiFiSettingsService::WiFiSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager) : AdminSettingsService(server, fs, securityManager, WIFI_SETTINGS_SERVICE_PATH, WIFI_SETTINGS_FILE) {
4+
#if defined(ESP8266)
5+
_onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(std::bind(&WiFiSettingsService::onStationModeDisconnected, this, std::placeholders::_1));
6+
#elif defined(ESP_PLATFORM)
7+
WiFi.onEvent(std::bind(&WiFiSettingsService::onStationModeDisconnected, this, std::placeholders::_1, std::placeholders::_2), WiFiEvent_t::SYSTEM_EVENT_AP_STADISCONNECTED);
8+
#endif
9+
}
410

511
WiFiSettingsService::~WiFiSettingsService() {}
612

@@ -56,7 +62,7 @@ void WiFiSettingsService::onConfigUpdated() {
5662
}
5763

5864
void WiFiSettingsService::reconfigureWiFiConnection() {
59-
// disconnect and de-configure wifi and software access point
65+
// disconnect and de-configure wifi
6066
WiFi.disconnect(true);
6167

6268
// reset last connection attempt to force loop to reconnect immediately
@@ -106,8 +112,16 @@ void WiFiSettingsService::manageSTA() {
106112
}
107113
// attempt to connect to the network
108114
WiFi.begin(_ssid.c_str(), _password.c_str());
109-
} else {
110-
Serial.println("Retrying WiFi connection.");
111-
WiFi.reconnect();
112-
}
115+
}
116+
}
117+
118+
#if defined(ESP8266)
119+
void WiFiSettingsService::onStationModeDisconnected(const WiFiEventStationModeDisconnected& event) {
120+
WiFi.disconnect(true);
121+
}
122+
#elif defined(ESP_PLATFORM)
123+
void WiFiSettingsService::onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) {
124+
WiFi.disconnect(true);
113125
}
126+
#endif
127+

src/WiFiSettingsService.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ class WiFiSettingsService : public AdminSettingsService {
4141
IPAddress _dnsIP1;
4242
IPAddress _dnsIP2;
4343

44+
#if defined(ESP8266)
45+
WiFiEventHandler _onStationModeDisconnectedHandler;
46+
void onStationModeDisconnected(const WiFiEventStationModeDisconnected& event);
47+
#elif defined(ESP_PLATFORM)
48+
void onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info);
49+
#endif
50+
4451
void readIP(JsonObject& root, String key, IPAddress& _ip);
4552
void writeIP(JsonObject& root, String key, IPAddress& _ip);
4653
void reconfigureWiFiConnection();

0 commit comments

Comments
 (0)