Non-blocking WiFi connection

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
PLUB
Posts: 3
Joined: Fri Jan 08, 2021 1:06 pm

Non-blocking WiFi connection

Post by PLUB » Fri Jan 08, 2021 6:16 pm

Hi,
I don't speak perfect English and I use the Google translator ;) , sorry if the syntax is not perfect.
I had fun with an Arduino with C ++ and now I want to use an ESP32 in Micropython, which allows Wifi connections.
Most tutorials use a blocking loop to connect like:
in C:
While (WIFI.Status ()! = WL_CONNECTED) {
delay (500);
Serial.print (".")}

or in Python:
if not wlan.isconnected ():
print ('connecting to network ...')
wlan.connect ('essid', 'password')
while not wlan.isconnected ():
pass
print ('network config:', wlan.ifconfig ())
If the connection cannot be established, the ESP loops indefinitely and is lost.
However, I saw other Arduino tutorials that used WifiEventHandler.
Example:
WiFiEventHandler gotIpEventHandler, disconnectedEventHandler;

void setup ()
{
Serial.begin (115200);
Serial.println ();

gotIpEventHandler = WiFi.onStationModeGotIP ([] (const WiFiEventStationModeGotIP & event)
{
Serial.print ("Station connected, IP:");
Serial.println (WiFi.localIP ());
});

disconnectedEventHandler = WiFi.onStationModeDisconnected ([] (const WiFiEventStationModeDisconnected & event)
{
Serial.println ("Station disconnected");
});

Serial.printf ("Connecting to% s ... \ n", ssid);
WiFi.begin (ssid, password);
}
I didn't find anything in Micropython. Is it possible to have an equivalent solution?
Thank you in advance

User avatar
pythoncoder
Posts: 4792
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Non-blocking WiFi connection

Post by pythoncoder » Fri Jan 08, 2021 6:22 pm

I'd implement a timeout. Replace

Code: Select all

while not wlan.isconnected ():
    pass
with something like

Code: Select all

from time import ticks_ms, ticks_diff

start = ticks_ms()
while not wlan.isconnected ():
    if ticks_diff(ticks_ms(), start) > 10_000:  # 10 second timeout
        # It hasn't connected, take some kind of action
Peter Hinch

PLUB
Posts: 3
Joined: Fri Jan 08, 2021 1:06 pm

Re: Non-blocking WiFi connection

Post by PLUB » Sat Jan 09, 2021 10:09 am

Thank you for your reply. I have already seen this solution, but during these 10s the ESP is blocked, and that whatever the delay.
If I understand correctly, the WifiEventHandler works like interrupts, which means non-blocking.
Is it correct?

User avatar
pythoncoder
Posts: 4792
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Non-blocking WiFi connection

Post by pythoncoder » Sat Jan 09, 2021 6:17 pm

I'm not sure what you mean when you say the ESP is blocked. This loop

Code: Select all

while not wlan.isconnected ():
    if ticks_diff(ticks_ms(), start) > 10_000:  # 10 second timeout
        # It hasn't connected, take some kind of action
is running: the isconnected() call is nonblocking. If you want to run concurrent tasks while waiting for a connection you need to use uasyncio. See the docs and this tutorial.

It is entirely possible to write asynchronous WiFi code, the one exception being socket.getaddrinfo(): DNS lookup currently blocks for the duration. Fixing this is work in progress.

For an example of an asynchronous WiFi application see resilient asynchronous MQTT.
Peter Hinch

PLUB
Posts: 3
Joined: Fri Jan 08, 2021 1:06 pm

Re: Non-blocking WiFi connection

Post by PLUB » Sat Jan 09, 2021 7:41 pm

Thank you for this information.
I will read them and try to assimilate them.
Plub

Post Reply