WiFi dropouts unless using WiFi.setSleepMode(WIFI_NONE_SLEEP)
I've noticed lately that multiple devices I have have started dropping off WiFi and reconnecting (causing MQTT dropouts etc) unless I specify WiFi.setSleepMode(WIFI_NONE_SLEEP) when setting up the WiFi adapter.
ESP.getFullVersion() output:
SDK:3.0.0-dev(c0f7b44)/Core:2.5.0=20500000/lwIP:STABLE-2_1_2_RELEASE/glue:1.1/BearSSL:6778687
WiFi setup code in setup():
mConnectHandler = WiFi.onStationModeConnected(onConnected);
mDisConnectHandler = WiFi.onStationModeDisconnected(onDisconnect);
mGotIpHandler = WiFi.onStationModeGotIP(onGotIP);
WiFi.disconnect() ;
WiFi.persistent(false);
WiFi.setSleepMode(WIFI_NONE_SLEEP);
WiFi.mode(WIFI_STA); // Client mode
WiFi.setOutputPower(17); // 10dBm == 10mW, 14dBm = 25mW, 17dBm = 50mW, 20dBm = 100mW
WiFi.begin(ssid, password); // Start WiFi.
And the functions:
void onConnected(const WiFiEventStationModeConnected& event){
update_status += millis();
update_status += ": Connected to AP.";
update_status += "\n";
}
void onDisconnect(const WiFiEventStationModeDisconnected& event){
update_status += millis();
update_status += ": Station disconnected";
update_status += "\n";
client.disconnect();
}
void onGotIP(const WiFiEventStationModeGotIP& event){
update_status += millis();
update_status += ": Station connected, IP: ";
update_status += WiFi.localIP().toString();
update_status += "\n";
timer.setTimeout(1000, checkForUpdate);
}
update_status is a global string that is included on the web page interface.
If I comment out the setSleepMode line, I get regular wifi disconnections.
Issue moved across from https://github.com/platformio/platform-espressif8266/issues/135
Can you try that and report back wether it is sufficient without WiFi.setSleepMode(WIFI_NONE_SLEEP) (regarding power consumption) ?
https://github.com/esp8266/Arduino/issues/2330#issuecomment-455322676
I've implemented this as a SimpleTimer via:
void GratuitousARPTask() {
netif *n = netif_list;
while (n) {
etharp_gratuitous(n);
n = n->next;
}
}
SimpleTimer then triggers every ~5000ms for that task.
I can see the following on the network via tcpdump:
03:21:22.728282 ARP, Request who-has 10.1.1.11 tell 10.1.1.11, length 46
03:21:27.704425 ARP, Request who-has 10.1.1.11 tell 10.1.1.11, length 46
03:21:32.699405 ARP, Request who-has 10.1.1.11 tell 10.1.1.11, length 46
03:21:37.726173 ARP, Request who-has 10.1.1.11 tell 10.1.1.11, length 46
03:21:42.708973 ARP, Request who-has 10.1.1.11 tell 10.1.1.11, length 46
03:21:47.688705 ARP, Request who-has 10.1.1.11 tell 10.1.1.11, length 46
03:21:52.726033 ARP, Request who-has 10.1.1.11 tell 10.1.1.11, length 46
I've also disabled the WiFi.setSleepMode(WIFI_NONE_SLEEP);, so we'll see what happens.
Setting WIFI_LIGHT_SLEEP and sending GARP's - and it doesn't seem to fix the issue. I don't have any of the ESP8266 units with over an hour uptime according to the AP.
I even tried setting one to 802.11g mode, no real difference.
Have you tried with latest git which uses a more stable espressif firmware ?
Embarrassingly, I can't quite remember what to feed into PlatformIO to try this.
Currently, I have the following in platformio.ini:
platform = espressif8266
;platform = https://github.com/platformio/platform-espressif8266.git
;platform = https://github.com/platformio/platform-espressif8266.git#feature/stage
I'm pretty sure none of these are directly from this repo. I'm pretty sure I've done this before - but I can't find my notes that mention it. Any hints on being able to get this to reflect here instead?
This entry use latest ESP8266 dev version: platform = https://github.com/platformio/platform-espressif8266.git#feature/stage
This entry use latest ESP8266 dev version:
platform = https://github.com/platformio/platform-espressif8266.git#feature/stage
That doesn't map up with whats in this repo though....
From https://github.com/platformio/platform-espressif8266/tree/feature/stage (in your browser): Latest commit 645d2d9 14 days ago
The commit list here has: Latest commit cdb5495 2 days ago
I'm not sure the two compare...
You have to force platformio to update!
Ah! That's what I was missing....
Platform Espressif 8266 (Stage)
--------
Updating espressif8266 @ 645d2d9 [Up-to-date]
Updating framework-arduinoespressif8266 @ cdb5495 [Up-to-date]
Updating tool-esptool @ 1.413.0 [Up-to-date]
Updating toolchain-xtensa @ 1.40802.0 [Up-to-date]
Let me start testing again.
Woah, this seems to lead to hard hangs if I set WiFi.setSleepMode(WIFI_LIGHT_SLEEP);
I hit the reset button to bring it back to life after each of the disconnection timeouts below. Setting WiFi.setSleepMode(WIFI_NONE_SLEEP); seems to restore functionality with no hangs.
Version string is now:
SDK:2.2.1(cfd48f3)/Core:2.6.0-dev=20600000/lwIP:STABLE-2_1_2_RELEASE/glue:1.1-7-g82abda3/BearSSL:a143020
MQTT logs for timing:
Apr 28 23:11:33 mosquitto[538]: 1556457093: New connection from 10.1.1.11 on port 1883.
Apr 28 23:11:33 mosquitto[538]: 1556457093: New client connected from 10.1.1.11 as 84:F3:EB:10:75:2C (c1, k15).
Apr 28 23:12:08 mosquitto[538]: 1556457128: Client 84:F3:EB:10:75:2C has exceeded timeout, disconnecting.
Apr 28 23:12:08 mosquitto[538]: 1556457128: Socket error on client 84:F3:EB:10:75:2C, disconnecting.
Apr 28 23:13:28 mosquitto[538]: 1556457208: New connection from 10.1.1.11 on port 1883.
Apr 28 23:13:28 mosquitto[538]: 1556457208: New client connected from 10.1.1.11 as 84:F3:EB:10:75:2C (c1, k15).
Apr 28 23:13:52 mosquitto[538]: 1556457232: Client 84:F3:EB:10:75:2C has exceeded timeout, disconnecting.
Apr 28 23:13:52 mosquitto[538]: 1556457232: Socket error on client 84:F3:EB:10:75:2C, disconnecting.
Make sure you do a clean build and build again after updating the core library, when using PlatformIO. Most of the time it works just fine, but I know from experience that it may cost a lot of hours debugging to find not reproducible error reports, which seem to be fixed after a clean build.
Apart from that, I've seen similar issues. Gratuitous ARP does seem to help a bit, but it isn't the fix for all.
What I'm seeing is that the node does reduce power somehow (you can measure its power consumption) and then starts to miss packets until it receives some ping requests (or other repeated network requests) To answer these requests, the current usage increases for a while and all requests are answered well.
Even when setting the wifi to none sleep, the power consumption may drop after a while and then it starts to miss packets. Strange thing is, it will never miss ICMP packets. It may take up-to a few-100 msec to reply the first one, but all ping packets will get a reply.
Now the really strange part.... Some builds run fine for weeks and some can hardly stay up for a few hours to days. So it somewhat looks to me like some uninitialized variable or some array being addressed out of bound, or an alignment issue. Behavior of these can all be different by some changes in totally unrelated code.
When do you call the function Gratuitous ARP, during the loop correct?
@TD-er How can we use Gratuitous ARP. Is there any temporay solution to this problem. I went through few threads related to this on this repo as well as EspEasy's repo, i saw that you have worked on it a lot. Did you find some temporary or permanent solution to this problem ?
I do send Gratuitous ARP packets when just connected and when I encounter failed connect attempts (also DNS lookup) And also periodically I do send them, when it is enabled in the settings.
Another approach can be to send ping packets to the node, since ICMP packets are somehow treated differently. Not sure if it is only on the ESP, or also in the AP what makes it act different. You can see its power consumption remain constant (high) when it is receiving pings, but also its reaction time improves and that must be something in the AP I guess.
I am not (yet) using it on the ESP32 and you can clearly see a difference in how well it does react on web requests or the UDP packets sent among ESPeasy nodes. The ESP32 does only receive a handful of them and thus lists only a fraction of the ESPeasy nodes in the network. Some listed nodes have a "last seen" timestamp of 5 - 10 minutes, while each node does send its presence every minute.
I fixed it by doing: WiFi.setSleepMode(WIFI_NONE_SLEEP);
Without that, it still seems to drop out. Of course, you need to be able to tolerate the extra power draw...
Not tested on ESP32, but on an ESP8266 I've notice this is no guarantee it will keep the wifi radio on. I still noticed the power consumption to drop after some time (10+ minutes) and as soon as the power dropped, the packets were missing again.
There is something in the background messing with the power settings.
@TD-er Thank you for this. I checked usage of sendGratuitousARP in espeasy repo. And i implemented the same in my code.
void sendGratuitousARP() {
if (!wifiConnected()) {
return;
}
unsigned long start = millis();
netif *n = netif_list;
while (n) {
etharp_gratuitous(n);
n = n->next;
}
DEBUG_MSG_P(PSTR("Sent Gratuitous ARP : Time Took - %d\n"), millis() - start);
}
And i am calling this function as soon as node connects to the wifi. And after that calling it every 5 seconds. However, there is something wrong with this. Device is crashing sometimes when calling this function right after getting connected with wifi. (I tried giving a delay of 100ms also, not helping). I am using Lwip 2 higher bandwidth version. Are we not supposed to call this right after connecting with WiFi.
This is my exception stack
Exception 0: Illegal instruction
PC: 0x2d2d2d2d
EXCVADDR: 0x00000000
Decoding stack results
0x40100c84: malloc(size_t) at /Users/arihantdaga/Documents/Arduino/hardware/esp8266com/esp8266/cores/esp8266/umm_malloc/umm_malloc.cpp line 1677
0x40237183: new_linkoutput at glue-lwip/lwip-git.c line 235
0x40237574: ethernet_output at netif/ethernet.c line 312
0x4023ec0f: etharp_raw at core/ipv4/etharp.c line 1161
0x4023ee0a: etharp_request at core/ipv4/etharp.c line 1202
0x402042f0: wifiConnected() at /Users/arihantdaga/Documents/Arduino/KIOT_NEW/ESP_FRAMEWORK/kiot/kiot/wifi.ino line 61
0x402046eb: sendGratuitousARP() at /Users/arihantdaga/Documents/Arduino/KIOT_NEW/ESP_FRAMEWORK/kiot/kiot/wifi.ino line 571
0x40205f4e: _wiFiStatusCallback(justwifi_messages_t, char*) at /Users/arihantdaga/Documents/Arduino/KIOT_NEW/ESP_FRAMEWORK/kiot/kiot/wifi.ino line 423
0x4022a666: std::_Function_handler ::_M_invoke(std::_Any_data const&, justwifi_messages_t, char*) at /Users/arihantdaga/Documents/Arduino/hardware/esp8266com/esp8266/tools/xtensa-lx106-elf/xtensa-lx106-elf/include/c++/4.8.2/functional line 2073
0x40100522: millis() at /Users/arihantdaga/Documents/Arduino/hardware/esp8266com/esp8266/cores/esp8266/core_esp8266_wiring.cpp line 186
0x402187a5: JustWifi::_doCallback(justwifi_messages_t, char*) at /Users/arihantdaga/Documents/Arduino/libraries/justwifi_ModifiedByArihant/src/JustWifi.cpp line 418
0x40214298: ESP8266WiFiSTAClass::setAutoConnect(bool) at /Users/arihantdaga/Documents/Arduino/hardware/esp8266com/esp8266/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp line 413
0x402189b4: JustWifi::_connect(unsigned char) at /Users/arihantdaga/Documents/Arduino/libraries/justwifi_ModifiedByArihant/src/JustWifi.cpp line 111
0x4021901c: JustWifi::connected() at /Users/arihantdaga/Documents/Arduino/libraries/justwifi_ModifiedByArihant/src/JustWifi.cpp line 603
0x40201e55: loop() at /Users/arihantdaga/Documents/Arduino/KIOT_NEW/ESP_FRAMEWORK/kiot/kiot/kiot.ino line 139
0x402299de: uart_read(uart_t*, char*, size_t) at /Users/arihantdaga/Documents/Arduino/hardware/esp8266com/esp8266/cores/esp8266/uart.cpp line 274
0x402299d4: uart_read(uart_t*, char*, size_t) at /Users/arihantdaga/Documents/Arduino/hardware/esp8266com/esp8266/cores/esp8266/uart.cpp line 249
0x40218d44: JustWifi::_startSTA(bool) at /Users/arihantdaga/Documents/Arduino/libraries/justwifi_ModifiedByArihant/src/JustWifi.cpp line 359
0x402190b1: JustWifi::loop() at /Users/arihantdaga/Documents/Arduino/libraries/justwifi_ModifiedByArihant/src/JustWifi.cpp line 657
0x40100522: millis() at /Users/arihantdaga/Documents/Arduino/hardware/esp8266com/esp8266/cores/esp8266/core_esp8266_wiring.cpp line 186
0x4020471f: wifiLoop() at /Users/arihantdaga/Documents/Arduino/KIOT_NEW/ESP_FRAMEWORK/kiot/kiot/wifi.ino line 544
0x40201e58: loop() at /Users/arihantdaga/Documents/Arduino/KIOT_NEW/ESP_FRAMEWORK/kiot/kiot/kiot.ino line 138
0x40228a64: loop_wrapper() at /Users/arihantdaga/Documents/Arduino/hardware/esp8266com/esp8266/cores/esp8266/core_esp8266_main.cpp line 132
Yep, you have to wait a while after setup of the WiFi. See also this issue: https://github.com/esp8266/Arduino/issues/6266 Currently I'm changing a lot in the ESPeasy code regarding WiFi, because a lot of checks and local patches crept in the code the past year to find the issues with the WiFi instability.
But in general, if you observe the rules I mention in the last post I linked, you should be fine. Also add some delays after connecting. In ESPeasy, the state of WiFi is followed by looking at the events. If you don't want to do that, just make sure you have both a connection and an IP address before sending data.
@TD-er I tried delay(200) also. And i am getting IP also, after that only i am starting sendGratuitousARP(). I checked the ESPEasy's code again after your recent updates. This is triggering hardware watchdog reset.
void sendGratuitousARP() {
if (!wifiConnected()) {
return;
}
unsigned long start = millis();
netif *n = netif_list;
while (n) {
etharp_gratuitous(n);
n = n->next;
}
DEBUG_MSG_P(PSTR("Sent Gratuitous ARP : Time Took - %d\n"), millis() - start);
}
[011561] [WIFI] Connected Wifi
[011561] [WIFI] MODE STA -------------------------------------
[011562] [WIFI] SSID D-Link_DIR-600M
[011562] [WIFI] IP 192.168.100.2
[011565] [WIFI] MAC 80:7D:3A:73:E1:F5
[011568] [WIFI] GW 192.168.100.1
[011571] [WIFI] DNS 192.168.100.1
[011574] [WIFI] MASK 255.255.255.0
[011577] [WIFI] HOST sw_1_rel_807d3a73e1f
[011581] [WIFI] ----------------------------------------------
[011687] Point 1
---------------------------------->> This is where it it'll trigger Hardware Watchdog. << ----------------------------------
ets Jan 8 2013,rst cause:4, boot mode:(3,6)
wdt reset
load 0x4010f000, len 1384, room 16
tail 8
chksum 0x2d
csum 0x2d
v0937b076
~ld
I have tried lwip 2 lower memory also, using latest esp core from github. But its same, now its triggering hardware watch dog. I am not sure if its working with ESPEasy. I'll try and verify that also today.
EDIT: Calling sendGratuitousARP() function is triggering wdt reset.