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.
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.
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 wildcardsHow 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 |
|
| QoS 0, 1 and 2The 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 |
|
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".
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 willBuild 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 |
|
| A broker on the boardYour 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 |
|
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".
Real systems
Home Assistant discovery, and locking the broker down.
| Lesson | Time | What students do |
|---|---|---|
| Home Assistant discoveryAnnounce 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 |
|
| SecurityLock 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 |
|
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".
Boss project
Two devices that only meet through the broker.
| Lesson | Time | What students do |
|---|---|---|
| Boss: two devices over MQTTA 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 |
|
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".
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.