B4R Question ESP8266 Radio seems to sleep after a few minutes, how to change inactivity period

miker2069

Active Member
Licensed User
Longtime User
More of an indirect B4R question. I notice that after about 2 minutes or so of inactivity, the ESP8266 wifi radio seems to go to "sleep". I noticed this with a simple ping test (10.0.0.22 is my ESP8266):

ping 10.0.0.22

Pinging 10.0.0.22 with 32 bytes of data:
Request timed out.
Request timed out.
Reply from 10.0.0.22: bytes=32 time=71ms TTL=128
Reply from 10.0.0.22: bytes=32 time=3ms TTL=128

The first few attempts always time out then then it wakes up on the 3rd and subsequent attempts. I can see this in code when connecting to the ESP8266 as it takes several seconds for the initial connection. Subsequent connections (if made within a 2 minute window) are fast.

Obviously this makes some sense as a power savings technique and my app can tolerate a "wake up" period. I was just wondering if there was a way to turn this off or increase the time before the radio goes to sleep. I've tried googling just general ESP8266 discussions on this topic to no avail.

Any help would be appreciated.
 

miker2069

Active Member
Licensed User
Longtime User
Try to call with inline code:
WiFi.forceSleepWake();

Use a timer to call it every few seconds.
Example:
https://www.b4x.com/android/forum/threads/esp8266-wifi-and-socket.69164/#post-438940

I haven't tested it.
Thanks for the suggestion - I tried that, didn't work (actually had opposite effect). I'll keep researching. I should add my board is NodeMCU 12e. I'm assuming the behavior is the same. It's almost exactly 2 minute where the wifi radio seems to go into a sleep mode. It does wake up after a connection attempt (which takes anywhere from 6-20 seconds). I've confirmed the CPU is still activate during this period as I used a timer to print out to the log every few seconds.

I'll keep researching...thanks!
 
Upvote 0

miker2069

Active Member
Licensed User
Longtime User
To add some info to this topic, I also add the rMQTT library to my project (so now I'm using the WIFI server and and setup a simple connection to cloudmqtt.com). The ESP8266 does the same thing in that after 2 minutes the radio "seems to go to sleep". If I try a ping - no response. Now what I noticed is if I broadcast on a subscribed MQTT topic, the ESP8266 sees it near instantly (and the ESP8266 can respond with a publish back to the broker just as fast). If I try the ping test right after that, the pings respond immediately. Interesting (i.e. strange) behavior. The WiFiServerSocket seems to take a while to establish a connection after an idle greater than 2 min, while the WiFiSocket used by MQTT communicate near real-time no matter how long it's been since the ESP8266 has sent/received data. Note as I mentioned originally this isn't a B4R issue and more to do with the underlying ESP8266 libraries and how the h/w is getting setup.

My thought is that the MQTT client code uses a persistent connection to the broker and since it never seems to break after 2 minutes, this rules out the WIFI radio being turned off ( as I once thought)

Just posting if anyone else encounters this behavior.
 
Upvote 0
Top