← index #6787Issue #4732
Related · high · value 1.338
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

<WLAN>.isconnected returns false when ESP32 connected to Wifi

closedby deepaerialopened 2019-04-30updated 2021-05-29
port-esp32

In boot.py I have following code that connects ESP32 NodeMCU to WiFi network.

import network
import ujson
from time import sleep
from machine import Pin


def read_networks_from_config():
    with open('config.json', 'r') as file:
        data = ujson.load(file)
    return data['Networks']

networks = read_networks_from_config()


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

print('Scanning network...')

discovered_networks = sta_if.scan()
for network in discovered_networks:
    n_ssid = network[0].decode('utf-8')
    print('Checking network {}'.format(n_ssid))
    net_info = list(filter(lambda x: x['SSID'] == n_ssid, networks))
    if net_info:
        net_info = net_info[0]
        sta_if.connect(net_info['SSID'], net_info['Password'])
        if sta_if.isconnected():
            led = Pin(2, Pin.OUT)
            led.value(True)
            print('Connected...')
            break
        else:
            print('Failed to connect...')

If device successfully connected to WiFi network then onboard led should turn on, but for some reason if sta_if.isconnected(): statement always returns False even though microcontroller is visible in network and is pingable. Weird thing I noticed that when connecting to device using rshell utility like this:

$  rshell --buffer-size=30 -p /dev/ttyUSB0 

if sta_if.isconnected(): does not return False value and led turns on. Also in repl isconnected returns True too.

Firmware information:

>>> os.uname()
(sysname='esp32', nodename='esp32', release='1.10.0', version='v1.10-298-g47e76b527 on 2019-04-18', machine='ESP32 module with ESP32')

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