Lesson plan

Connected devices in MicroPython lesson plan

Network-ready MicroPython on the Pico W: every WiFi state and failure, HTTP status codes and headers, a web server from a bare socket, JSON both ways, asyncio servers, backoff and the watchdog, ending in an ocean buoy dashboard.

4 chapters, 7 lessons, about 7 hours, on the Raspberry Pi Pico W. Every lesson runs in the browser, with nothing to install.

The unit

Objectives and materials

By the end, students can

  • The network module and HTTP requests, failures included.
  • A web server from a socket, and JSON for programs.
  • asyncio servers, and recovering from every failure.
  • An ocean buoy dashboard on a Pico W.

Materials for the whole unit

  • Pressure, humidity and temperature, BME280
  • Raspberry Pi Pico W

Every part is already on the simulator’s bench, so there is nothing to buy or hand out. The counts are the most any one lesson uses, if you build it on real breadboards too.

Chapter 1. About 2 h 5 min

The board as a client

The network module and HTTP requests, failures included.

LessonTimeWhat students do
The network modulePast "connected": every state a station passes through, a connect function with a timeout that fails fast, the address, mask, gateway and DNS server, the hostname and MAC, and names that do not resolve.60 min
  1. Run it: watch the states
  2. Four numbers from DHCP
  3. A name and a MAC
  4. Names that do not resolve
  5. A wrong password
  6. Rename the board
  7. Leases and fixed addresses
urequests in depthEvery answer has a status: 200 and 201 for success, 4xx for your mistake, 5xx for the server’s. Headers, bodies, a fetch() that always closes, failures before any answer, and a POST with your own headers.65 min
  1. Run it: a careful fetch
  2. Status and headers
  3. The body
  4. The server’s problem
  5. Our mistake
  6. No answer at all
  7. A POST with headers
  8. Memory on a microcontroller

Materials

  • Raspberry Pi Pico W

Check for understanding

Each student finishes "urequests in depth" on their own, without opening the hints. The simulator checks 8 of its 8 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Memory on a microcontroller".

Chapter 2. About 1 h 50 min

The board as a server

A web server from a socket, and JSON for programs.

LessonTimeWhat students do
A tiny web serverThe board as the server, from a bare socket: bind, listen, accept, read the request line, route the path, send a status line, headers and a page, and switch the Pico W’s LED from a browser.50 min
  1. Run it: listening
  2. A browser asks
  3. Routes
  4. Not found
  5. Click it yourself
  6. Who can reach it
JSON both waysThe language devices and dashboards speak: parse a space weather feed into dicts and lists, survive bad JSON and missing keys, turn your own readings into JSON, and serve them at /api.60 min
  1. Run it: a space weather feed
  2. Text to data
  3. Lists inside
  4. Not JSON at all
  5. A missing key
  6. Data to text
  7. Serve it at /api

Materials

  • Raspberry Pi Pico W

Check for understanding

Each student finishes "JSON both ways" on their own, without opening the hints. The simulator checks 7 of its 7 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Serve it at /api".

Chapter 3. About 1 h 55 min

Robust devices

asyncio servers, and recovering from every failure.

LessonTimeWhat students do
asyncio serversA server that does not stop the world: asyncio.start_server with one coroutine per connection, a blinker task that never pauses, and the rule that makes it all work: await, never sleep.50 min
  1. Run it: serving and blinking
  2. Three jobs at once
  3. A request in the middle
  4. Two visitors
  5. Blink faster
  6. Never block
Error recoveryA device that keeps working when the network does not: retries with exponential backoff for joining and for requests, running on the last good value, and a hardware watchdog that restarts a hung board.65 min
  1. Run it: a device for the real world
  2. The watchdog
  3. The server is busy
  4. It comes back
  5. Down for good
  6. Backoff on joining
  7. Hang it
  8. Layers of defense

Materials

  • Raspberry Pi Pico W

Check for understanding

Each student finishes "Error recovery" on their own, without opening the hints. The simulator checks 8 of its 8 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Layers of defense".

Chapter 4. About 1 h 5 min

Boss project

An ocean buoy dashboard on a Pico W.

LessonTimeWhat students do
Boss: an ocean buoy dashboardA Pico W on a buoy: a BME280 read by an asyncio task, a sea forecast fetched with backoff, an HTML dashboard and a JSON API served at once, and a storm light driven by the forecast and the pressure.65 min
  1. Run it: buoy 7 online
  2. The forecast
  3. The dashboard
  4. The API
  5. A storm forecast
  6. Falling pressure
  7. The forecast goes down
  8. Tune the storm line

Materials

  • Pressure, humidity and temperature, BME280
  • Raspberry Pi Pico W

Check for understanding

Each student finishes "Boss: an ocean buoy dashboard" on their own, without opening the hints. The simulator checks 8 of its 8 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Tune the storm line".

Your class

Teach this unit as a class

Start a class from this course and its 7 lessons are assigned in one go, in order. Students join with a six-character code or printed login cards, and each one’s work comes back to you.

The class tools are free for 30 days for one class of up to 20 students, with no card. The lessons themselves are free either way.