Skip to content

Test: MQTT (publish, subscribe and the broker)

Networking & IoT

MQTT: Publish, Subscribe and the Broker in the Middle

Why publish and subscribe?

The greenhouse has two sensor nodes, a fan controller, a phone dashboard and a logger in the cloud. Without a broker, a sensor has to know the address of every device that wants its readings, and send each of them a copy: every new display means reprogramming the sensor. MQTT puts a broker in the middle (here Mosquitto, running on a Raspberry Pi). Every device makes one connection to it. A sensor publishes each reading once, to a topic such as greenhouse/1/temp; the broker sends a copy to every client that has subscribed to that topic. Publishers and subscribers never know about each other. Switch between the two designs and add subscribers.

Challenge: With the broker, add three new subscribers and get a reading to all of them without touching node 1.

BrokerMosquitto on the Pi5 connected · 0 retainedSensor node 1ESP32-S3, bench 1connectedSensor node 2ESP32-S3, bench 2connectedFan controllerESP32-S3 and a relayconnectedfan offPhone dashboardan app on the Wi-FiconnectedCloud loggera server, via the internetconnected
PUBLISHacknowledgementsconnect and subscribekeep-alive pingsa will✕ lostRretained copy· each hop is slowed to 450 ms
← Start here
Speed
t = 0.0 s
Design
Subscribers

Node 1 sends per reading

1

Last reading reached

0 of 2

Node 1 must know

the broker only

With a broker, node 1 sends one PUBLISH whatever the number of subscribers, and a new subscriber only has to connect and SUBSCRIBE. All the clients connect out to the broker, so none of them needs to be reachable from outside.

Wire log, newest first

Nothing on the wire yet.