since some time I am playing with the 8266, before I only worked with the ESP32.
Where I use the ESPs, wifi is only sporadically available.
Therefore, I have always worked with the ESP32 with scripts that turn off the WLAN after no signal is present and then activated at regular intervals (1std) again to see if WLAN is available again and to connect again.
This script has been running stable for months.
I have now adapted it to the ESP8266, but there it does not run permanently stable. After a few days, sometimes weeks, the ram begins to dwindle when reconnecting.
What then at some point leads to a crash.
I have tested with the 2.7.4 and the 3.0.2
This is how I connect:
void Connect() {
// WiFi.mode(WIFI_OFF);
Serial.setDebugOutput(true);
WiFi.persistent(false); // https://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/generic-class.html#mode
Serial.println(F("WiFi.persistent(false) - Done"));
Serial.print(F("Remaining free RAM: "));
Serial.println(formatBytes(ESP.getFreeHeap()));
WiFi.setAutoReconnect(false);
Serial.println(F("WiFi.setAutoReconnect(false) - Done"));
Serial.print(F("Remaining free RAM: "));
Serial.println(formatBytes(ESP.getFreeHeap()));
WiFi.mode(WIFI_STA);
Serial.println(F("WiFi.mode(WIFI_STA) - Done"));
Serial.print(F("Remaining free RAM: "));
Serial.println(formatBytes(ESP.getFreeHeap()));
WiFi.hostname(HOSTNAME);
Serial.println(F("WiFi.hostname(HOSTNAME) - Done"));
Serial.print(F("Remaining free RAM: "));
Serial.println(formatBytes(ESP.getFreeHeap()));
WiFi.begin("xxx", "xxx");
Serial.println(F("WiFi.begin(xxx, xxx) - Done"));
Serial.print(F("Remaining free RAM: "));
Serial.println(formatBytes(ESP.getFreeHeap()));
WiFiOff = false;
}
with this I check if WiFi is available or not
void onWifiConnect(const WiFiEventStationModeGotIP& event) {
#ifdef DEBUGSerial
Serial.println(F("Connected to Wi-Fi."));
#endif
WiFiconnected = true;
TelnetPrint.begin();
WiFiconnecting_count = 0;
WiFiOff_count = 0;
}
void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
WiFiconnected = false;
TelnetPrint.stop();
#ifdef DEBUGSerial
Serial.println(F("Disconnected from Wi-Fi."));
#endif
}
This is a part of my logging:
05:13:03.498 WiFiconnected: 0
05:13:03.498 (IP unset)
05:13:03.498 WiFi-OFF: 1
05:13:03.498 WiFi-OFF_count: 1
05:13:03.498 WiFi-Connecting_count: 0
05:13:03.498 Measurement_count: 15
05:13:03.498 Unix-Time: 1658891575
05:13:03.498 Records in cache: 372
05:13:03.498
05:13:03.498 Remaining free RAM: 25.48 KB
05:13:03.498 Lowest free RAM: 25.20 KB
05:13:03.498 RSSI: --> 31
05:13:03.505 Heap Fragmentation: --> 3
05:13:03.505 Heap LargestBlock: --> 24.84 KB
05:13:03.505 Try to reconnect
05:13:03.511 WiFi.persistent(false) - Done
05:13:03.511 Remaining free RAM: 25.48 KB
05:13:03.527 WiFi.setAutoReconnect(false) - Done
05:13:03.527 Remaining free RAM: 25.48 KB
05:13:03.527 mode : sta(44:17:93:0d:59:ce)
add if0
WiFi.mode(WIFI_STA) - Done
05:13:03.527 Remaining free RAM: 25.24 KB
05:13:03.534 WiFi.hostname(ESP8266-Keller): lwIP error -16 on interface st (index 0) WiFi.hostname(HOSTNAME) - Done
05:13:03.540 Remaining free RAM: 25.24 KB
05:13:03.540 WiFi.begin(xxx, xxx) - Done
05:13:03.540 Remaining free RAM: 25.02 KB
05:13:03.547 ___________________________
05:13:03.547 Time taken by Heartbeat: 54 milliseconds
05:13:04.502 wifi evt: 8 WiFiconnected: 0
05:13:04.502 (IP unset)
05:13:04.502 WiFi-OFF: 0
05:13:04.502 WiFi-OFF_count: 0
05:13:04.502 WiFi-Connecting_count: 19
05:13:04.502 Measurement_count: 14
05:13:04.502 Unix-Time: 1658891576
05:13:04.502 Records in cache: 372
05:13:04.502
05:13:04.502 Remaining free RAM: 23.72 KB
05:13:04.502 Lowest free RAM: 23.72 KB
05:13:04.502 RSSI: --> 31
05:13:04.502 Heap Fragmentation: --> 2
05:13:04.508 Heap LargestBlock: --> 23.34 KB
05:13:04.508 ___________________________
05:13:04.515 Time taken by Heartbeat: 16 milliseconds
We can see in the run 05:13:03 that the "WiFiOFF_count:" has counted down to 1 and counts to "0" in this heartbeat loop and so " Void Connect" is called.
I checked the individual components of "Void Connect" for RAM loss, they are not.
The loop is left with 25.02 KB.
In the next Heartbeat Loop 05:13:04 it is suddenly only 23.72 KB.
Any ideas?
What I notice it happens more in areas with poorer WiFi signal