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
Non-blocking WiFi connection
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Non-blocking WiFi connection
I'd implement a timeout. Replace
with something like
Code: Select all
while not wlan.isconnected ():
pass
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
Index to my micropython libraries.
Index to my micropython libraries.
Re: Non-blocking WiFi connection
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?
If I understand correctly, the WifiEventHandler works like interrupts, which means non-blocking.
Is it correct?
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Non-blocking WiFi connection
I'm not sure what you mean when you say the ESP is blocked. This loop
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.
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
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
Index to my micropython libraries.
Index to my micropython libraries.
Re: Non-blocking WiFi connection
Thank you for this information.
I will read them and try to assimilate them.
Plub
I will read them and try to assimilate them.
Plub
Re: Non-blocking WiFi connection
you can also use a timer. then place "isconnected()" in the timer callback function. I created a software timer so I don't consume hardware ones everytime I have a function that has to wait for some operation.