Shaair
Shaair

Reputation: 19

While running wlan.status() command on esp32 it is giving value of 1010 instead of 1-5

While running this code on my esp32 device:

import utime
import network

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect("Dream Net R-632", "07132711")


max_wait = 10
while max_wait > 0:
    """
        0   STAT_IDLE -- no connection and no activity,
        1   STAT_CONNECTING -- connecting in progress,
        -3  STAT_WRONG_PASSWORD -- failed due to incorrect password,
        -2  STAT_NO_AP_FOUND -- failed because no access point replied,
        -1  STAT_CONNECT_FAIL -- failed due to other problems,
        3   STAT_GOT_IP -- connection successful.
    """
    if wlan.status() < 0 or wlan.status() >= 3:
        break
    max_wait -= 1
    print('waiting for connection... ' + str(max_wait))
    utime.sleep(1)


if wlan.status() != 3:
    raise RuntimeError('network connection failed')
else:
    print('wlan connected')
    status = wlan.ifconfig()
    print('IP address = ' + status[0])
    print('subnet mask = ' + status[1])
    print('gateway  = ' + status[2])
    print('DNS server = ' + status[3])

I had run this code on my esp32 device through Thonny ide but always getting the error that: "RuntimeError: network connection failed". Tried running:

wlan.isconnected()

and it's returning me True meaning that wifi is connected. But when I check wlan.status() instead of getting value between 1-5 it's giving me 1010 value which is causing RunTimeError.

I had checked my ssid and pin. Everything is fine except this .status() thing that is returning to 1010.

>>> wlan.active(True)
True
>>> wlan.status()
1010
>>> 

Upvotes: 0

Views: 1702

Answers (2)

Yanai Zaicik
Yanai Zaicik

Reputation: 1

Your code was written for a Raspberry Pico, which doesn't use the same list of status or codes as the ESP32

You can get the full list and corresponding codes for your device by typing help(network) in the shell, after having loaded the network library:

>>> import network   # if you haven't already loaded it
>>> help(network)

  # results truncated for clarity
  STAT_IDLE -- 1000
  STAT_CONNECTING -- 1001
  STAT_GOT_IP -- 1010
  STAT_NO_AP_FOUND -- 201
  STAT_WRONG_PASSWORD -- 202
  STAT_BEACON_TIMEOUT -- 200
  STAT_ASSOC_FAIL -- 203
  STAT_HANDSHAKE_TIMEOUT -- 204

If you try the same on a Raspberry Pi, you'll get a different result:

>>> import network
>>> help(network)

  # results also truncated for clarity
  STAT_IDLE -- 0
  STAT_CONNECTING -- 1
  STAT_WRONG_PASSWORD -- -3
  STAT_NO_AP_FOUND -- -2
  STAT_CONNECT_FAIL -- -1
  STAT_GOT_IP -- 3

To be honest, this appears to be quite poorly documented and took me a while to figure out, since I'm pretty new at this. I think the logic behind

For this particular code, a quick fix for ESP32 would be to replace the lines

    if wlan.status() < 0 or wlan.status() >= 3
# ... (truncated) ...
if wlan.status() != 3:

with

    if wlan.status() < 1000 or wlan.status() >= 1010
# ... (truncated) ...
if wlan.status() != 1010:

Readers might also pay attention to the fact that the ESP32 (at least the ESP32-C3) has a STAT_ASSOC_FAIL status while the RPi uses STAT_CONNECT_FAIL

Upvotes: 0

stdunbar
stdunbar

Reputation: 17535

According to this documentation the statuses are:

WLAN.status()

Returns the current status of the wireless connection.

   STAT_IDLE – no connection, no activities-1000
   STAT_CONNECTING – Connecting-1001
   STAT_WRONG_PASSWORD – Failed due to password error-202
   STAT_NO_AP_FOUND – Failed, because there is no access point reply,201
   STAT_GOT_IP – Connected-1010
   STAT_ASSOC_FAIL – 203
   STAT_BEACON_TIMEOUT – Timeout-200
   STAT_HANDSHAKE_TIMEOUT – Handshake timeout-204

So it looks like your result is expected.

Upvotes: 1

Related Questions