Skip to content

Commit ea6aa78

Browse files
authored
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.
1 parent 39a86b0 commit ea6aa78

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

lib/framework/WiFiSettingsService.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ WiFiSettingsService::WiFiSettingsService(AsyncWebServer* server, FS* fs, Securit
1818
WiFi.onEvent(
1919
std::bind(&WiFiSettingsService::onStationModeDisconnected, this, std::placeholders::_1, std::placeholders::_2),
2020
WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
21+
WiFi.onEvent(std::bind(&WiFiSettingsService::onStationModeStop, this, std::placeholders::_1, std::placeholders::_2),
22+
WiFiEvent_t::SYSTEM_EVENT_STA_STOP);
2123
#elif defined(ESP8266)
2224
_onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(
2325
std::bind(&WiFiSettingsService::onStationModeDisconnected, this, std::placeholders::_1));
@@ -54,7 +56,8 @@ void WiFiSettingsService::readFromJsonObject(JsonObject& root) {
5456
// Turning off static ip config if we don't meet the minimum requirements
5557
// of ipAddress, gateway and subnet. This may change to static ip only
5658
// as sensible defaults can be assumed for gateway and subnet
57-
if (_settings.staticIPConfig && (_settings.localIP == INADDR_NONE || _settings.gatewayIP == INADDR_NONE || _settings.subnetMask == INADDR_NONE)) {
59+
if (_settings.staticIPConfig &&
60+
(_settings.localIP == INADDR_NONE || _settings.gatewayIP == INADDR_NONE || _settings.subnetMask == INADDR_NONE)) {
5861
_settings.staticIPConfig = false;
5962
}
6063
}
@@ -79,11 +82,17 @@ void WiFiSettingsService::onConfigUpdated() {
7982
}
8083

8184
void WiFiSettingsService::reconfigureWiFiConnection() {
82-
// disconnect and de-configure wifi
83-
WiFi.disconnect(true);
84-
8585
// reset last connection attempt to force loop to reconnect immediately
8686
_lastConnectionAttempt = 0;
87+
88+
// disconnect and de-configure wifi
89+
#ifdef ESP32
90+
if (WiFi.disconnect(true)) {
91+
_stopping = true;
92+
}
93+
#elif defined(ESP8266)
94+
WiFi.disconnect(true);
95+
#endif
8796
}
8897

8998
void WiFiSettingsService::readIP(JsonObject& root, String key, IPAddress& _ip) {
@@ -136,6 +145,12 @@ void WiFiSettingsService::manageSTA() {
136145
void WiFiSettingsService::onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) {
137146
WiFi.disconnect(true);
138147
}
148+
void WiFiSettingsService::onStationModeStop(WiFiEvent_t event, WiFiEventInfo_t info) {
149+
if (_stopping) {
150+
_lastConnectionAttempt = 0;
151+
_stopping = false;
152+
}
153+
}
139154
#elif defined(ESP8266)
140155
void WiFiSettingsService::onStationModeDisconnected(const WiFiEventStationModeDisconnected& event) {
141156
WiFi.disconnect(true);

lib/framework/WiFiSettingsService.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#define WIFI_SETTINGS_FILE "/config/wifiSettings.json"
88
#define WIFI_SETTINGS_SERVICE_PATH "/rest/wifiSettings"
9-
#define WIFI_RECONNECTION_DELAY 1000 * 60
9+
#define WIFI_RECONNECTION_DELAY 1000 * 30
1010

1111
class WiFiSettings {
1212
public:
@@ -41,7 +41,9 @@ class WiFiSettingsService : public AdminSettingsService<WiFiSettings> {
4141
unsigned long _lastConnectionAttempt;
4242

4343
#ifdef ESP32
44+
bool _stopping;
4445
void onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info);
46+
void onStationModeStop(WiFiEvent_t event, WiFiEventInfo_t info);
4547
#elif defined(ESP8266)
4648
WiFiEventHandler _onStationModeDisconnectedHandler;
4749
void onStationModeDisconnected(const WiFiEventStationModeDisconnected& event);

0 commit comments

Comments
 (0)