Skip to content

Commit dfc403c

Browse files
authored
Merge pull request #514 from brentru/fix-wifi-picow
Fix PicoW Infinite Reboot due to WiFi Failure
2 parents 0a24b2c + bb85dcb commit dfc403c

File tree

3 files changed

+43
-14
lines changed

3 files changed

+43
-14
lines changed

src/Wippersnapper.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2362,13 +2362,15 @@ void Wippersnapper::runNetFSM() {
23622362
fsmNetwork = FSM_NET_ESTABLISH_NETWORK;
23632363
break;
23642364
case FSM_NET_ESTABLISH_NETWORK:
2365-
WS_DEBUG_PRINTLN("Connecting to WiFi...");
2365+
WS_DEBUG_PRINTLN("Establishing network connection...");
2366+
WS_PRINTER.flush();
23662367
#ifdef USE_DISPLAY
23672368
if (WS._ui_helper->getLoadingState())
23682369
WS._ui_helper->set_label_status("Connecting to WiFi...");
23692370
#endif
23702371
// Perform a WiFi scan and check if SSID within
23712372
// secrets.json is within the scanned SSIDs
2373+
WS_DEBUG_PRINT("Performing a WiFi scan for SSID...");
23722374
if (!check_valid_ssid()) {
23732375
#ifdef USE_DISPLAY
23742376
WS._ui_helper->show_scr_error("ERROR",
@@ -2378,24 +2380,26 @@ void Wippersnapper::runNetFSM() {
23782380
haltError("ERROR: Unable to find WiFi network, rebooting soon...",
23792381
WS_LED_STATUS_WIFI_CONNECTING);
23802382
}
2383+
WS_DEBUG_PRINTLN("SSID found!");
23812384
// Attempt to connect to wireless network
23822385
maxAttempts = 5;
23832386
while (maxAttempts > 0) {
23842387
// blink before we connect
23852388
statusLEDBlink(WS_LED_STATUS_WIFI_CONNECTING);
2386-
WS.feedWDT();
2389+
feedWDT();
23872390
// attempt to connect
2388-
WS_DEBUG_PRINTLN("Attempting to connect to WiFi...");
2391+
WS_DEBUG_PRINT("Connecting to WiFi (attempt #");
2392+
WS_DEBUG_PRINT(5 - maxAttempts);
2393+
WS_DEBUG_PRINTLN(")");
2394+
WS_PRINTER.flush();
2395+
feedWDT();
23892396
_connect();
2390-
WS.feedWDT();
2391-
// blink to simulate a delay to allow wifi connection to process
2392-
statusLEDBlink(WS_LED_STATUS_WIFI_CONNECTING);
2397+
feedWDT();
23932398
// did we connect?
23942399
if (networkStatus() == WS_NET_CONNECTED)
23952400
break;
23962401
maxAttempts--;
23972402
}
2398-
23992403
// Validate connection
24002404
if (networkStatus() != WS_NET_CONNECTED) {
24012405
WS_DEBUG_PRINTLN("ERROR: Unable to connect to WiFi!");
@@ -2412,7 +2416,6 @@ void Wippersnapper::runNetFSM() {
24122416
fsmNetwork = FSM_NET_CHECK_NETWORK;
24132417
break;
24142418
case FSM_NET_ESTABLISH_MQTT:
2415-
WS_DEBUG_PRINTLN("Attempting to connect to IO...");
24162419
#ifdef USE_DISPLAY
24172420
if (WS._ui_helper->getLoadingState())
24182421
WS._ui_helper->set_label_status("Connecting to IO...");
@@ -2421,16 +2424,25 @@ void Wippersnapper::runNetFSM() {
24212424
// Attempt to connect
24222425
maxAttempts = 5;
24232426
while (maxAttempts > 0) {
2427+
WS_DEBUG_PRINT("Connecting to AIO MQTT (attempt #");
2428+
WS_DEBUG_PRINT(5 - maxAttempts);
2429+
WS_DEBUG_PRINTLN(")");
2430+
WS_PRINTER.flush();
2431+
WS_DEBUG_PRINT("WiFi Status: ");
2432+
WS_DEBUG_PRINTLN(networkStatus());
2433+
WS_PRINTER.flush();
2434+
feedWDT();
24242435
statusLEDBlink(WS_LED_STATUS_MQTT_CONNECTING);
2436+
feedWDT();
24252437
int8_t mqttRC = WS._mqtt->connect();
2438+
feedWDT();
24262439
if (mqttRC == WS_MQTT_CONNECTED) {
24272440
fsmNetwork = FSM_NET_CHECK_MQTT;
24282441
break;
24292442
}
24302443
WS_DEBUG_PRINTLN(
24312444
"Unable to connect to Adafruit IO MQTT, retrying in 3 seconds...");
24322445
statusLEDBlink(WS_LED_STATUS_MQTT_CONNECTING);
2433-
delay(1800);
24342446
maxAttempts--;
24352447
}
24362448
if (fsmNetwork != FSM_NET_CHECK_MQTT) {

src/Wippersnapper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
#endif
7272

7373
#define WS_VERSION \
74-
"1.0.0-alpha.74" ///< WipperSnapper app. version (semver-formatted)
74+
"1.0.0-beta.74" ///< WipperSnapper app. version (semver-formatted)
7575

7676
// Reserved Adafruit IO MQTT topics
7777
#define TOPIC_IO_THROTTLE "/throttle" ///< Adafruit IO Throttle MQTT Topic

src/network_interfaces/ws_networking_pico.h

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* please support Adafruit and open-source hardware by purchasing
99
* products from Adafruit!
1010
*
11-
* Copyright (c) Brent Rubell 2022 for Adafruit Industries.
11+
* Copyright (c) Brent Rubell 2023 for Adafruit Industries.
1212
*
1313
* MIT license, all text here must be included in any redistribution.
1414
*
@@ -261,10 +261,25 @@ class ws_networking_pico : public Wippersnapper {
261261
_status = WS_SSID_INVALID;
262262
} else {
263263
_disconnect();
264-
delay(100);
264+
delay(5000);
265+
WS.feedWDT();
266+
WiFi.mode(WIFI_STA);
267+
WS.feedWDT();
268+
WiFi.setTimeout(20000);
269+
WS.feedWDT();
265270
WiFi.begin(_ssid, _pass);
271+
// Wait setTimeout duration for a connection and check if connected every
272+
// 5 seconds
273+
for (int i = 0; i < 4; i++) {
274+
WS.feedWDT();
275+
delay(5000);
276+
WS.feedWDT();
277+
if (WiFi.status() == WL_CONNECTED) {
278+
_status = WS_NET_CONNECTED;
279+
return;
280+
}
281+
}
266282
_status = WS_NET_DISCONNECTED;
267-
delay(5000);
268283
}
269284
}
270285

@@ -274,8 +289,10 @@ class ws_networking_pico : public Wippersnapper {
274289
*/
275290
/**************************************************************************/
276291
void _disconnect() {
292+
WS.feedWDT();
277293
WiFi.disconnect();
278-
delay(500);
294+
delay(5000);
295+
WS.feedWDT();
279296
}
280297
};
281298

0 commit comments

Comments
 (0)