@@ -243,13 +243,35 @@ boolean WiFiManager::startConfigPortal(char const *apName, char const *apPasswo
243
243
return WiFi.status () == WL_CONNECTED;
244
244
}
245
245
246
-
247
246
int WiFiManager::connectWifi (String ssid, String pass) {
248
247
DEBUG_WM (F (" Connecting as wifi client..." ));
249
248
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) {
250
270
// check if we've got static_ip settings, if we do, use those.
251
271
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
+ }
253
275
WiFi.config (_sta_static_ip, _sta_static_gw, _sta_static_sn);
254
276
DEBUG_WM (WiFi.localIP ());
255
277
}
@@ -263,7 +285,9 @@ int WiFiManager::connectWifi(String ssid, String pass) {
263
285
WiFi.begin (ssid.c_str (), pass.c_str ());
264
286
} else {
265
287
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
+ }
267
291
#if defined(ESP8266)
268
292
// trying to fix connection in progress hanging
269
293
ETS_UART_INTR_DISABLE ();
@@ -275,19 +299,14 @@ int WiFiManager::connectWifi(String ssid, String pass) {
275
299
276
300
WiFi.begin ();
277
301
} else {
278
- DEBUG_WM (" No saved credentials" );
302
+ if (count == 0 ) {
303
+ DEBUG_WM (" No saved credentials" );
304
+ }
279
305
}
280
306
}
281
307
282
308
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
+
291
310
return connRes;
292
311
}
293
312
0 commit comments