🛠 Client HTTP : consommer une API depuis la Pico W
Objectives
In the other direction: the Pico W becomes an HTTP client. This is the foundational step for any sensor dashboard, connected alarm clock, or weather display. MicroPython includes the requests library, which hides sockets behind requests.get / requests.post. You will also see how the same operation is written on the two other recommended stacks for the Pico W: CircuitPython (module wifi + socketpool, official port raspberry_pi_pico_w) and Arduino via the community core Philhower arduino-pico (recommended by Adafruit, WiFi.h mapped to the cyw43 driver).
Hardware
- 1 × Raspberry Pi Pico W + MicroPython
_picowfirmware - Internet access via the access point (2.4 GHz)
- Serial console at 115200 baud
- A simple HTTP URL for testing (a public JSON API, or your server from the previous tutorial)
Complete code
# http_client.py — GET + POST JSON depuis la Pico W (MicroPython)
import machine, network, time
wlan = network.WLAN; wlan.active(True)
if not wlan.isconnected:
wlan.connect('my_ssid', 'my_password')
while not wlan.isconnected:
machine.idle
import requests # bundlé dans MicroPython
r = requests.get('http://example.com')
print(r.status_code, r.text[:60]) # 200 + début du corps
# Version CircuitPython (board raspberry_pi_pico_w) — identifiants dans
# CIRCUITPY_WIFI_SSID / CIRCUITPY_WIFI_PASSWORD du fichier settings.toml :
# import wifi, socketpool, requests
# pool = socketpool.SocketPool(wifi.radio)
# r = requests.get('http://example.com', pool=pool)
# >
# wifi.radio.connect non revérifiés sur la doc CircuitPython (404 au fetch).
Expected verification
200 followed by the first characters of the page is displayed on the console. With a JSON API, replace the truncation with r.json to obtain a usable Python dictionary. With HTTPS (https://), MicroPython verifies the certificate: an accurate clock and valid CAs are required (see the NTP tutorial).
Common errors
- HTTPS without an updated time: X.509 certificate validation fails on a clock stuck in 2000 — synchronize the time (NTP) or test with HTTP first.
- Forgetting
pool=poolin CircuitPython:requestsfalls back to the default network interface and the call fails; on the Pico W you must passSocketPool(wifi.radio). - Chunked/gzipped response poorly handled by
r.text: some APIs send gzip that MicroPython does not decompress — choose a plain-text API or decode the headers yourself. - Default timeout too short on a congested Wi-Fi network: pass
timeout=torequests.get. - Believing CircuitPython supports BLE: on the Pico W, BLE is not supported by CircuitPython (hardware capability only) — for Bluetooth, switch to MicroPython (
bluetooth/aioble) or the C SDK with BTstack. - Spamming APIs without backoff: the radio consumes a lot of power; group your requests and sleep between them (
time.sleep).
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)
- Servir une page web depuis la Pico W (socket TCP + HTTP)
- 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