From ec5c58f6077b09cc0cfe112acd9256fcc071e9c3 Mon Sep 17 00:00:00 2001 From: brentru Date: Mon, 4 Oct 2021 14:33:23 -0400 Subject: [PATCH 1/6] add attempts, writing, 1s between checks for network uptime --- library.properties | 2 +- src/Wippersnapper.cpp | 34 ++++++++++++++++++-- src/Wippersnapper.h | 2 +- src/network_interfaces/Wippersnapper_ESP32.h | 8 ++--- 4 files changed, 37 insertions(+), 9 deletions(-) diff --git a/library.properties b/library.properties index 905a32519..e10cc4ea4 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Adafruit WipperSnapper Beta -version=1.0.0-beta.12 +version=1.0.0-beta.13 author=Adafruit maintainer=Adafruit sentence=Arduino library for Adafruit.io WipperSnapper diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index 18215bc29..fee71c338 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -871,6 +871,7 @@ void Wippersnapper::runNetFSM() { // Initial state fsm_net_t fsmNetwork; fsmNetwork = FSM_NET_CHECK_MQTT; + int maxAttempts; while (fsmNetwork != FSM_NET_CONNECTED) { switch (fsmNetwork) { case FSM_NET_CHECK_MQTT: @@ -893,10 +894,37 @@ void Wippersnapper::runNetFSM() { break; case FSM_NET_ESTABLISH_NETWORK: // WS_DEBUG_PRINTLN("FSM_NET_ESTABLISH_NETWORK"); + maxAttempts = 20; setStatusLEDColor(LED_NET_CONNECT); - _connect(); - // transition - fsmNetwork = FSM_NET_CHECK_NETWORK; + + while (maxAttempts >= 0) { + WS.feedWDT(); + // attempt to connect + _connect(); + // did we connect? + if (networkStatus() == WS_NET_CONNECTED) + break; + maxAttempts--; + } + // Validate connection + if (networkStatus() == WS_NET_CONNECTED) { + fsmNetwork = FSM_NET_CHECK_NETWORK; + break; + } + // Otherwise we're not connected to the network + WS_DEBUG_PRINTLN("Unable to connect to the network, error code:"); + WS_DEBUG_PRINTLN(networkStatus()); + // Should be USE_TINYUSB? + _fileSystem->writeErrorToBootOut("ERROR: Unable to connect to WiFi network"); + + // blink and hang forever + while (1) { + WS.feedWDT(); + WS.statusLEDBlink(WS_LED_STATUS_ERROR); + delay(1000); + } + + //fsmNetwork = FSM_NET_CHECK_NETWORK; break; case FSM_NET_ESTABLISH_MQTT: // WS_DEBUG_PRINTLN("FSM_NET_ESTABLISH_MQTT"); diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index 2474fd4a7..e0379e871 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -60,7 +60,7 @@ #endif #define WS_VERSION \ - "1.0.0-beta.12" ///< WipperSnapper app. version (semver-formatted) + "1.0.0-beta.13" ///< WipperSnapper app. version (semver-formatted) // Reserved Adafruit IO MQTT topics #define TOPIC_IO_THROTTLE "/throttle" ///< Adafruit IO Throttle MQTT Topic diff --git a/src/network_interfaces/Wippersnapper_ESP32.h b/src/network_interfaces/Wippersnapper_ESP32.h index 475ea8ae2..4a5aa3c0f 100644 --- a/src/network_interfaces/Wippersnapper_ESP32.h +++ b/src/network_interfaces/Wippersnapper_ESP32.h @@ -181,15 +181,15 @@ class Wippersnapper_ESP32 : public Wippersnapper { delay(100); WiFi.begin(_ssid, _pass); _status = WS_NET_DISCONNECTED; - delay(100); + delay(1000); } - // wait for a connection to be established +/* // wait for a connection to be established long startRetry = millis(); while (WiFi.status() != WL_CONNECTED && millis() - startRetry < 10000) { // do nothing, busy loop during the timeout - } - } + } */ +} /**************************************************************************/ /*! From 1a07c03ae05b4e68328de1cc00bbdf46cda7e88f Mon Sep 17 00:00:00 2001 From: brentru Date: Mon, 4 Oct 2021 15:01:09 -0400 Subject: [PATCH 2/6] add errorWriteHang, blink as it connects --- src/Wippersnapper.cpp | 43 +++++++++++++++++++++++++++---------------- src/Wippersnapper.h | 1 + 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index fee71c338..fefbe4b10 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -858,6 +858,27 @@ void Wippersnapper::subscribeWSTopics() { _topic_description_sub->setCallback(cbRegistrationStatus); } +/**************************************************************************/ +/*! + @brief Writes an error message to the serial and the filesystem, + blinks WS_LED_STATUS_ERROR pattern and hangs. +*/ +/**************************************************************************/ +void Wippersnapper::errorWriteHang(String error) { + // Otherwise we're not connected to the network + WS_DEBUG_PRINTLN(error); + // Should be USE_TINYUSB? Should check inside or here..? + _fileSystem->writeErrorToBootOut("ERROR: Unable to connect to WiFi network"); + + // blink and hang forever + while (1) { + WS.feedWDT(); + WS.statusLEDBlink(WS_LED_STATUS_ERROR); + delay(1000); + } +} + + /**************************************************************************/ /*! @brief Checks network and MQTT connectivity. Handles network @@ -895,36 +916,26 @@ void Wippersnapper::runNetFSM() { case FSM_NET_ESTABLISH_NETWORK: // WS_DEBUG_PRINTLN("FSM_NET_ESTABLISH_NETWORK"); maxAttempts = 20; - setStatusLEDColor(LED_NET_CONNECT); - while (maxAttempts >= 0) { + setStatusLEDColor(LED_NET_CONNECT); WS.feedWDT(); // attempt to connect _connect(); // did we connect? if (networkStatus() == WS_NET_CONNECTED) break; + // Blink LED? + setStatusLEDColor(BLACK); maxAttempts--; } // Validate connection if (networkStatus() == WS_NET_CONNECTED) { fsmNetwork = FSM_NET_CHECK_NETWORK; break; + } else { + // unrecoverable error, hang forever + errorWriteHang("Unable to connect to Wireless Network"); } - // Otherwise we're not connected to the network - WS_DEBUG_PRINTLN("Unable to connect to the network, error code:"); - WS_DEBUG_PRINTLN(networkStatus()); - // Should be USE_TINYUSB? - _fileSystem->writeErrorToBootOut("ERROR: Unable to connect to WiFi network"); - - // blink and hang forever - while (1) { - WS.feedWDT(); - WS.statusLEDBlink(WS_LED_STATUS_ERROR); - delay(1000); - } - - //fsmNetwork = FSM_NET_CHECK_NETWORK; break; case FSM_NET_ESTABLISH_MQTT: // WS_DEBUG_PRINTLN("FSM_NET_ESTABLISH_MQTT"); diff --git a/src/Wippersnapper.h b/src/Wippersnapper.h index e0379e871..adc891592 100644 --- a/src/Wippersnapper.h +++ b/src/Wippersnapper.h @@ -225,6 +225,7 @@ class Wippersnapper { // Errors void haltError(String error); + void errorWriteHang(String error); // MQTT topic callbacks // // Decodes a signal message From dc692d0a8e8daf3dc7b687ec91dd06cd85c940df Mon Sep 17 00:00:00 2001 From: brentru Date: Mon, 4 Oct 2021 15:38:06 -0400 Subject: [PATCH 3/6] handle MQTT case? --- src/Wippersnapper.cpp | 59 +++++++++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 25 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index fefbe4b10..37f1a5955 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -865,20 +865,19 @@ void Wippersnapper::subscribeWSTopics() { */ /**************************************************************************/ void Wippersnapper::errorWriteHang(String error) { - // Otherwise we're not connected to the network - WS_DEBUG_PRINTLN(error); - // Should be USE_TINYUSB? Should check inside or here..? - _fileSystem->writeErrorToBootOut("ERROR: Unable to connect to WiFi network"); - - // blink and hang forever - while (1) { - WS.feedWDT(); - WS.statusLEDBlink(WS_LED_STATUS_ERROR); - delay(1000); - } + // Print error + WS_DEBUG_PRINTLN(error); +#ifdef USE_TINYUSB + _fileSystem->writeErrorToBootOut("errorName"); +#endif + // Signal and hang forever + while (1) { + WS.feedWDT(); + WS.statusLEDBlink(WS_LED_STATUS_ERROR); + delay(1000); + } } - /**************************************************************************/ /*! @brief Checks network and MQTT connectivity. Handles network @@ -915,7 +914,8 @@ void Wippersnapper::runNetFSM() { break; case FSM_NET_ESTABLISH_NETWORK: // WS_DEBUG_PRINTLN("FSM_NET_ESTABLISH_NETWORK"); - maxAttempts = 20; + // Attempt to connect to wireless network + maxAttempts = 2; while (maxAttempts >= 0) { setStatusLEDColor(LED_NET_CONNECT); WS.feedWDT(); @@ -924,29 +924,38 @@ void Wippersnapper::runNetFSM() { // did we connect? if (networkStatus() == WS_NET_CONNECTED) break; - // Blink LED? setStatusLEDColor(BLACK); + delay(500); maxAttempts--; } - // Validate connection + // Validate connection OK if (networkStatus() == WS_NET_CONNECTED) { - fsmNetwork = FSM_NET_CHECK_NETWORK; - break; - } else { - // unrecoverable error, hang forever - errorWriteHang("Unable to connect to Wireless Network"); + fsmNetwork = FSM_NET_CHECK_NETWORK; + break; + } else { // unrecoverable error, hang forever + errorWriteHang("ERROR: Unable to connect to Wireless Network"); } break; case FSM_NET_ESTABLISH_MQTT: // WS_DEBUG_PRINTLN("FSM_NET_ESTABLISH_MQTT"); - setStatusLEDColor(LED_IO_CONNECT); WS._mqtt->setKeepAliveInterval(WS_KEEPALIVE_INTERVAL); - mqttRC = WS._mqtt->connect(WS._username, WS._key); - if (mqttRC == WS_MQTT_CONNECTED) { - fsmNetwork = FSM_NET_CHECK_MQTT; + // Attempt to connect + maxAttempts = 2; + while (maxAttempts >= 0) { + setStatusLEDColor(LED_IO_CONNECT); + mqttRC = WS._mqtt->connect(WS._username, WS._key); + if (mqttRC == WS_MQTT_CONNECTED) { + fsmNetwork = FSM_NET_CHECK_MQTT; + break; + } + setStatusLEDColor(BLACK); + delay(500;) + } + if (fsmNetwork == FSM_NET_CHECK_MQTT) { break; + } else { // unrecoverable error, hang forever + errorWriteHang("ERROR: Unable to connect to Adafruit.IO"); } - fsmNetwork = FSM_NET_CHECK_NETWORK; break; default: // don't feed wdt From 8a989a0d7b7fb9b8684e5e6d1321c12876309eeb Mon Sep 17 00:00:00 2001 From: brentru Date: Mon, 4 Oct 2021 16:01:49 -0400 Subject: [PATCH 4/6] wait in network adapter --- src/Wippersnapper.cpp | 7 +++---- src/network_interfaces/Wippersnapper_ESP32.h | 6 +++--- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index 37f1a5955..f9e35ad40 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -915,7 +915,7 @@ void Wippersnapper::runNetFSM() { case FSM_NET_ESTABLISH_NETWORK: // WS_DEBUG_PRINTLN("FSM_NET_ESTABLISH_NETWORK"); // Attempt to connect to wireless network - maxAttempts = 2; + maxAttempts = 5; while (maxAttempts >= 0) { setStatusLEDColor(LED_NET_CONNECT); WS.feedWDT(); @@ -925,10 +925,9 @@ void Wippersnapper::runNetFSM() { if (networkStatus() == WS_NET_CONNECTED) break; setStatusLEDColor(BLACK); - delay(500); maxAttempts--; } - // Validate connection OK + // Validate connection if (networkStatus() == WS_NET_CONNECTED) { fsmNetwork = FSM_NET_CHECK_NETWORK; break; @@ -949,7 +948,7 @@ void Wippersnapper::runNetFSM() { break; } setStatusLEDColor(BLACK); - delay(500;) + delay(500); } if (fsmNetwork == FSM_NET_CHECK_MQTT) { break; diff --git a/src/network_interfaces/Wippersnapper_ESP32.h b/src/network_interfaces/Wippersnapper_ESP32.h index 4a5aa3c0f..ff1f67cf6 100644 --- a/src/network_interfaces/Wippersnapper_ESP32.h +++ b/src/network_interfaces/Wippersnapper_ESP32.h @@ -181,14 +181,14 @@ class Wippersnapper_ESP32 : public Wippersnapper { delay(100); WiFi.begin(_ssid, _pass); _status = WS_NET_DISCONNECTED; - delay(1000); + delay(100); } -/* // wait for a connection to be established + // wait for a connection to be established long startRetry = millis(); while (WiFi.status() != WL_CONNECTED && millis() - startRetry < 10000) { // do nothing, busy loop during the timeout - } */ + } } /**************************************************************************/ From a39b4c0c692db22c21919e472f51cfa6d23a036e Mon Sep 17 00:00:00 2001 From: brentru Date: Mon, 4 Oct 2021 16:05:38 -0400 Subject: [PATCH 5/6] decrement attempts, increase max for MQTT! --- src/Wippersnapper.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index f9e35ad40..fdd55ff0d 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -939,7 +939,7 @@ void Wippersnapper::runNetFSM() { // WS_DEBUG_PRINTLN("FSM_NET_ESTABLISH_MQTT"); WS._mqtt->setKeepAliveInterval(WS_KEEPALIVE_INTERVAL); // Attempt to connect - maxAttempts = 2; + maxAttempts = 10; while (maxAttempts >= 0) { setStatusLEDColor(LED_IO_CONNECT); mqttRC = WS._mqtt->connect(WS._username, WS._key); @@ -948,7 +948,8 @@ void Wippersnapper::runNetFSM() { break; } setStatusLEDColor(BLACK); - delay(500); + delay(1000); + maxAttempts--; } if (fsmNetwork == FSM_NET_CHECK_MQTT) { break; From c2a23cec051fff9b7232aa290e17a5c334b4eb3d Mon Sep 17 00:00:00 2001 From: brentru Date: Mon, 4 Oct 2021 16:12:23 -0400 Subject: [PATCH 6/6] use c_str() --- src/Wippersnapper.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Wippersnapper.cpp b/src/Wippersnapper.cpp index fdd55ff0d..5d3a6c96f 100644 --- a/src/Wippersnapper.cpp +++ b/src/Wippersnapper.cpp @@ -868,7 +868,7 @@ void Wippersnapper::errorWriteHang(String error) { // Print error WS_DEBUG_PRINTLN(error); #ifdef USE_TINYUSB - _fileSystem->writeErrorToBootOut("errorName"); + _fileSystem->writeErrorToBootOut(error.c_str()); #endif // Signal and hang forever while (1) {