wifi

Join a WLAN in station mode.

Attributes

Functions

connect(wlan, ssid, password[, timeout_s, attempts, log])

Connect wlan (a network.WLAN STA interface) and return its IP address.

known_networks(config)

Return the configured networks as [(ssid, password)].

plan(networks, visible)

Order networks for trying after a scan.

connect_any(wlan, networks[, last, timeout_s, ...])

Connect to one of the known networks.

last_network([path])

Return the SSID that worked last time, or None.

remember_network(ssid[, path])

Store the SSID that worked; writes the flash only when it changes.

Module Contents

wifi.POLL_INTERVAL_S = 0.25
wifi.STAT_CONNECTING = 1001
wifi.RADIO_SETTLE_S = 0.5
wifi.connect(wlan, ssid, password, timeout_s=6, attempts=3, log=print)

Connect wlan (a network.WLAN STA interface) and return its IP address.

Each attempt waits up to timeout_s; between attempts the radio is restarted. On the Feather ESP32-S3 the first attempt after a reset occasionally hangs in STAT_CONNECTING, while successful attempts finish within 1-3 s; a fresh attempt then usually succeeds. Returns None if all attempts fail.

wifi.LAST_NETWORK_FILE = 'wlan_last.txt'
wifi.SCANNING = 'scanning'
wifi.CONNECTING = 'connecting'
wifi.known_networks(config)

Return the configured networks as [(ssid, password)].

They come from config.WIFI_NETWORKS, or from the single WIFI_SSID/WIFI_PASSWORD pair of older configurations.

wifi.plan(networks, visible)

Order networks for trying after a scan.

The list order is the priority. Networks in range come first, then those the scan did not show (hidden SSIDs do not show up). visible is a set of SSIDs.

wifi.connect_any(wlan, networks, last=None, timeout_s=6, attempts=2, log=print, progress=None)

Connect to one of the known networks.

The first network in the list that works wins; the result is (ip, ssid), or (None, None) if none works.

If last (the network that worked last time) is the first in the list, it is tried without a scan, saving 2-3 s per wake in the usual case. Otherwise a scan decides, so the board moves back to a higher-priority network (like from a travel router to the home WLAN) when it is in range.

progress(step, ssid) is called before each step, with SCANNING (ssid None) or CONNECTING, to show what takes the time.

wifi.last_network(path=LAST_NETWORK_FILE)

Return the SSID that worked last time, or None.

wifi.remember_network(ssid, path=LAST_NETWORK_FILE)

Store the SSID that worked; writes the flash only when it changes.