Pico W in MicroPython: Pin("LED") and the WLAN
MicroPython on a Raspberry Pi Pico W blinks the LED on its radio, scans for Wi-Fi, joins a WPA2 network with a deadline and reports its address, or why it…
Communication · Beginner
In a real product: Wi-Fi sensor node (Smart home): A battery sensor joins the network to report, and says clearly when it can't.
On a Pico W, MicroPython names the board's LED "LED": Pin("LED") is the radio chip's GPIO 0, not an RP2040 pin. The same firmware has the network module, which drives the CYW43439 radio. This program blinks the LED while it scans and joins a network, then prints its address, or what the status code means. An LED on GP15 lights when it could not join.
What you'll learn
Pin("LED")on a Pico W, and whyPin(25)does not light it (GP25 is the radio's chip select)network.WLAN(network.STA_IF):active(),scan(),connect(),status()andifconfig()- Waiting for a connection with a deadline instead of forever
How it works
w.active(True) powers the radio and loads its firmware. w.scan() returns a list of tuples (SSID, BSSID, channel, signal strength, security, hidden). w.connect() starts joining and returns at once, so the program polls w.status() until it reaches STAT_GOT_IP or fails, or until five seconds pass.
The status is a number: STAT_CONNECTING (1) while the radio associates, STAT_GOT_IP (3) once DHCP has given the board an address, STAT_NO_AP_FOUND (-2) when no network with that name answered and STAT_WRONG_PASSWORD (-3) when the WPA2 handshake failed. w.ifconfig() then gives the address, the netmask, the gateway and the DNS server.
The simulated access point is the diagram's "network": TryEmbedded, WPA2 with the password lab-pass-123. The program joins it and prints 192.168.4.2; the Network panel under the canvas shows the scan, the join and the lease.
Try this
- Change the password: the status becomes
STAT_WRONG_PASSWORDand the LED on GP15 lights. - Ask for a network that is not there,
w.connect("Elsewhere", "..."):STAT_NO_AP_FOUND. - Run it on a real Pico W with your own network's name and password.
main.py
# Raspberry Pi Pico W (MicroPython): the LED is the radio's, Pin("LED"). The program joins the WPA2 network
# "TryEmbedded"; an LED on GP15 lights when it could not.
import network
import time
from machine import Pin
led = Pin("LED", Pin.OUT)
no_wifi = Pin(15, Pin.OUT)
MEANING = {
network.STAT_GOT_IP: "connected",
network.STAT_NO_AP_FOUND: "no network with that name",
network.STAT_WRONG_PASSWORD: "wrong password",
network.STAT_CONNECT_FAIL: "could not connect",
}
w = network.WLAN(network.STA_IF)
w.active(True)
for ssid, bssid, channel, rssi, security, hidden in w.scan():
print("found", ssid.decode(), "channel", channel, rssi, "dBm")
w.connect("TryEmbedded", "lab-pass-123")
deadline = time.ticks_add(time.ticks_ms(), 5000)
while 0 <= w.status() < network.STAT_GOT_IP and time.ticks_diff(deadline, time.ticks_ms()) > 0:
led.toggle()
time.sleep_ms(100)
status = w.status()
print("status", status, "-", MEANING.get(status, "still connecting"))
if w.isconnected():
address, netmask, gateway, dns = w.ifconfig()
print("address", address, "gateway", gateway, "dns", dns)
no_wifi.value(0 if w.isconnected() else 1)
led.on()