← index #17339Issue #17621
Off-topic · high · value 0.232
QUERY · ISSUE

sta_if.status() unreliable

openby robtinkersopened 2025-05-22updated 2026-03-16
bugport-esp32

Port, board and/or hardware

esp32 port

MicroPython version

MicroPython v1.25.0 on 2025-04-15; Generic ESP32 module with ESP32

Reproduction

import network, time
from sys import print_exception

REAL_SSID = 'MyNetwork'
REAL_PASS = 'MyPassword'

NETWORKS = (('FakeNetwork1', '12345678'), (REAL_SSID, REAL_PASS), ('FakeNetwork2', '12345678'), ('FakeNetwork3', '12345678'))

STATUS_LOOKUP = {
    network.STAT_ASSOC_FAIL: 'ASSOC_FAIL',
    network.STAT_BEACON_TIMEOUT: 'BEACON_TIMEOUT',
    network.STAT_CONNECTING: 'CONNECTING',
    network.STAT_CONNECT_FAIL: 'CONNECT_FAIL',
    network.STAT_GOT_IP: 'GOT_IP',
    network.STAT_HANDSHAKE_TIMEOUT: 'HANDSHAKE_TIMEOUT',
    network.STAT_IDLE: 'IDLE',
    network.STAT_NO_AP_FOUND: 'NO_AP_FOUND',
    network.STAT_NO_AP_FOUND_IN_AUTHMODE_THRESHOLD: 'NO_AP_FOUND_IN_AUTHMODE_THRESHOLD',
    network.STAT_NO_AP_FOUND_IN_RSSI_THRESHOLD: 'NO_AP_FOUND_IN_RSSI_THRESHOLD',
    network.STAT_NO_AP_FOUND_W_COMPATIBLE_SECURITY: 'NO_AP_FOUND_W_COMPATIBLE_SECURITY',
    network.STAT_WRONG_PASSWORD: 'WRONG_PASSWORD',
}

def print_status_loop(interface, duration):
    old_status = None
    t0 = time.ticks_ms()
    while True:
        tt = time.ticks_diff(time.ticks_ms(), t0)
        if tt > duration * 1000:
            break
        status = interface.status()
        if old_status != status:
            old_status = status
            print(tt, STATUS_LOOKUP.get(status, str(status)))

sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)

for network in NETWORKS:
    print( '======== disconnect()')
    try: sta_if.disconnect()
    except Exception as e: print_exception(e)
    print_status_loop(sta_if, 5)
    print(f'======== connect({network[0]}, ...)')
    sta_if.connect(*network)
    print_status_loop(sta_if, 5)

Expected behaviour

I would expect the connect() call to reliably initialise the status to something (CONNECTING?)

I would expect the disconnect() call to reliably set the status to something (IDLE?)

Observed behaviour

connect() only works as I expect if the previous connect() call eventually GOT_IP, but not otherwise.

disconnect() almost works as I expect if the previous connect() call eventually GOT_IP, but not otherwise.

======== disconnect()
0 IDLE
======== connect(FakeNetwork1, ...)
0 CONNECTING
2417 NO_AP_FOUND
======== disconnect()
0 NO_AP_FOUND
======== connect(MyNetwork, ...)
0 NO_AP_FOUND
1398 GOT_IP
======== disconnect()
0 GOT_IP
1 IDLE
======== connect(FakeNetwork2, ...)
0 IDLE
2417 NO_AP_FOUND
======== disconnect()
0 NO_AP_FOUND
======== connect(FakeNetwork3, ...)
0 NO_AP_FOUND

Additional Information

I would like to loop through a bunch of pre-configured wifi networks to find one that works (e.g. for a portable device).

Unfortunately if a .connect() call doesn't eventually connect (the usual case in my scenario), then polling .status() becomes unreliable and I have to guess an appropriate timer and wait for .isconnected() for each ssid/pass, significantly slowing down startup if the working network is down the list.

Code of Conduct

Yes, I agree

CANDIDATE · ISSUE

Raspberry Pico W: Hangs after WLAN deinit()

closedby SmokeyBrandieopened 2025-07-06updated 2025-07-17
bugport-rp2

Port, board and/or hardware

pico w

MicroPython version

MicroPython v1.25.0 on 2025-04-15; Raspberry Pi Pico W with RP2040

Reproduction

import network
import time
from time import sleep
import urequests
import gc

WLAN_SSID = 'MYSSID'
WLAN_PASSWORD = '123'

wlan = network.WLAN(network.STA_IF)

counter = 1
while True:
    print('Start round {}'.format(counter))

    wlan.active(True)
    wlan.connect(WLAN_SSID, WLAN_PASSWORD)

    for i in range(20):
        if wlan.isconnected():
            break
        print('Trying to connect... Status is: ', wlan.status())
        time.sleep(1)

    if wlan.isconnected():
        response = urequests.get('https://www.google.com')
        response.close()

    print('Deactivate WLAN...')
    print('Memory alloc {}'.format(gc.mem_alloc()))
    print('Memory free {}'.format(gc.mem_free()))

    wlan.disconnect()
    wlan.active(False)
    wlan.deinit()

    print('Sleeping...')
    time.sleep(5)
    counter += 1

Expected behaviour

Should not hang

Observed behaviour

This script works approx. 10-30 times and then it suddenly hangs. It seems to be the
deinit() function which causes the freeze.

I tested several pico w hardware, but same problem.

Additional Information

No, I've provided everything above.

Code of Conduct

Yes, I agree

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied