Skip to content

Commit 7d72b88

Browse files
Merge pull request #24 from TimothyFran/dev
Implemented WiFi AutoReconnect
2 parents ce8b90e + 4406b4d commit 7d72b88

File tree

2 files changed

+15
-24
lines changed

2 files changed

+15
-24
lines changed

src/NetWizard.cpp

Lines changed: 14 additions & 23 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,7 +410,13 @@ 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+
#if defined(ESP8266) || defined(ESP32)
415+
// Set auto reconnect
416+
if (autoreconnect) {
417+
WiFi.setAutoReconnect(true);
418+
}
419+
#endif
423420
// Set hostname
424421
WiFi.setHostname(_nw.hostname.c_str());
425422
// Connect to WiFi
@@ -1097,17 +1094,14 @@ void NetWizard::_stopHTTP() {
10971094
void NetWizard::_startPortal() {
10981095
WiFi.mode(WIFI_AP_STA);
10991096
WiFi.persistent(false);
1100-
#if defined(ESP8266) || defined(ESP32)
1101-
WiFi.setAutoReconnect(false);
1102-
#endif
11031097
if (this->isConfigured()) {
11041098
// If connection status is not NOT_FOUND, then disconnect
11051099
if (_nw.status == NetWizardConnectionStatus::NOT_FOUND) {
11061100
NETWIZARD_DEBUG_MSG("Configured connection not found. Starting portal with AP only.\n");
11071101
_disconnect();
11081102
} else {
11091103
NETWIZARD_DEBUG_MSG("Starting portal in AP+STA mode\n");
1110-
_connect(_nw.sta.ssid.c_str(), _nw.sta.password.c_str());
1104+
_connect(_nw.sta.ssid.c_str(), _nw.sta.password.c_str(), true);
11111105
}
11121106
WiFi.softAP(_nw.portal.ap.ssid.c_str(), _nw.portal.ap.password.c_str());
11131107
} else {
@@ -1164,10 +1158,7 @@ void NetWizard::_stopPortal() {
11641158
NETWIZARD_DEBUG_MSG("Connecting to configured connection\n");
11651159
WiFi.mode(WIFI_STA);
11661160
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());
1161+
_connect(_nw.sta.ssid.c_str(), _nw.sta.password.c_str(), true);
11711162
} else {
11721163
NETWIZARD_DEBUG_MSG("Switching off wifi as the device is not configured\n");
11731164
WiFi.mode(WIFI_OFF);

src/NetWizard.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ class NetWizard {
283283
} _nw;
284284

285285
// helper functions
286-
void _connect(const char* ssid, const char* password);
286+
void _connect(const char* ssid, const char* password, bool autoreconnect = false);
287287
void _disconnect();
288288
void _saveSTACredentials();
289289
void _loadSTACredentials();

0 commit comments

Comments
 (0)