🛠 Servir une page web depuis la Pico W (socket TCP + HTTP)
Objectives
Once connected to Wi-Fi (connection tutorial), the Pico W is a full network node: it knows how to listen on a TCP port and answer in HTTP. You will write a mini web server with MicroPython's socket module — the same principle as the official picow_httpd example from the pico-examples repository (pico_w/wifi/ folder), in much shorter form. The displayed page can later carry buttons to drive GPIOs: this is the basis of any embedded control interface.
Hardware
- 1 × Raspberry Pi Pico W +
_picowMicroPython firmware - 2.4 GHz Wi-Fi network (the board must be reachable from your PC)
- Serial console at 115200 bauds (to read the IP at startup)
- A web browser on the same network
Full code
# webserver.py — mini serveur HTTP sur la Pico W (MicroPython)
import machine, network, socket, time
def do_connect:
wlan = network.WLAN
wlan.active(True)
if not wlan.isconnected:
wlan.connect('my_ssid', 'my_password')
while not wlan.isconnected:
machine.idle
print('IP :', wlan.ipconfig('addr4'))
do_connect
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
s = socket.socket
s.bind(addr)
s.listen(1)
print('ecoute sur port 80')
while True:
cl, remote = s.accept
req = cl.recv(100) # lit la requête HTTP
print('recu:', req[:40])
page = (b'HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n'
b'<h1>Bonjour depuis ma Pico W</h1>'
b'<p>Rafrichis-moi !</p>')
cl.write(page)
cl.close
Expected verification
The serial console displays the IP (e.g. 192.168.1.42). Pointing the browser at http://192.168.1.42/, the page displays and each reload makes the received request scroll in the console. The CYW43439 can also serve as an access point (SoftAP) with up to 4 clients — in this mode, the Pico W itself distributes the network (documented limit of the chip).
Common errors
- The browser runs on a different network (4G, guest VLAN): the board must be reachable on the same LAN; check the ping before hunting for a bug.
- Blocking
acceptloop: one connection at a time; if a client keeps the connection open (some browsers preconnect), the following ones wait — add acl.settimeoutor serve fast and close cleanly. - Forgetting the
\r\nin the HTTP header: the browser displays raw text or spins around; the header separator is mandatory. recv(100)then full request processing: enough for a simple GET, but do not count on serving files with that — read the request up to the double\r\n\r\n.- PC firewall blocking, or router with client isolation (AP isolation): two classic causes of "unreachable IP".
- Nothing goes through in SoftAP: the CYW43439 limits the number of clients (4) and your PC must be connected to the network emitted by the board.
Other tutorials for this board
- Faire clignoter la LED embarquée (elle est derrière la puce Wi-Fi !)
- Se connecter à un réseau Wi-Fi (station, WPA2/WPA3)
- Bouton et potentiomètre sans marcher sur le sans-fil (GPIO libres & ADC)
- Client HTTP : consommer une API depuis la Pico W
- Heure exacte via NTP : l'horloge temps réel des projets connectés
- Client MQTT : télémétrie IoT légère sur la Pico W
Code examples for this board
clignotante.pyPythonbouton-interruption.pyPythonpwm-servo.pyPythonlecture-adc.pyPythonneopixel.pyPythonoled-i2c.pyPython