Page 1 of 1

[SOLVED] lightsleep() causes WiFi failure

Posted: Mon May 10, 2021 12:38 am
by BetterAutomations
Please assist, this is driving me crazy. I'm on 1.15, using either ESP-WROOM-32 or a WiPy 2.0. After lightsleep() it sometimes works and sometimes does not, then usually it hangs. See the output below.

Please note that I am doing a ping after light sleep and if it fails, I do a wlan.connect(). No good.

Code: Select all

from machine import lightsleep
from machine import freq
from machine import Pin  # Only for WiPy2
from time import sleep_ms
import network
import uping
import config


TESTHOST = 'google.com'


def test_ping():
    try:
        result = uping.ping(
            host=TESTHOST,
            count=1,
            timeout=1000,
            quiet=True,)
        if result[1] >= 1:
            print('Connected')
            return True
        else:
            print('Not pinging')
            return False
    except OSError:  # TODO Specifically, -202
        print('Not connected')
        return None
    except:  # noqa FIXME What other exceptions?
        pass
        return None


# 80MHz, slowest possible which still allows Wi-Fi, saves power
freq(80000000)
antenna = Pin(16, Pin.OUT, value=0)  # Only for WiPy2
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(config.wifi.get('ssid'), config.wifi.get('password'))
while not wlan.isconnected():
    sleep_ms(100)
print('Network config:', wlan.ifconfig())
sleep_ms(2000)
while True:
    print('Zzz')
    lightsleep(10000)
    print('Wakey wakey')
    try:
        if test_ping():
            continue
        else:
            print('Reconnecting...')
            wlan.connect(config.wifi.get('ssid'), config.wifi.get('password'))
    except OSError:  # TODO And more specifically, Wifi Internal Error
        pass
    except:  # noqa
        pass
    while not wlan.isconnected():
        sleep_ms(100)
    print('Retrying test ping...')
    test_ping()
Output:

Code: Select all

ets Jun  8 2016 00:22:57

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
ets Jun  8 2016 00:22:57

