Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better signaling for network connection #166

Merged
merged 6 commits into from
Oct 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Adafruit WipperSnapper Beta
version=1.0.0-beta.12
version=1.0.0-beta.13
author=Adafruit
maintainer=Adafruit <[email protected]>
sentence=Arduino library for Adafruit.io WipperSnapper
Expand Down
66 changes: 57 additions & 9 deletions src/Wippersnapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,26 @@ 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) {
// Print error
WS_DEBUG_PRINTLN(error);
#ifdef USE_TINYUSB
_fileSystem->writeErrorToBootOut(error.c_str());
#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
Expand All @@ -871,6 +891,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:
Expand All @@ -893,21 +914,48 @@ void Wippersnapper::runNetFSM() {
break;
case FSM_NET_ESTABLISH_NETWORK:
// WS_DEBUG_PRINTLN("FSM_NET_ESTABLISH_NETWORK");
setStatusLEDColor(LED_NET_CONNECT);
_connect();
// transition
fsmNetwork = FSM_NET_CHECK_NETWORK;
// Attempt to connect to wireless network
maxAttempts = 5;
while (maxAttempts >= 0) {
setStatusLEDColor(LED_NET_CONNECT);
WS.feedWDT();
// attempt to connect
_connect();
// did we connect?
if (networkStatus() == WS_NET_CONNECTED)
break;
setStatusLEDColor(BLACK);
maxAttempts--;
}
// Validate connection
if (networkStatus() == WS_NET_CONNECTED) {
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 = 10;
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(1000);
maxAttempts--;
}
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
Expand Down
3 changes: 2 additions & 1 deletion src/Wippersnapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -225,6 +225,7 @@ class Wippersnapper {

// Errors
void haltError(String error);
void errorWriteHang(String error);

// MQTT topic callbacks //
// Decodes a signal message
Expand Down
2 changes: 1 addition & 1 deletion src/network_interfaces/Wippersnapper_ESP32.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ class Wippersnapper_ESP32 : public Wippersnapper {
while (WiFi.status() != WL_CONNECTED && millis() - startRetry < 10000) {
// do nothing, busy loop during the timeout
}
}
}

/**************************************************************************/
/*!
Expand Down