-->
Page 1 of 1

Reconnect problems with Wifi, sometimes loosing RAM

PostPosted: Fri Aug 12, 2022 3:09 am
by peff
Hi,
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:
Code: Select allvoid 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
Code: Select allvoid 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:

Code: Select all05: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

Re: Reconnect problems with Wifi, sometimes loosing RAM

PostPosted: Fri Aug 12, 2022 11:02 am
by rooppoorali
I think ESP8266 is not suitable for your application. You must stick to ESP32. Because ESP8266 has slower processing speed and less memory size.

Re: Reconnect problems with Wifi, sometimes loosing RAM

PostPosted: Fri Aug 12, 2022 12:27 pm
by peff
nop, the app is very small only every minute one measurement and if Wifi available put it into an influxDB if not store it into a linked list until Wifi is available again.

Re: Reconnect problems with Wifi, sometimes loosing RAM

PostPosted: Fri Sep 30, 2022 7:44 am
by peff
I have been making progress on this problem over the last few weeks.
Two things have significantly reduced the issue.

A:
I only call WiFi.begin() to reconnect.
Everything else like hostname, Wifi.mode() or WiFi.persistent() are only set once in the setup.
The values remain even with WiFi.disconnect(true).


B:
Probably the most important thing.
Deactivating the automatic radio channel setting in the Fritz!Box
I had noticed that the issues mostly occurred when the channel had changed between deactivation and activation of the WiFi on the ESP.

It is also interesting to note that these issues only occurred in wireless areas with weak WiFi coverage.

The test machine has now been running stably for 5 weeks :D

So there must be some problem with weak WiFi and automatic channel change of FritzBox.
Maybe when the channel is changed between two connection attempts etc.?

Br.
Peff