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.

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.

LessonTimeWhat 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
  1. Run it: a topic is a path
  2. Rules for good topics
  3. The + wildcard
  4. The # wildcard
  5. Quiz: predict the matches
  6. Filters a broker refuses
  7. A watcher on the LED
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
  1. Run it: QoS 0 on a flaky link
  2. Quiz: a worse link
  3. QoS 1: at least once
  4. QoS 2: exactly once
  5. Which QoS for what
  6. What QoS 0 lost
  7. 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.

LessonTimeWhat 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
  1. Run it: a broker in twenty lines
  2. Keep the last value
  3. Clear a retained message
  4. The last will
  5. Quiz: a clean goodbye
  6. The keepalive
  7. A status light
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
  1. Run it and press the button
  2. A lamp that obeys
  3. Toggle it off
  4. A dashboard that joins late
  5. Design the topic tree
  6. 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.

LessonTimeWhat 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
  1. Run it: an empty Home Assistant
  2. Announce a sensor
  3. Send a reading
  4. Quiz: Home Assistant restarts
  5. A button as a binary sensor
  6. Unavailable, through the will
  7. Remove an entity
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
  1. Run it: log in
  2. A wrong password
  3. A stranger
  4. Access lists
  5. Credentials out of the code
  6. TLS and port 8883
  7. 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.

LessonTimeWhat 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
  1. The brief
  2. Milestone 1: the press switches the lamp
  3. Milestone 2: a dashboard on wildcards
  4. Milestone 3: a device drops
  5. Make it yours: another room
  6. 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.