Skip to content

Commit c92b9b5

Browse files
authored
Merge pull request #166 from adafruit/network-handling
Better signaling for network connection
2 parents 985319d + c2a23ce commit c92b9b5

File tree

4 files changed

+61
-12
lines changed

4 files changed

+61
-12
lines changed

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Adafruit WipperSnapper Beta
2-
version=1.0.0-beta.12
2+
version=1.0.0-beta.13
33
author=Adafruit
44
maintainer=Adafruit <[email protected]>
55
sentence=Arduino library for Adafruit.io WipperSnapper

src/Wippersnapper.cpp

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,26 @@ void Wippersnapper::subscribeWSTopics() {
858858
_topic_description_sub->setCallback(cbRegistrationStatus);
859859
}
860860

861+
/**************************************************************************/
862+
/*!
863+
@brief Writes an error message to the serial and the filesystem,
864+
blinks WS_LED_STATUS_ERROR pattern and hangs.
865+
*/
866+
/**************************************************************************/
867+
void Wippersnapper::errorWriteHang(String error) {
868+
// Print error
869+
WS_DEBUG_PRINTLN(error);
870+
#ifdef USE_TINYUSB
871+
_fileSystem->writeErrorToBootOut(error.c_str());
872+
#endif
873+
// Signal and hang forever
874+
while (1) {
875+
WS.feedWDT();
876+
WS.statusLEDBlink(WS_LED_STATUS_ERROR);
877+
delay(1000);
878+
}
879+
}
880+
861881
/**************************************************************************/
862882
/*!
863883
@brief Checks network and MQTT connectivity. Handles network
@@ -871,6 +891,7 @@ void Wippersnapper::runNetFSM() {
871891
// Initial state
872892
fsm_net_t fsmNetwork;
873893
fsmNetwork = FSM_NET_CHECK_MQTT;
894+
int maxAttempts;
874895
while (fsmNetwork != FSM_NET_CONNECTED) {
875896
switch (fsmNetwork) {
876897
case FSM_NET_CHECK_MQTT:
@@ -893,21 +914,48 @@ void Wippersnapper::runNetFSM() {
893914
break;
894915
case FSM_NET_ESTABLISH_NETWORK:
895916
// WS_DEBUG_PRINTLN("FSM_NET_ESTABLISH_NETWORK");
896-
setStatusLEDColor(LED_NET_CONNECT);
897-
_connect();
898-
// transition
899-
fsmNetwork = FSM_NET_CHECK_NETWORK;
917+
// Attempt to connect to wireless network
918+
maxAttempts = 5;
919+
while (maxAttempts >= 0) {
920+
setStatusLEDColor(LED_NET_CONNECT);
921+
WS.feedWDT();
922+
// attempt to connect
923+
_connect();
924+
// did we connect?
925+
if (networkStatus() == WS_NET_CONNECTED)
926+
break;
927+
setStatusLEDColor(BLACK);
928+
maxAttempts--;
929+
}
930+
// Validate connection
931+
if (networkStatus() == WS_NET_CONNECTED) {
932+
fsmNetwork = FSM_NET_CHECK_NETWORK;
933+
break;
934+
} else { // unrecoverable error, hang forever
935+
errorWriteHang("ERROR: Unable to connect to Wireless Network");
936+
}
900937
break;
901938
case FSM_NET_ESTABLISH_MQTT:
902939
// WS_DEBUG_PRINTLN("FSM_NET_ESTABLISH_MQTT");
903-
setStatusLEDColor(LED_IO_CONNECT);
904940
WS._mqtt->setKeepAliveInterval(WS_KEEPALIVE_INTERVAL);
905-
mqttRC = WS._mqtt->connect(WS._username, WS._key);
906-
if (mqttRC == WS_MQTT_CONNECTED) {
907-
fsmNetwork = FSM_NET_CHECK_MQTT;
941+
// Attempt to connect
942+
maxAttempts = 10;
943+
while (maxAttempts >= 0) {
944+
setStatusLEDColor(LED_IO_CONNECT);
945+
mqttRC = WS._mqtt->connect(WS._username, WS._key);
946+
if (mqttRC == WS_MQTT_CONNECTED) {
947+
fsmNetwork = FSM_NET_CHECK_MQTT;
948+
break;
949+
}
950+
setStatusLEDColor(BLACK);
951+
delay(1000);
952+
maxAttempts--;
953+
}
954+
if (fsmNetwork == FSM_NET_CHECK_MQTT) {
908955
break;
956+
} else { // unrecoverable error, hang forever
957+
errorWriteHang("ERROR: Unable to connect to Adafruit.IO");
909958
}
910-
fsmNetwork = FSM_NET_CHECK_NETWORK;
911959
break;
912960
default:
913961
// don't feed wdt

src/Wippersnapper.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
#endif
6161

6262
#define WS_VERSION \
63-
"1.0.0-beta.12" ///< WipperSnapper app. version (semver-formatted)
63+
"1.0.0-beta.13" ///< WipperSnapper app. version (semver-formatted)
6464

6565
// Reserved Adafruit IO MQTT topics
6666
#define TOPIC_IO_THROTTLE "/throttle" ///< Adafruit IO Throttle MQTT Topic
@@ -225,6 +225,7 @@ class Wippersnapper {
225225

226226
// Errors
227227
void haltError(String error);
228+
void errorWriteHang(String error);
228229

229230
// MQTT topic callbacks //
230231
// Decodes a signal message

src/network_interfaces/Wippersnapper_ESP32.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ class Wippersnapper_ESP32 : public Wippersnapper {
189189
while (WiFi.status() != WL_CONNECTED && millis() - startRetry < 10000) {
190190
// do nothing, busy loop during the timeout
191191
}
192-
}
192+
}
193193

194194
/**************************************************************************/
195195
/*!

0 commit comments

Comments
 (0)