rst:0x10 (RTCWDT_RTC_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:4
load:0x3fff0034,len:5624
load:0x40078000,len:12696
load:0x40080400,len:4292
entry 0x400806b0
MicroPython v1.15 on 2021-05-09; ESP32 module with ESP32
Type "help()" for more information.
>>> import testmain
Network config: ('192.168.137.39', '255.255.255.0', '192.168.137.1', '192.168.137.1')
Zzz
Wakey wakey
Connected
Zzz
Wakey wakey
Not pinging
Reconnecting...
Retrying test ping...
Not pinging
Zzz
Wakey wakey
Not pinging
Reconnecting...
E (23976) wifi:sta is connecting, return error
Hung at this point...

Re: lightsleep() causes WiFi failure

Posted: Mon May 17, 2021 2:44 am
by BetterAutomations
On ESP32-WROVER-IE with 8MB RAM and 8MB flash, I get this error repeatedly.

Code: Select all

E (39193) wifi:esf_buf: t=2 l=76 max:32, alloc:32 no eb, TXQ_BLOCK=0
W (39193) wifi:alloc eb len=76 type=2 fail, heap:41300

W (39193) wifi:m f probe req l=0

E (39313) wifi:esf_buf: t=2 l=76 max:32, alloc:32 no eb, TXQ_BLOCK=0
W (39313) wifi:alloc eb len=76 type=2 fail, heap:41300

W (39313) wifi:m f probe req l=0

E (39443) wifi:esf_buf: t=2 l=76 max:32, alloc:32 no eb, TXQ_BLOCK=0
W (39443) wifi:alloc eb len=76 type=2 fail, heap:41300

W (39443) wifi:m f probe req l=0

E (39563) wifi:esf_buf: t=2 l=76 max:32, alloc:32 no eb, TXQ_BLOCK=0
W (39563) wifi:alloc eb len=76 type=2 fail, heap:41300

W (39563) wifi:m f probe req l=0

[DEBUG] [LOOP] No wifi.ping(), trying wifi.connect() again
[DEBUG] Connecting to network...
E (40413) wifi:esf_buf: t=2 l=76 max:32, alloc:32 no eb, TXQ_BLOCK=0
W (40413) wifi:alloc eb len=76 type=2 fail, heap:41508

W (40413) wifi:m f probe req l=0

E (40533) wifi:esf_buf: t=2 l=76 max:32, alloc:32 no eb, TXQ_BLOCK=0
W (40533) wifi:alloc eb len=76 type=2 fail, heap:41560

W (40533) wifi:m f probe req l=0

E (40653) wifi:esf_buf: t=2 l=76 max:32, alloc:32 no eb, TXQ_BLOCK=4000
W (40663) wifi:alloc eb len=76 type=2 fail, heap:41560

W (40663) wifi:m f probe req l=0

E (40783) wifi:esf_buf: t=2 l=76 max:32, alloc:32 no eb, TXQ_BLOCK=4000
W (40783) wifi:alloc eb len=76 type=2 fail, heap:41560

W (40783) wifi:m f probe req l=0

E (40903) wifi:esf_buf: t=2 l=76 max:32, alloc:32 no eb, TXQ_BLOCK=4000
W (40903) wifi:alloc eb len=76 type=2 fail, heap:41560

W (40903) wifi:m f probe req l=0

E (41033) wifi:esf_buf: t=2 l=76 max:32, alloc:32 no eb, TXQ_BLOCK=4000
W (41033) wifi:alloc eb len=76 type=2 fail, heap:41560

W (41033) wifi:m f probe req l=0

E (41153) wifi:esf_buf: t=2 l=76 max:32, alloc:32 no eb, TXQ_BLOCK=0
W (41153) wifi:alloc eb len=76 type=2 fail, heap:41560

W (41153) wifi:m f probe req l=0

E (41283) wifi:esf_buf: t=2 l=76 max:32, alloc:32 no eb, TXQ_BLOCK=0

Re: lightsleep() causes WiFi failure

Posted: Mon May 17, 2021 2:43 pm
by karfas
No idea about the messages in your second posts.

A quick google search "esp32 light sleep wifi" brings me to https://www.esp32.com/viewtopic.php?t=15888 and from there to https://docs.espressif.com/projects/esp ... leep-modes .
There I find
In deep sleep and light sleep modes, wireless peripherals are powered down. Before entering deep sleep or light sleep modes, applications must disable WiFi and BT using appropriate calls (esp_bluedroid_disable(), esp_bt_controller_disable(), esp_wifi_stop()). WiFi and BT connections will not be maintained in deep sleep or light sleep, even if these functions are not called.

If WiFi connection needs to be maintained, enable WiFi modem sleep, and enable automatic light sleep feature (see Power Management APIs). This will allow the system to wake up from sleep automatically when required by WiFi driver, thereby maintaining connection to the AP.
Calls to the WIFI-power save functions are in the (not yet merged) patch requests
https://github.com/micropython/micropython/pull/6774
https://github.com/micropython/micropython/pull/5473

Re: lightsleep() causes WiFi failure

Posted: Mon May 17, 2021 3:43 pm
by BetterAutomations
Good catch, but in my code I am using wlan.connect() again after light sleep. It simply refuses to reconnect after light sleep. Try and see if it does the same for you. Three different ESP modules, all three do more-or-less the same thing.

Re: lightsleep() causes WiFi failure

Posted: Mon May 17, 2021 7:13 pm
by karfas
OK, tried this with a modified version of your code (my micropython has no "uping" out of the box).

The following works for me:

Code: Select all

from time import sleep_ms
import network

from config import ssid, password

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
while not wlan.isconnected():
    sleep_ms(100)
print('Network config:', wlan.ifconfig())
sleep_ms(2000)
while True:
    print('Zzz')
    wlan.active(False)
    lightsleep(2000)
    print('Wakey wakey')
    wlan.active(True)
    print('Retrying connect...')
    wlan.connect(ssid, password)
    while not wlan.isconnected():
        sleep_ms(100)
    print("connected 2")
Output:

Code: Select all

MicroPython v1.15-57-g121f11825 on 2021-05-07; T-Higrow BME280 with ESP32
Type "help()" for more information.
>>> import f
Network config: ('192.168.8.122', '255.255.255.0', '192.168.8.1', '192.168.8.1')
Zzz
Wakey wakey
Retrying connect...
connected 2
Zzz
Wakey wakey
Retrying connect...
connected 2
I think you should
a) set wlan.active(False) before entering light sleep
b) don't attempt to ping (when coming out of light sleep, this will not work anyway)

Regards,

Thomas

Re: lightsleep() causes WiFi failure

Posted: Tue May 18, 2021 11:56 am
by BetterAutomations
Wow, that works great. You are fantastic, don't let Damien tell you otherwise 8-) I guess the docs should be updated to say set wlan to inactive.

BTW uping does work coming out of light sleep. I obtained it here:
viewtopic.php?t=5287

Re: [SOLVED] lightsleep() causes WiFi failure

Posted: Tue May 18, 2021 8:04 pm
by karfas
All in the post above:
Before entering deep sleep or light sleep modes, applications must disable WiFi and BT using appropriate calls (... esp_wifi_stop()
Leaving Wifi enabled will also most likely leave the receiver/transmitter on, so minimizing the effect of light sleep.