Properly checking for errors during a WiFi connection?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
User avatar
Hyperion
Posts: 13
Joined: Wed Oct 16, 2019 9:34 am

Properly checking for errors during a WiFi connection?

Post by Hyperion » Sun Oct 20, 2019 2:47 pm

Hello, MicroPython forums! I'm trying to connect to a network on a ESP32 while telling the status and the reason for an error, if it occurs. Since there is no WLAN irq for the ESP32 port, I thought that initializing a timer when connecting, for checking the status every half a second and counting it up to a given timeout, is quite adequate. Or is there any more recommended way to do that? Also, it seems to stay for well over 10 seconds in the 1001 status (connecting) and never goes to the error status messages. So how do I find out when it resulted in an incorrect password or any other error?

I cannot find any propper documentation for the WLAN.status() numbers. The ones I found so far are:
  • 200: BEACON_TIMEOUT
  • 201: NO_AP_FOUND
  • 202: WRONG_PASSWORD
  • 203: ASSOC_FAIL
  • 204: HANDSHAKE_TIMEOUT
  • 1000: IDLE
  • 1001: CONNECTING
  • 1010: GOT_IP
I have constructed this list from the STAT_ variables that appear when I use tab on the network module, in REPL. But it appears to be incomplete, as sometimes I get to status 205, 8 or 15 after issuing the disconnect() command while I'm still connecting. Are there even more and what do they mean?

Lastly, when I worked with AT firmware on the ESP32 before trying out MicroPython, I could get a response when the module was connecting to a network, and another when it had already connected and was getting an IP. MicroPython seems to skip the "getting IP" step and only go from 1001 (connecting) immediately after issuing the connect command to 1010 (connected and got IP). Is it just incomplete so far or is there another way of checking for that?

TL;DR:
  1. How to properly retrieve the error status of a connection (wrong password or timeout) or how to access the WLAN irq on ESP32?
  2. What are all the WLAN.status() codes?
  3. Can I check for when the module is connected but still getting the IP, before the connection is fully established?

User avatar
Hyperion
Posts: 13
Joined: Wed Oct 16, 2019 9:34 am

Re: Properly checking for errors during a WiFi connection?

Post by Hyperion » Tue Oct 29, 2019 9:19 am

I still haven't found a better solution than using a timer and disconnecting it after a certain time. Does noone have any WiFi connection monitoring experience with the ESP32?

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

Re: Properly checking for errors during a WiFi connection?

Post by pythoncoder » Wed Oct 30, 2019 10:53 am

With Kevin Köck (@kevinkk525) I developed two libraries which support "resilient" WiFi connections, asynchronous MQTT and micropython-iot. You might like to look at the code to see how to initiate and reliably maintain a WiFi connection. Note that these libraries are cross platform; they will run on ESP32.
Peter Hinch

kevinkk525
Posts: 769
Joined: Sat Feb 03, 2018 7:02 pm

Re: Properly checking for errors during a WiFi connection?

Post by kevinkk525 » Wed Oct 30, 2019 11:04 am

I should add that neither of those libraries exposes why the wifi connection fails. The libraries just try to connect with the given credentials and tries to stay connected.
Should you have provided wrong credentials then the library will just not be able to connect. The mqtt_client will raise an Exception if the first connect to wifi fails but still the user has to figure out himself why the wifi connection is not successful.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

User avatar
Hyperion
Posts: 13
Joined: Wed Oct 16, 2019 9:34 am

Re: Properly checking for errors during a WiFi connection?

Post by Hyperion » Mon Nov 04, 2019 9:09 am

If there's currently no way of knowing what caused the connection error, can it at least be implemented?

And as for the interrupt, that's out of luck aswell?

kevinkk525
Posts: 769
Joined: Sat Feb 03, 2018 7:02 pm

Re: Properly checking for errors during a WiFi connection?

Post by kevinkk525 » Mon Nov 04, 2019 10:22 am

If the user wants to know why the initial wifi connection fails, he can check it himself. The mqtt_as library will raise an Exception on "client.connect()" if the wifi connection can't be established.
Therefore there is no need for an interrupt.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

User avatar
Hyperion
Posts: 13
Joined: Wed Oct 16, 2019 9:34 am

Re: Properly checking for errors during a WiFi connection?

Post by Hyperion » Mon Nov 04, 2019 7:40 pm

Problem is I want to raise it automatically the exact moment the connection has caused an error. And I want to avoid using libraries because according to the filesystem commands I have only 2MB of available space, at least a half of which I know I'll need for certain data.

By the way, any idea what the other codes are for?

tjlynchny
Posts: 2
Joined: Fri Jan 24, 2020 4:32 pm

Re: Properly checking for errors during a WiFi connection?

Post by tjlynchny » Thu Jan 30, 2020 7:25 pm

If the user wants to know why the initial wifi connection fails, he can check it himself.
Well, yes, I do want to know why the initial connection fails. I tried checking like this:

Code: Select all

import network
nic = network.WLAN(network.STA_IF)
nic.active(True)
nic.connect('my-ssid', 'my-password')

s=nic.status()
if s == STAT_NO_AP_FOUND:
    print("no AP found")
if s == STAT_WRONG_PASSWORD:
    print("wrong password")
. . .
I've tried checking for all six STAT_ constants as listed in the docs ( https://docs.micropython.org/en/latest/ ... .WLAN.html ) but, in fact, they are not defined. At least, I'm seeing an error that says they are not defined.

What am I missing?

My AP is working, got the proper SSID and password, using 2.4GHz ...

Christian Walther
Posts: 121
Joined: Fri Aug 19, 2016 11:55 am

Re: Properly checking for errors during a WiFi connection?

Post by Christian Walther » Thu Jan 30, 2020 9:39 pm

They are in the network module, not in the global environment: network.STAT_CONNECTING etc. See help(network).

tjlynchny
Posts: 2
Joined: Fri Jan 24, 2020 4:32 pm

Re: Properly checking for errors during a WiFi connection?

Post by tjlynchny » Fri Jan 31, 2020 2:22 pm

They are in the network module, not in the global environment
In the sample code I posted, the first line is

Code: Select all

import network
, and I'm still seeing error messages telling me the constants are undefined.

Looking at source, I don't see any place where, for example,

Code: Select all

STAT_NO_AP_FOUND
is defined.

Still don't see what I'm doing wrong.

Post Reply