MicroPython web server on a Pico W: switch the LED
MicroPython on a Raspberry Pi Pico W: a socket server on port 80 serves a page with ON and OFF links that switch the board’s own LED. The whole build, a Raspberry Pi Pico W, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Advanced About 20 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $6. On Mokxi they're free.
What you'll learn
- Joining Wi-Fi with
network.WLAN - A tiny web server built on a socket
- Reading the path in an HTTP request
- Switching hardware from a web page
- Difficulty: Advanced
- Time: About 20 minutes
- Board: Raspberry Pi Pico W
- Parts: 1
How it works
The Pico W has a Wi-Fi chip, and the LED on the board is wired to that chip. There is nothing to build: the whole project is the code. It joins the network named "Mokxi-Lab", waits until it is connected, and prints its address.
Then it opens a socket server on port 80 and waits. Each time a browser asks for a page, the code reads the path. The path /on turns the LED on, /off turns it off, and any page it sends back says whether the LED is on or off, with ON and OFF links. Open the Web panel to visit the board.
Parts list
1 part, plus the jumper wires. They retail for about $6; on Mokxi they're free.
- 1 × Raspberry Pi Pico W
Kits that can build it
These 2 starter kits have every part in the circuit. Each kit's page lists what else it can build.
The code
The MicroPython code exactly as the editor opens it. Change a line there and press Run: the board runs it on the official MicroPython firmware, with nothing to compile.
# A web page that switches the Pico W's LED: join the network, then serve
# port 80. /on and /off switch the LED (Pin("LED"), on the radio chip);
# every page has links to both. Open the Web panel to visit the board.
import network
import socket
import time
from machine import Pin
led = Pin("LED", Pin.OUT, value=0)
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect("Mokxi-Lab", "breadboard")
while not wlan.isconnected():
time.sleep_ms(250)
print("Serving on http://%s/" % wlan.ifconfig()[0])
PAGE = """<!DOCTYPE html><html><body><h1>Pico W LED</h1>
<p>The LED is %s.</p><p><a href="/on">ON</a> <a href="/off">OFF</a></p></body></html>"""
server = socket.socket()
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind(("0.0.0.0", 80))
server.listen(1)
while True:
client, _ = server.accept()
request = client.recv(1024).decode()
path = request.split(" ")[1] if " " in request else "/"
if path == "/on":
led.value(1)
elif path == "/off":
led.value(0)
print("GET", path, "LED", "on" if led.value() else "off")
client.send("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n")
client.send(PAGE % ("on" if led.value() else "off"))
client.close()
Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- It never prints an address This project
The loop waits forisconnected. Check the network name and password in thewlan.connectline. - The page loads but the LED does not change This project
Visit /on and /off, or click the links. The LED isPin("LED"), on the radio chip, not a numbered pin. - Pins are powered from 5 V, or a 5 V signal goes into a GPIO. The RP2040 pins are 3.3 V only and are not 5 V tolerant. Raspberry Pi Pico W
Power the board from VBUS or VSYS at 5 V, but keep GPIO signals at 3.3 V. Use a level shifter or a divider for 5 V parts. - WiFi code fails to connect, or the on-board LED code does nothing. The LED on the Pico W is not on GP25 like on the plain Pico. Raspberry Pi Pico W
Use the board's LED_BUILTIN name so the library drives it through the WiFi chip. Join a 2.4 GHz network only, with the right SSID and password, and wait for the link before sending.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.