Skip to content

Commit 4d500e8

Browse files
committed
WiFiManager: added workaround for #234
This is an ugly workaround for the issue where every other attempt to connect to a wifi network fails. This patch just tries tries to connect for a second time if the first time fails. Workaround for: espressif/arduino-esp32#234
1 parent 97b382e commit 4d500e8

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

WiFiManager.cpp

+31-12
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,35 @@ boolean WiFiManager::startConfigPortal(char const *apName, char const *apPasswo
243243
return WiFi.status() == WL_CONNECTED;
244244
}
245245

246-
247246
int WiFiManager::connectWifi(String ssid, String pass) {
248247
DEBUG_WM(F("Connecting as wifi client..."));
249248

249+
int connRes = doConnectWifi(ssid, pass, 0);
250+
if (connRes != WL_CONNECTED) {
251+
// Connection failed; could be due to this issue where every 2nd connect
252+
// attempt fails: https://github.com/espressif/arduino-esp32/issues/234
253+
// As temporary workaround: try to connect for second time
254+
WiFi.disconnect(true);
255+
connRes = doConnectWifi(ssid, pass, 1);
256+
}
257+
DEBUG_WM ("Connection result: ");
258+
DEBUG_WM ( connRes );
259+
260+
//not connected, WPS enabled, no pass - first attempt
261+
if (_tryWPS && connRes != WL_CONNECTED && pass == "") {
262+
startWPS();
263+
//should be connected at the end of WPS
264+
connRes = waitForConnectResult();
265+
}
266+
return connRes;
267+
}
268+
269+
int WiFiManager::doConnectWifi(String ssid, String pass, int count) {
250270
// check if we've got static_ip settings, if we do, use those.
251271
if (_sta_static_ip) {
252-
DEBUG_WM(F("Custom STA IP/GW/Subnet"));
272+
if (count == 0) {
273+
DEBUG_WM(F("Custom STA IP/GW/Subnet"));
274+
}
253275
WiFi.config(_sta_static_ip, _sta_static_gw, _sta_static_sn);
254276
DEBUG_WM(WiFi.localIP());
255277
}
@@ -263,7 +285,9 @@ int WiFiManager::connectWifi(String ssid, String pass) {
263285
WiFi.begin(ssid.c_str(), pass.c_str());
264286
} else {
265287
if (WiFi.SSID()) {
266-
DEBUG_WM("Using last saved values, should be faster");
288+
if (count == 0) {
289+
DEBUG_WM("Using last saved values, should be faster");
290+
}
267291
#if defined(ESP8266)
268292
//trying to fix connection in progress hanging
269293
ETS_UART_INTR_DISABLE();
@@ -275,19 +299,14 @@ int WiFiManager::connectWifi(String ssid, String pass) {
275299

276300
WiFi.begin();
277301
} else {
278-
DEBUG_WM("No saved credentials");
302+
if (count == 0) {
303+
DEBUG_WM("No saved credentials");
304+
}
279305
}
280306
}
281307

282308
int connRes = waitForConnectResult();
283-
DEBUG_WM ("Connection result: ");
284-
DEBUG_WM ( connRes );
285-
//not connected, WPS enabled, no pass - first attempt
286-
if (_tryWPS && connRes != WL_CONNECTED && pass == "") {
287-
startWPS();
288-
//should be connected at the end of WPS
289-
connRes = waitForConnectResult();
290-
}
309+
291310
return connRes;
292311
}
293312

WiFiManager.h

+1
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ class WiFiManager
167167

168168
int status = WL_IDLE_STATUS;
169169
int connectWifi(String ssid, String pass);
170+
int doConnectWifi(String ssid, String pass, int count);
170171
uint8_t waitForConnectResult();
171172

172173
void handleRoot();

0 commit comments

Comments
 (0)