Skip to content
Tutorials Center
My learning

Topics

Lesson 35 of 36 ยท Projects and Practice

Project: A Mini MQTT Sensor Network

Combine everything: a TCP broker with wildcards, retained state and Last Will, three simulated sensor nodes, a dashboard client and a crash that is detected.

This project joins the pieces of the MQTT chapter into one running system. Over real TCP sockets on loopback you will start a broker, three sensor nodes, and a dashboard that shows readings and node status. One node will crash without saying goodbye, and the broker will publish its Last Will so the dashboard learns about it. It is the toy broker extended with the features from retained messages, will and sessions.

This is a teaching broker with a text protocol, not real MQTT, and it has no security. See the security lessons for what a production system needs.

Architecture

flowchart LR
  N1["node kitchen"] -- "net/kitchen/temp<br/>net/kitchen/status (retained, will)" --> B(("Broker<br/>127.0.0.1"))
  N2["node garage"] --> B
  N3["node attic"] --> B
  B -- "net/#" --> D["Dashboard"]

Topic plan (broad to narrow, as in the topic lesson):

  • net/<node>/temp: a reading as a number.
  • net/<node>/status: online or offline, retained. The will is offline.

Protocol lines: SUB <filter>, PUB <topic> <retain 0|1> <payload>, WILL <topic> <retain> <payload> (registered at connect time) and BYE (clean disconnect, which cancels the will).

The whole system

Node.js Broker, three nodes, a crash and a dashboard

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Read the output in three parts. First every node announces online and the dashboard shows the readings. Second, when the attic socket is destroyed, the dashboard immediately sees attic status = offline, which came from the will and not from the node itself. Third, a dashboard that joins late receives the retained status of all nodes at once, so it shows the correct picture without waiting for new messages. The kitchen's clean shutdown published offline itself and the will was discarded.

Stretch goals

  1. Heartbeat. Have nodes send PING every few seconds and make the broker drop and "will" a node after twice that time without any message (a keep-alive), so that a network failure that does not close the socket is also detected.
  2. Access control. Let WILL and PUB only use topics that start with net/<own-name>/.
  3. Alert client. Add a client that subscribes to net/+/temp and publishes net/alerts/<node> when a value exceeds 30.
  4. Real MQTT. Install Mosquitto on a PC, run the sensor logic on an ESP32 or Raspberry Pi with the paho-mqtt or PubSubClient library, and subscribe with mosquitto_sub -t "net/#" -v.

A real node on a Raspberry Pi in Python looks like this (read-only, needs paho-mqtt and a broker):

Python
import paho.mqtt.client as mqtt, time, random

c = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, client_id="attic")
c.will_set("net/attic/status", "offline", qos=1, retain=True)
c.connect("broker.local", 1883, keepalive=30)
c.publish("net/attic/status", "online", qos=1, retain=True)
c.loop_start()
while True:
    c.publish("net/attic/temp", round(25 + random.random() * 6, 1))
    time.sleep(10)

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Who published attic status = offline when the attic node lost power?

    A dead node cannot send anything; the broker publishes the will when the connection ends without BYE.
  2. 2. Why does a late dashboard see the nodes' status immediately?

    Retained status messages hold the last known state per topic.
  3. 3. Why are temperature readings not retained in this design?

    Status is state worth retaining. A retained old temperature might look current to a late joiner.
  4. 4. What does BYE do in the toy protocol?

    It is the analogue of MQTT's DISCONNECT packet, which cancels the will.

Summary

  • A small TCP broker plus nodes demonstrates fan-out, wildcards, retained state and Last Will working together.
  • Status topics use retain plus will so everyone always sees the truth.
  • Clean disconnect cancels the will; a crash triggers it.
  • Next steps: keep-alive timeouts, access control and a move to real MQTT with Mosquitto.

Next: IoT Practice: Questions, Exercises and Interview Prep