Skip to content

Commit 3bb5fad

Browse files
committed
There has been an assumption that device has to be told to connect but Wifi already does it's best to connect in the background. Calling WiFi.begin while device is already trying to connect will occasionally cause WiFi to lock up. Only call WiFi.begin after calling WiFi.disconnect. These changes are aimed at stopping WiFi.begin from being called at the wrong time.
The concept of autoconnect is also deprecated because it is trying to do what the device is already doing. Calling this method will now block until WiFi connects. It has been changed to wait for 10 seconds then go into setup mode if connection fails. Sketch can avoid blocking call then use (WiFi.status()==WL_CONNECTED) test to see if connected yet. Removed - Tries to reconnect in station mode with whatever credentials have been saved when StartConfigPortal times out otherwise device will be without WiFi after exiting StartConfigPortal. - because it was wrong.
1 parent 8f5171c commit 3bb5fad

File tree

2 files changed

+31
-37
lines changed

2 files changed

+31
-37
lines changed

WiFiManager.cpp

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,11 @@ boolean WiFiManager::autoConnect() {
127127
String ssid = "ESP" + String(ESP.getChipId());
128128
return autoConnect(ssid.c_str(), NULL);
129129
}
130-
130+
/* This is not very useful as there has been an assumption that device has to be
131+
told to connect but Wifi already does it's best to connect in background. Calling this
132+
method will block until WiFi connects. Sketch can avoid
133+
blocking call then use (WiFi.status()==WL_CONNECTED) test to see if connected yet.
134+
*/
131135
boolean WiFiManager::autoConnect(char const *apName, char const *apPassword) {
132136
DEBUG_WM(F(""));
133137
DEBUG_WM(F("AutoConnect"));
@@ -136,15 +140,22 @@ boolean WiFiManager::autoConnect(char const *apName, char const *apPassword) {
136140
//String ssid = getSSID();
137141
//String pass = getPassword();
138142

139-
// attempt to connect; should it fail, fall back to AP
143+
// device will attempt to connect by itself; wait 10 secs
144+
// to see if it succeeds and should it fail, fall back to AP
140145
WiFi.mode(WIFI_STA);
141-
142-
if (connectWifi("", "") == WL_CONNECTED) {
143-
DEBUG_WM(F("IP Address:"));
144-
DEBUG_WM(WiFi.localIP());
145-
//connected
146-
return true;
147-
}
146+
unsigned long startedAt = millis();
147+
while(millis() - startedAt < 10000)
148+
{
149+
delay(100);
150+
if (WiFi.status()==WL_CONNECTED) {
151+
float waited = startedAt - millis();
152+
DEBUG_WM(F("After waiting "));
153+
DEBUG_WM(waited/1000);
154+
DEBUG_WM(F(" secs local ip: "));
155+
DEBUG_WM(WiFi.localIP());
156+
return true;
157+
}
158+
}
148159

149160
return startConfigPortal(apName, apPassword);
150161
}
@@ -208,13 +219,6 @@ boolean WiFiManager::startConfigPortal(char const *apName, char const *apPasswo
208219
}
209220
yield();
210221
}
211-
//Timed out so go back to station mode using system-stored ssid and pass
212-
if (connectWifi(WiFi.SSID().c_str(),WiFi.psk().c_str()) != WL_CONNECTED) {
213-
DEBUG_WM(F("Failed to connect after configuration mode timeout."));
214-
} else {
215-
//connected
216-
WiFi.mode(WIFI_STA);
217-
}
218222
server.reset();
219223
dnsServer.reset();
220224

@@ -231,26 +235,16 @@ int WiFiManager::connectWifi(String ssid, String pass) {
231235
WiFi.config(_sta_static_ip, _sta_static_gw, _sta_static_sn);
232236
DEBUG_WM(WiFi.localIP());
233237
}
234-
//fix for auto connect racing issue
235-
if (WiFi.status() == WL_CONNECTED) {
236-
DEBUG_WM("Already connected. Bailing out.");
237-
return WL_CONNECTED;
238-
}
239-
//check if we have ssid and pass and force those, if not, try with last saved values
238+
//check if we have ssid and pass and force those, if not,
239+
//do nothing as it will try with last saved values automatically
240240
if (ssid != "") {
241-
WiFi.begin(ssid.c_str(), pass.c_str());
242-
} else {
243-
if(WiFi.SSID()) {
244-
DEBUG_WM("Using last saved values, should be faster");
245-
//trying to fix connection in progress hanging
246-
ETS_UART_INTR_DISABLE();
247-
wifi_station_disconnect();
248-
ETS_UART_INTR_ENABLE();
249-
250-
WiFi.begin();
251-
} else {
241+
resetSettings(); /*Disconnect and wipe out old values. If you don't do this
242+
esp8266 will sometimes lock up when SSID or password is different to
243+
the already stored values. Mostly it doesn't but occasionally it does.
244+
*/
245+
WiFi.begin(ssid.c_str(), pass.c_str());// Start Wifi with new values.
246+
} else if(!WiFi.SSID()) {
252247
DEBUG_WM("No saved credentials");
253-
}
254248
}
255249

256250
int connRes = waitForConnectResult();
@@ -320,7 +314,7 @@ String WiFiManager::getConfigPortalSSID() {
320314

321315
void WiFiManager::resetSettings() {
322316
DEBUG_WM(F("settings invalidated"));
323-
DEBUG_WM(F("THIS MAY CAUSE AP NOT TO START UP PROPERLY. YOU NEED TO COMMENT IT OUT AFTER ERASING THE DATA."));
317+
//DEBUG_WM(F("THIS MAY CAUSE AP NOT TO START UP PROPERLY. YOU NEED TO COMMENT IT OUT AFTER ERASING THE DATA."));
324318
WiFi.disconnect(true);
325319
//delay(200);
326320
}

WiFiManager.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ class WiFiManager
7070
public:
7171
WiFiManager();
7272

73-
boolean autoConnect();
74-
boolean autoConnect(char const *apName, char const *apPassword = NULL);
73+
boolean autoConnect(); //Deprecated. Do not use.
74+
boolean autoConnect(char const *apName, char const *apPassword = NULL); //Deprecated. Do not use.
7575

7676
//if you want to always start the config portal, without trying to connect first
7777
boolean startConfigPortal();

0 commit comments

Comments
 (0)