Skip to content

Commit 02d20d2

Browse files
committed
fix: autoreconnect logic
1 parent 0579ec9 commit 02d20d2

File tree

2 files changed

+13
-25
lines changed

2 files changed

+13
-25
lines changed

src/NetWizard.cpp

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,7 @@ void NetWizard::autoConnect(const char* ssid, const char* password) {
8888
// Credentials loaded successfully
8989
WiFi.mode(WIFI_STA);
9090
WiFi.persistent(false);
91-
#if defined(ESP8266) || defined(ESP32)
92-
WiFi.setAutoReconnect(false);
93-
#endif
94-
_connect(_nw.sta.ssid.c_str(), _nw.sta.password.c_str());
91+
_connect(_nw.sta.ssid.c_str(), _nw.sta.password.c_str(), true);
9592

9693
// Check if connected within connection timeout
9794
unsigned long startMillis = millis();
@@ -116,10 +113,7 @@ void NetWizard::autoConnect(const char* ssid, const char* password) {
116113
NETWIZARD_DEBUG_MSG("Trying to connect again...\n");
117114
WiFi.mode(WIFI_STA);
118115
WiFi.persistent(false);
119-
#if defined(ESP8266) || defined(ESP32)
120-
WiFi.setAutoReconnect(false);
121-
#endif
122-
_connect(_nw.sta.ssid.c_str(), _nw.sta.password.c_str());
116+
_connect(_nw.sta.ssid.c_str(), _nw.sta.password.c_str(), true);
123117
// last connect time
124118
lastConnectMillis = millis();
125119
}
@@ -212,7 +206,7 @@ IPAddress NetWizard::subnetMask() {
212206

213207
bool NetWizard::connect() {
214208
if (_nw.sta.configured) {
215-
_connect(_nw.sta.ssid.c_str(), _nw.sta.password.c_str());
209+
_connect(_nw.sta.ssid.c_str(), _nw.sta.password.c_str(), true);
216210
return true;
217211
}
218212
return false;
@@ -226,7 +220,7 @@ bool NetWizard::connect(const char* ssid, const char* password) {
226220
// save credentials
227221
_saveSTACredentials();
228222
// connect
229-
_connect(_nw.sta.ssid.c_str(), _nw.sta.password.c_str());
223+
_connect(_nw.sta.ssid.c_str(), _nw.sta.password.c_str(), true);
230224
return true;
231225
}
232226

@@ -332,10 +326,7 @@ void NetWizard::loop() {
332326
NETWIZARD_DEBUG_MSG("Password: " + _nw.portal.sta.password + " \n");
333327
// Connect to temporary credentials
334328
WiFi.persistent(false);
335-
#if defined(ESP8266) || defined(ESP32)
336-
WiFi.setAutoReconnect(false);
337-
#endif
338-
_connect(_nw.portal.sta.ssid.c_str(), _nw.portal.sta.password.c_str());
329+
_connect(_nw.portal.sta.ssid.c_str(), _nw.portal.sta.password.c_str(), false);
339330
_nw.portal.connect_millis = millis();
340331
_nw.portal.state = NetWizardPortalState::WAITING_FOR_CONNECTION;
341332
break;
@@ -419,12 +410,15 @@ void NetWizard::removeParameter(NetWizardParameter* parameter) {
419410
}
420411
}
421412

422-
void NetWizard::_connect(const char* ssid, const char* password) {
413+
void NetWizard::_connect(const char* ssid, const char* password, bool autoreconnect) {
414+
// Set auto reconnect
415+
if (autoreconnect) {
416+
WiFi.setAutoReconnect(true);
417+
}
423418
// Set hostname
424419
WiFi.setHostname(_nw.hostname.c_str());
425420
// Connect to WiFi
426421
WiFi.begin(ssid, password);
427-
WiFi.setAutoReconnect(true);
428422
_nw.status = NetWizardConnectionStatus::CONNECTING;
429423
}
430424

@@ -1097,17 +1091,14 @@ void NetWizard::_stopHTTP() {
10971091
void NetWizard::_startPortal() {
10981092
WiFi.mode(WIFI_AP_STA);
10991093
WiFi.persistent(false);
1100-
#if defined(ESP8266) || defined(ESP32)
1101-
WiFi.setAutoReconnect(false);
1102-
#endif
11031094
if (this->isConfigured()) {
11041095
// If connection status is not NOT_FOUND, then disconnect
11051096
if (_nw.status == NetWizardConnectionStatus::NOT_FOUND) {
11061097
NETWIZARD_DEBUG_MSG("Configured connection not found. Starting portal with AP only.\n");
11071098
_disconnect();
11081099
} else {
11091100
NETWIZARD_DEBUG_MSG("Starting portal in AP+STA mode\n");
1110-
_connect(_nw.sta.ssid.c_str(), _nw.sta.password.c_str());
1101+
_connect(_nw.sta.ssid.c_str(), _nw.sta.password.c_str(), true);
11111102
}
11121103
WiFi.softAP(_nw.portal.ap.ssid.c_str(), _nw.portal.ap.password.c_str());
11131104
} else {
@@ -1164,10 +1155,7 @@ void NetWizard::_stopPortal() {
11641155
NETWIZARD_DEBUG_MSG("Connecting to configured connection\n");
11651156
WiFi.mode(WIFI_STA);
11661157
WiFi.persistent(false);
1167-
#if defined(ESP8266) || defined(ESP32)
1168-
WiFi.setAutoReconnect(true);
1169-
#endif
1170-
_connect(_nw.sta.ssid.c_str(), _nw.sta.password.c_str());
1158+
_connect(_nw.sta.ssid.c_str(), _nw.sta.password.c_str(), true);
11711159
} else {
11721160
NETWIZARD_DEBUG_MSG("Switching off wifi as the device is not configured\n");
11731161
WiFi.mode(WIFI_OFF);

src/NetWizard.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ class NetWizard {
269269
} _nw;
270270

271271
// helper functions
272-
void _connect(const char* ssid, const char* password);
272+
void _connect(const char* ssid, const char* password, bool autoreconnect = false);
273273
void _disconnect();
274274
void _saveSTACredentials();
275275
void _loadSTACredentials();

0 commit comments

Comments
 (0)