← index #6787Issue #12930
Off-topic · high · value 1.907
QUERY · ISSUE

stm32: network WLAN(AP_IF).isconnected() always returns True

openby fragmuffinopened 2021-01-19updated 2026-03-25
port-stm32

The documentation for network.WLAN.isconnected() states:

In AP mode returns True when a station is connected. Returns False otherwise.

ref: http://docs.micropython.org/en/v1.13/library/network.WLAN.html#network.WLAN.isconnected

However, when I run the following code on a PYBD_SF6W:

import utime as time
import network
from urandom import randint
import machine

# Access Point
def make_ap():
    print("Access Point...")
    ap = network.WLAN(network.AP_IF)
    ap.config(essid='MyPyboard:{:04d}'.format(randint(1, 10000)))
    ap.active(True)

    while not ap.active():
        time.sleep(0.1)

    print("  AP_IF: {!r}".format(ap.ifconfig()))
    return ap

# ----- Mainline
print('machine.reset_cause()', machine.reset_cause())
ap = make_ap()  # network.WLAN in AP_IF mode
while True:
    time.sleep(1)
    print('isconnected()', ap.isconnected())

I get the output:

MicroPython v1.13-274-g49dd9ba1a on 2021-01-19; PYBD-SF6W with STM32F767IIK
Type "help()" for more information.
>>>
paste mode; Ctrl-C to cancel, Ctrl-D to finish
=== # pasted above code
machine.reset_cause() 1
Access Point...
  AP_IF: ('192.168.4.1', '255.255.255.0', '192.168.4.1', '0.0.0.0')
isconnected() True
isconnected() True
isconnected() True
DHCPS: client connected: MAC=66:bc:1c:1a:af:f1 IP=192.168.4.16
isconnected() True
isconnected() True
isconnected() True

What I would expect is that before the DHCP connection is made, before any stations are connected to the AP, isconnected() should return False.

CANDIDATE · ISSUE

esp32: When network.STAT_NO_AP_FOUND and network.STAT_WRONG_PASSWORD occurred?

closedby IhorNehrutsaopened 2023-11-09updated 2023-11-23
bug

boot.py is:

print('\nboot.py')

from machine import reset

import network
wlan = network.WLAN(network.STA_IF)

def do_connect(ssid, password):
    wlan_status = wlan.status()
    wlan.active(True)
    if not wlan.isconnected():
        print('connecting to network...')
        print('wlan:', wlan.ifconfig(), wlan.isconnected(), wlan.status())
        wlan.connect(ssid, password)
        while not wlan.isconnected():
            if wlan_status != wlan.status():
                print('wlan status', wlan_status, 'changed to', wlan.status())
                wlan_status = wlan.status()
    print('wlan:', wlan.ifconfig(), wlan.isconnected(), wlan.status())
>>> reset()
ets Jul 29 2019 12:21:46

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:4728
load:0x40078000,len:14876
ho 0 tail 12 room 4
load:0x40080400,len:3368
entry 0x400805cc

boot.py
MicroPython-1.21.0-xtensa-IDFv5.0.2-with-newlib4.1.0
('newlib', '4.1.0')
GCC 11.2.0
MicroPython v1.21.0 on 2023-10-05; Generic ESP32 module with ESP32
Type "help()" for more information.

When the ssid and password are correct, the connection is established.

>>> do_connect('right_ssid', 'right_password')
connecting to network...
wlan: ('0.0.0.0', '0.0.0.0', '0.0.0.0', '0.0.0.0') False 1000
wlan status 1000 changed to 1001
wlan: ('192.168.3.230', '255.255.255.0', '192.168.3.1', '8.8.8.8') True 1010

If the ssid is wrong, the connection is not established.
I expected wlan.status() to return network.STAT_NO_AP_FOUND, but it doesn't.

>>> do_connect('wrong_ssid', 'password')
connecting to network...
wlan: ('0.0.0.0', '0.0.0.0', '0.0.0.0', '0.0.0.0') False 1000
wlan status 1000 changed to 1001

If the ssid is correct and password is wrong, the connection is not established.
I expected wlan.status() to return network.STAT_WRONG_PASSWORD, but it doesn't.
E (41882) wifi:Set status to INIT reported infinity times.

>>> do_connect('right_ssid', wrong_password')
connecting to network...
wlan: ('0.0.0.0', '0.0.0.0', '0.0.0.0', '0.0.0.0') False 1000
wlan status 1000 changed to 1001
E (41882) wifi:Set status to INIT
E (44322) wifi:Set status to INIT
E (52192) wifi:Set status to INIT
E (54632) wifi:Set status to INIT
...

for reference:

>>> network.STAT_IDLE
1000
>>> network.STAT_CONNECTING
1001
>>> network.STAT_GOT_IP
1010
>>> network.STAT_NO_AP_FOUND
201
network.STAT_WRONG_PASSWORD
202

wlan.status() must return
network.STAT_NO_AP_FOUND if ssid is wrong and
network.STAT_WRONG_PASSWORD if password is wrong

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