MQTT publish and subscribe
On this page
A notice board in a workshop has a section for each subject. The person who pins a notice does not know who will read it, and readers look only at the sections they care about. MQTT moves data between devices in this way: a device publishes a message to a named topic, and a server called the broker passes it to everyone who has subscribed to that topic.
See it
A sensor publishes a reading to the topic home/garden/moisture. Three programs have subscribed: a display to home/garden/moisture, a phone app to home/garden/# and a laptop script to home/+/moisture. Who receives the reading?
Choose the topic that the sensor publishes to. The text inside each subscriber is its filter. The broker compares the topic with every filter, level by level, and delivers the message where they match. The sensor sees none of this.
The idea
In MQTT, devices never send to each other. A device publishes a message to a topic, which is a name such as home/garden/moisture. A program that wants those messages subscribes to the topic. The broker is the server between them: it passes each message to every subscriber whose filter matches.
A topic is a row of levels separated by the / sign. A filter may hold two wildcards: + accepts any one level, and # accepts everything below. The sender does not know who the subscribers are, or whether there are any.
One device can do both. A sensor publishes its readings and also subscribes to a topic for commands, such as a new measuring interval. The lesson on MQTT and cloud communication builds such a device.
MQTT needs a network and a broker, and both can fail. So a decision that must be fast or safe, such as stopping a pump that runs dry, stays inside the device. The broker is for oversight: readings, alerts and records.
One worked number
A site has 3 sensors, and 4 programs need their readings. How many connections are needed if every sensor sends directly to every program, and how many with a broker? What changes when a fifth program is added?
Show the worked solution, 3 steps
- Count the direct connections
Each of the 3 sensors connects to each of the 4 programs: 3 × 4 = 12.
- Count the connections with a broker
Each sensor and each program connects once, to the broker: 3 + 4 = 7.
- Add the fifth program
Direct: 3 more connections, and every sensor must be changed to know the new address. With a broker: 1 more connection, and no sensor is touched.
The mistake to avoid
Check yourself
A site has 10 sensors and 3 programs that read all of them. How many connections are needed with a broker, and how many if every sensor sends directly to every program?
A program subscribes with the filter site/+/temperature. Which of these topics does it receive?
A sensor publishes an alarm, and the broker confirms that it received the message. Can the sensor be sure that the phone of the technician has shown the alarm?
Where you meet it
- A borehole pump far from the officeThe controller publishes pressure and running hours. The office subscribes and never needs the address of the pump.
- Cold rooms on several sitesEach room publishes to its own topic, and one filter with a + collects the temperature of every room.
- A solar plantA gateway publishes the inverter readings. The recording program and the alarm program both subscribe, and neither knows the other.
- A generator on standbyThe broker can announce that a device has gone silent, so a dead link is not mistaken for a quiet machine.
- Devices publish to a topic. They do not send to each other.
- The broker passes each message to every subscriber whose filter matches.
- + accepts one level and # accepts all levels below. Topics are case sensitive.
- Published does not mean received. Fast and safe decisions stay in the device.
Discussion
No comments yet. Questions, results from your own build, a better way to explain it: all welcome.
My notes on this page
Kept on this device only. Nothing is sent anywhere.
Tip: select words in the lesson, or tap a figure or an equation, to put a note right there.
Notes on earlier wording
The lesson has changed and these words are no longer in it. Your notes are kept, with the words you chose.