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

821 Views Asked by At

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
>>> 
1

There are 1 best solutions below

0
stdunbar On

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.