@@ -113,6 +113,11 @@ void Wippersnapper::provision() {
113113 _fileSystem->parseSecrets ();
114114#elif defined(USE_LITTLEFS)
115115 _littleFS->parseSecrets ();
116+
117+ // ESP8266 stability: ensure LittleFS is fully unmounted and not referenced
118+ // during WiFi/MQTT connect (flash operations can interfere with WiFi).
119+ delete _littleFS;
120+ _littleFS = NULL ;
116121#else
117122 set_user_key (); // non-fs-backed, sets global credentials within network iface
118123#endif
@@ -2338,17 +2343,17 @@ bool Wippersnapper::generateWSTopics() {
23382343 WS._config .aio_user , _device_uid, TOPIC_SIGNALS, TOPIC_DISPLAY);
23392344 } else {
23402345 WS_DEBUG_PRINTLN (
2341- " FATAL ERROR: Failed to allocate memory for DISPLAY topic!" );
2346+ " FATAL ERROR: Failed to allocate memory for b2d DISPLAY topic!" );
23422347 return false ;
23432348 }
23442349
23452350 // Subscribe to signal's DISPLAY sub-topic and set callback
23462351 _topic_signal_display_sub =
23472352 new Adafruit_MQTT_Subscribe (WS._mqtt , WS._topic_signal_display_brkr , 1 );
2348- WS_DEBUG_PRINTLN (" Subscribing to DISPLAY topic: " );
2353+ WS_DEBUG_PRINTLN (" Subscribing to b2d DISPLAY topic: " );
23492354 WS_DEBUG_PRINTLN (WS._topic_signal_display_brkr );
23502355 WS._mqtt ->subscribe (_topic_signal_display_sub);
2351- WS_DEBUG_PRINTLN (" Subscribed to DISPLAY topic!" );
2356+ WS_DEBUG_PRINTLN (" Subscribed to b2d DISPLAY topic!" );
23522357 _topic_signal_display_sub->setCallback (cbDisplayMessage);
23532358
23542359 // Calculate length of the topic for device-to-broker DISPLAY topic
@@ -2370,7 +2375,7 @@ bool Wippersnapper::generateWSTopics() {
23702375 TOPIC_SIGNALS, TOPIC_DISPLAY);
23712376 } else {
23722377 WS_DEBUG_PRINTLN (
2373- " FATAL ERROR: Failed to allocate memory for DISPLAY topic!" );
2378+ " FATAL ERROR: Failed to allocate memory for d2b DISPLAY topic!" );
23742379 return false ;
23752380 }
23762381
@@ -2553,6 +2558,11 @@ void Wippersnapper::haltError(String error, ws_led_status_t ledStatusColor,
25532558 WS.feedWDT (); // feed the WDT for the first X-5 seconds
25542559 } else if (i == seconds_until_reboot) {
25552560 WS.enableWDT (wdt_timeout_ms);
2561+ #ifdef ARDUINO_ARCH_ESP8266
2562+ // On ESP8266, explicitly restart as well. In some configurations the WDT
2563+ // may not reliably trigger if other system timers keep feeding it.
2564+ ESP.restart ();
2565+ #endif
25562566 }
25572567 }
25582568}
@@ -2771,6 +2781,15 @@ void printDeviceInfo() {
27712781 WS._macAddr [2 ], WS._macAddr [3 ], WS._macAddr [4 ], WS._macAddr [5 ]);
27722782 WS_DEBUG_PRINT (" MAC Address: " );
27732783 WS_DEBUG_PRINTLN (sMAC );
2784+
2785+ #ifdef ARDUINO_ARCH_ESP8266
2786+ WS_DEBUG_PRINT (" Free heap: " );
2787+ WS_DEBUG_PRINTLN (ESP.getFreeHeap ());
2788+ WS_DEBUG_PRINT (" Max free block: " );
2789+ WS_DEBUG_PRINTLN (ESP.getMaxFreeBlockSize ());
2790+ WS_DEBUG_PRINT (" Heap fragmentation (%): " );
2791+ WS_DEBUG_PRINTLN (ESP.getHeapFragmentation ());
2792+ #endif
27742793 WS_DEBUG_PRINTLN (" -------------------------------" );
27752794
27762795// (ESP32-Only) Print reason why device was reset
@@ -2798,6 +2817,62 @@ void Wippersnapper::connect() {
27982817 haltError (" Unable to generate Device UID" );
27992818 }
28002819
2820+ #ifdef ARDUINO_ARCH_ESP8266
2821+ // ESP8266 has very limited heap and WiFi scans/connect need a large
2822+ // contiguous block. Connect WiFi first before allocating MQTT topics.
2823+ WS_DEBUG_PRINTLN (" Running WiFi connect (ESP8266 pre-MQTT)..." );
2824+ WS_DEBUG_PRINT (" Free heap before WiFi connect: " );
2825+ WS_DEBUG_PRINTLN (ESP.getFreeHeap ());
2826+ WS_DEBUG_PRINT (" Max free block before WiFi connect: " );
2827+ WS_DEBUG_PRINTLN (ESP.getMaxFreeBlockSize ());
2828+ WS_DEBUG_PRINT (" Heap fragmentation before WiFi connect (%): " );
2829+ WS_DEBUG_PRINTLN (ESP.getHeapFragmentation ());
2830+
2831+ #if defined(USE_LITTLEFS)
2832+ // Extra safety: ensure LittleFS is not mounted during WiFi/MQTT connect.
2833+ LittleFS.end ();
2834+ #endif
2835+
2836+ if (networkStatus () != WS_NET_CONNECTED) {
2837+ WS_DEBUG_PRINTLN (" Establishing network connection..." );
2838+ WS_PRINTER.flush ();
2839+ WS_DEBUG_PRINT (" Performing a WiFi scan for SSID..." );
2840+ if (!check_valid_ssid ()) {
2841+ haltError (" ERROR: Unable to find WiFi network, rebooting soon..." ,
2842+ WS_LED_STATUS_WIFI_CONNECTING);
2843+ }
2844+
2845+ int maxAttempts = 5 ;
2846+ while (maxAttempts > 0 ) {
2847+ statusLEDBlink (WS_LED_STATUS_WIFI_CONNECTING);
2848+ feedWDT ();
2849+ WS_DEBUG_PRINT (" Connecting to WiFi (attempt #" );
2850+ WS_DEBUG_PRINT (5 - maxAttempts);
2851+ WS_DEBUG_PRINTLN (" )" );
2852+ WS_PRINTER.flush ();
2853+ feedWDT ();
2854+ _connect ();
2855+ feedWDT ();
2856+ if (networkStatus () == WS_NET_CONNECTED)
2857+ break ;
2858+ maxAttempts--;
2859+ }
2860+
2861+ if (networkStatus () != WS_NET_CONNECTED) {
2862+ WS_DEBUG_PRINTLN (" ERROR: Unable to connect to WiFi!" );
2863+ haltError (" ERROR: Unable to connect to WiFi, rebooting soon..." ,
2864+ WS_LED_STATUS_WIFI_CONNECTING);
2865+ }
2866+ }
2867+
2868+ WS_DEBUG_PRINT (" Free heap after WiFi connect: " );
2869+ WS_DEBUG_PRINTLN (ESP.getFreeHeap ());
2870+ WS_DEBUG_PRINT (" Max free block after WiFi connect: " );
2871+ WS_DEBUG_PRINTLN (ESP.getMaxFreeBlockSize ());
2872+ WS_DEBUG_PRINT (" Heap fragmentation after WiFi connect (%): " );
2873+ WS_DEBUG_PRINTLN (ESP.getHeapFragmentation ());
2874+ #endif
2875+
28012876 // Initialize MQTT client with device identifier
28022877 setupMQTTClient (_device_uid);
28032878
@@ -2810,9 +2885,17 @@ void Wippersnapper::connect() {
28102885 haltError (" Unable to allocate space for MQTT error topics" );
28112886 }
28122887
2813- // Connect to Network
2888+ #ifdef ARDUINO_ARCH_ESP8266
2889+ WS_DEBUG_PRINT (" Heap after topic allocation: " );
2890+ WS_DEBUG_PRINTLN (ESP.getFreeHeap ());
2891+ WS_DEBUG_PRINT (" Max free block after topic allocation: " );
2892+ WS_DEBUG_PRINTLN (ESP.getMaxFreeBlockSize ());
2893+ WS_DEBUG_PRINT (" Heap fragmentation after topic allocation (%): " );
2894+ WS_DEBUG_PRINTLN (ESP.getHeapFragmentation ());
2895+ #endif
2896+
2897+ // Connect to Network + MQTT
28142898 WS_DEBUG_PRINTLN (" Running Network FSM..." );
2815- // Run the network fsm
28162899 runNetFSM ();
28172900
28182901 // Enable WDT after wifi connection as wifiMulti doesnt feed WDT
0 commit comments