Lessons and the learning game

Connected devices in MicroPython

Network devices in MicroPython on a Pico W: WiFi states, HTTP, a socket web server, JSON, asyncio, backoff and the watchdog, and a buoy dashboard.

The board

Every lesson runs on a Raspberry Pi Pico W with the official MicroPython v1.29.0 firmware, on Mokxi's whole-chip model with its own lwIP network stack. The boss adds a BME280 on I2C0 (SDA on GP4, SCL on GP5). The course follows the MicroPython basics courses and does not repeat them.

Take it at /learn/path/micropython-iot on /learn.

The network it joins

The board joins Mokxi's simulated access point (Mokxi-Lab, password breadboard; the board's WiFi properties) and gets 192.168.4.2 by DHCP. DNS answers only names under .mokxi: weather.mokxi, time.mokxi, echo.mokxi and api.mokxi, whose status and body you set in the Cloud panel. Everything else, real websites included, fails to resolve with OSError -2, which the lessons catch on purpose. The Web panel is a browser on the same network, so the board's own server can be opened and clicked. See WiFi in Mokxi.

The lessons

  1. The network module. Every state wlan.status() passes through, a connect function that fails fast on a negative state, the address, mask, gateway and DNS server, the hostname and MAC, and names that do not resolve.
  2. urequests in depth. Status codes and what to do about 2xx, 4xx and 5xx, headers, a fetch that always closes, failures before any answer, and a POST with headers.
  3. A tiny web server. bind, listen and accept, the request line, routes that switch the Pico W's LED, and a 404.
  4. JSON. A space weather feed parsed into dicts and lists, bad JSON and missing keys caught, readings turned into JSON and served at /api.
  5. asyncio servers. asyncio.start_server with a coroutine per connection beside a blinker task that never stops.
  6. Error recovery. Exponential backoff for joining and for requests, running on the last good value, and a hardware watchdog that restarts a hung board.
  7. Boss: an ocean buoy dashboard. A BME280 task, a forecast task with backoff, an HTML dashboard and a JSON API, and a storm light driven by the forecast and the pressure.

Honest limits

  • The network is simulated: no radio, no real internet, no TLS. The code is the code a real Pico W runs on a real network; only the servers it can reach differ.
  • Nothing outside the browser tab can reach the board's server: the Web panel is the only client.
  • Changing the access point's settings while the board is connected does not drop it; the lessons show failures with the board's properties and the api.mokxi status instead.

Pi hardware in Python, Python on a Raspberry Pi's buses.