Pico 2 W in MicroPython: a WLAN with a deadline and a setup access point
MicroPython on a Raspberry Pi Pico 2 W scans for Wi-Fi, gives joining five seconds, starts an access point when that fails, and dims an LED from a knob all…
Communication · Beginner
In a real product: Wi-Fi dimmer (Smart home): A wall dimmer joins the home network when it can and offers its own setup network when it can't, while the knob keeps working.
The Pico 2 W runs MicroPython's RPI_PICO2_W build: the RP2350's Arm cores with the network module that drives the CYW43439 radio. As on the Pico W, Pin("LED") is the radio chip's GPIO 0, not a pin of the RP2350. This program blinks that LED while it joins the network, starts an access point when it can't, and turns a knob on GP26 into the brightness of an LED on GP15 the whole time.
What you'll learn
network.WLAN(network.STA_IF):scan(),connect()andstatus(), with a deadline fromtime.ticks_add()network.WLAN(network.AP_IF): an access point of the board's own, and its address fromifconfig()ADC(26).read_u16()andPWM(...).duty_u16(), both 16-bit, so one feeds the other directly
How it works
sta.active(True) powers the radio and loads its firmware. sta.connect() returns at once; the loop then polls sta.status() until it has an address (STAT_GOT_IP), fails (a negative status), or the deadline passes. time.ticks_diff() keeps that right even when the millisecond counter wraps.
When the join fails, sta.status() says why: STAT_NO_AP_FOUND (-2) for a network that is not there, STAT_WRONG_PASSWORD (-3) for a WPA2 handshake that failed. The program then turns the station off and starts an access point named pico2w-setup at 192.168.4.1, which a phone could join on a real board to give it the password.
The home network is the diagram's "network": TryEmbedded, WPA2 with the password lab-pass-123. As written the program joins it and prints 192.168.4.2, and the board's LED stays on.
Try this
- Change the password: the status is -3 and the access point starts; nothing joins it in the simulator.
- Print
sta.config("mac"): it comes from the radio chip. - Use
level.freq(50)and watch the LED flicker: PWM is a fast on and off, and 50 Hz is slow enough to see.
main.py
# Raspberry Pi Pico 2 W (MicroPython): join the home network within 5 seconds, or start an access point instead.
# Pin("LED") is the radio's GPIO 0. A knob on GP26 sets the brightness of the LED on GP15 (PWM).
import network
import time
from machine import ADC, PWM, Pin
led = Pin("LED", Pin.OUT)
knob = ADC(26)
level = PWM(Pin(15), freq=1000)
sta = network.WLAN(network.STA_IF)
sta.active(True)
print("networks in range:", [ap[0].decode() for ap in sta.scan()])
sta.connect("TryEmbedded", "lab-pass-123")
deadline = time.ticks_add(time.ticks_ms(), 5000)
while 0 <= sta.status() < network.STAT_GOT_IP and time.ticks_diff(deadline, time.ticks_ms()) > 0:
led.toggle()
time.sleep_ms(100)
if sta.isconnected():
print("joined, address", sta.ifconfig()[0])
led.on()
else:
print("could not join (status %d): starting an access point" % sta.status())
sta.active(False)
ap = network.WLAN(network.AP_IF)
ap.config(ssid="pico2w-setup", key="configure-me")
ap.active(True)
print("access point pico2w-setup at", ap.ifconfig()[0])
while True:
raw = knob.read_u16()
level.duty_u16(raw)
print("knob %.2f V" % (raw * 3.3 / 65535))
if not sta.isconnected():
led.toggle()
time.sleep_ms(250)