Source: https://mokxi.com/teachers/plans/mqtt-in-depth
Updated: 2026-11-11

Lesson plan

# MQTT in depth lesson plan

Topics and wildcards, QoS, retained messages and last wills, a broker of your own, Home Assistant discovery and security, built as real code on the Raspberry Pi, and two devices that talk only through the broker.

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

Start a class from this course

The unit

## Objectives and materials

### By the end, students can

- How topics match, and what each QoS promises on a link that loses packets.
- Retained messages, last wills, keepalives, and a broker module of your own.
- Home Assistant discovery, and locking the broker down.
- Two devices that only meet through the broker.

### Materials for the whole unit

- Half breadboard
- LED
- Pushbutton
- Raspberry Pi 4 Model B
- Resistor

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 1 h 50 min

## The rules of the broker

How topics match, and what each QoS promises on a link that loses packets.

Lesson | Time | What students do

Topics and wildcards How a broker sorts messages: topic levels, the + and # wildcards written as a real matching function, which filters are legal, and a watcher that lights an LED when its filter matches. | 55 min

- Run it: a topic is a path
- Rules for good topics
- The + wildcard
- The # wildcard
- Quiz: predict the matches
- Filters a broker refuses
- A watcher on the LED

QoS 0, 1 and 2 The three delivery promises on a link that loses packets: fire and forget, at least once with its duplicates, and exactly once with its four-packet handshake, each counted in packets. | 55 min

- Run it: QoS 0 on a flaky link
- Quiz: a worse link
- QoS 1: at least once
- QoS 2: exactly once
- Which QoS for what
- What QoS 0 lost
- Light it when nothing is lost

### Materials

- Half breadboard
- LED
- Pushbutton
- Raspberry Pi 4 Model B
- Resistor

### Check for understanding

Each student finishes "QoS 0, 1 and 2" 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: "Light it when nothing is lost".

Chapter 2. About 1 h 40 min

## State that lasts

Retained messages, last wills, keepalives, and a broker module of your own.

Lesson | Time | What students do

Retained messages and the last will Build a broker that keeps the last value of a topic for whoever subscribes later, clears it on an empty payload, and publishes a client's last will when it drops off or misses its keepalive. | 55 min

- Run it: a broker in twenty lines
- Keep the last value
- Clear a retained message
- The last will
- Quiz: a clean goodbye
- The keepalive
- A status light

A broker on the board Your broker becomes a module, and a real button and LED become clients: the button publishes, an automation decides, the lamp obeys and reports its state, and a dashboard that joins late still knows. | 45 min

- Run it and press the button
- A lamp that obeys
- Toggle it off
- A dashboard that joins late
- Design the topic tree
- One filter, every room

### Materials

- Half breadboard
- LED
- Pushbutton
- Raspberry Pi 4 Model B
- Resistor

### Check for understanding

Each student finishes "A broker on the board" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "One filter, every room".

Chapter 3. About 1 h 50 min

## Real systems

Home Assistant discovery, and locking the broker down.

Lesson | Time | What students do

Home Assistant discovery Announce devices to Home Assistant with retained JSON on discovery topics: a temperature sensor and a button appear on their own, survive a restart, go unavailable through the last will, and disappear on an empty config. | 55 min

- Run it: an empty Home Assistant
- Announce a sensor
- Send a reading
- Quiz: Home Assistant restarts
- A button as a binary sensor
- Unavailable, through the will
- Remove an entity

Security Lock the broker down: usernames and passwords with the real CONNACK codes, access lists that limit each client to its own topics, credentials out of the code, hashed passwords, and what TLS on port 8883 adds. | 55 min

- Run it: log in
- A wrong password
- A stranger
- Access lists
- Credentials out of the code
- TLS and port 8883
- Store hashes, not passwords

### Materials

- Half breadboard
- LED
- Pushbutton
- Raspberry Pi 4 Model B
- Resistor

### Check for understanding

Each student finishes "Security" 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: "Store hashes, not passwords".

Chapter 4. About 45 min

## Boss project

Two devices that only meet through the broker.

Lesson | Time | What students do

Boss: two devices over MQTT A switch device and a lamp device that only meet through the broker: last wills and birth messages, a command and a retained state, an automation between them, a dashboard on wildcards, and a room of your own. | 45 min

- The brief
- Milestone 1: the press switches the lamp
- Milestone 2: a dashboard on wildcards
- Milestone 3: a device drops
- Make it yours: another room
- Take it to real hardware

### Materials

- Half breadboard
- LED
- Pushbutton
- Raspberry Pi 4 Model B
- Resistor

### Check for understanding

Each student finishes "Boss: two devices over MQTT" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.

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

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.

Start a class from this course See the demo class

More units

## Other lesson plans

Electronics basics 20 lessons, about 10 h. Embedded on the Arduino Uno 16 lessons, about 12 h. Embedded on the ESP32-C3 13 lessons, about 11 h. Embedded on the Raspberry Pi Pico 7 lessons, about 6 h. Build your own fight stick 5 lessons, about 4 h. Embedded on the STM32F411 6 lessons, about 6 h. C++ on a board 13 lessons, about 10 h. Digital logic and memory 4 lessons, about 4 h. Interactive devices on the Uno 9 lessons, about 8 h. From gates to a computer 23 lessons, about 16 h. Build a claw machine 8 lessons, about 6 h. Every unit All the courses as ready-made units, on the teachers page.
