🛠 Client HTTP : consommer une API depuis la Pico W

Raspberry Pi Pico W · intermediaire · Schéma : IMG-pico-w-intermediaire-2-picow-http-client.svg — Pico W interrogeant un service web distant via le routeur, reponse JSON affichee dans le terminal serie, comparatif des trois piles MicroPython, CircuitPython et Arduino en legende

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

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials