@@ -858,6 +858,26 @@ void Wippersnapper::subscribeWSTopics() {
858
858
_topic_description_sub->setCallback (cbRegistrationStatus);
859
859
}
860
860
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
+
861
881
/* *************************************************************************/
862
882
/* !
863
883
@brief Checks network and MQTT connectivity. Handles network
@@ -871,6 +891,7 @@ void Wippersnapper::runNetFSM() {
871
891
// Initial state
872
892
fsm_net_t fsmNetwork;
873
893
fsmNetwork = FSM_NET_CHECK_MQTT;
894
+ int maxAttempts;
874
895
while (fsmNetwork != FSM_NET_CONNECTED) {
875
896
switch (fsmNetwork) {
876
897
case FSM_NET_CHECK_MQTT:
@@ -893,21 +914,48 @@ void Wippersnapper::runNetFSM() {
893
914
break ;
894
915
case FSM_NET_ESTABLISH_NETWORK:
895
916
// 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
+ }
900
937
break ;
901
938
case FSM_NET_ESTABLISH_MQTT:
902
939
// WS_DEBUG_PRINTLN("FSM_NET_ESTABLISH_MQTT");
903
- setStatusLEDColor (LED_IO_CONNECT);
904
940
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) {
908
955
break ;
956
+ } else { // unrecoverable error, hang forever
957
+ errorWriteHang (" ERROR: Unable to connect to Adafruit.IO" );
909
958
}
910
- fsmNetwork = FSM_NET_CHECK_NETWORK;
911
959
break ;
912
960
default :
913
961
// don't feed wdt
0 commit comments