Skip to content
You Start with one site
Amounts in Start with one site
IoT & AIoT · Course

MQTT, the language devices speak

Lesson 2 of 4 Beginner 9 min read
Found this useful?

If every device had to open a direct connection to every dashboard, app, and alerter that cared about its data, you would drown in wiring the first time an IP address changed. MQTT (Message Queuing Telemetry Transport) solves this the way a post office solves mail: nobody delivers hand to hand. Everyone talks to one hub in the middle.

#MQTT #IoT #Messaging

One broker in the middle

MQTT has a single piece in the center called the broker. Devices and apps never connect to each other; they all connect to the broker. A device publishes a message to the broker, and the broker hands it to everyone who subscribed. Do not just read that, do it: set a temperature and publish. Watch one message reach every subscriber at once, none of which the device knows anything about.

d/SWD-3F/t ESP32 your device waiting broker the hub Dashboard waiting Alert rule waiting Phone app waiting
One publish, delivered to every subscriber. The device never knows the dashboard, the alert rule, or your phone exist, and it does not need to.

Topics are the addresses

A message is not just thrown at the broker; it is published to a topic, a slash-separated label like farm/greenhouse-1/temperature. Topics are how the broker knows who gets what. A publisher never needs to know who is listening, and a listener never needs to know which device sent it. Topics form a tree, so a subscriber can ask for one exact reading or a whole branch:

farm greenhouse-1 greenhouse-2 pump-station temperature humidity farm/greenhouse-1/# · all farm/+/temperature · each
# matches everything below a point; + matches one level. Subscribe as narrow or as wide as you like.
On a broker you run, you choose the topic scheme. A good one puts the device in the topic, for example d/<id>/t for the telemetry it sends up and d/<id>/c for commands sent back down. That is the exact topic you published to above. The next lesson uses siliconwit.io, our monitoring service, as its worked example, and it gives each device those same fixed topics.

Publish and subscribe, not request and reply

This is the shift worth internalizing. A web browser pulls: it asks a server for a page and waits. MQTT pushes: a device publishes the instant it has something, and every subscriber is handed it immediately, exactly as the broker above delivered your reading the moment you pressed Publish. Nobody polls, nobody waits, and one message serves any number of listeners.

Two knobs you will see everywhere

You can send your first telemetry without touching these, but meet them now:

  • QoS (quality of service) is how hard the broker tries to deliver: 0 is fire and forget, 1 guarantees at least once, 2 guarantees exactly once. Higher costs more traffic; a reading every 30 seconds is happy at 0 or 1.
  • Retained tells the broker to keep the last message on a topic, so a subscriber that connects later immediately gets the current value instead of waiting. Perfect for state like a relay being on or off.

Check yourself

Question 1

A sensor has a new reading. How does it reach a dashboard in MQTT?

Question 2

What is a topic actually for?

Question 3

Your phone app opens and instantly shows a relay is ON, though the device last published an hour ago. What made that possible?

Question 4

You subscribe to farm/+/temperature. What do you receive?

Key points
  • A broker sits in the middle; devices and apps connect to it, never to each other.
  • Topics are slash-separated addresses; publish to one, subscribe to one, with + and # wildcards.
  • MQTT pushes, so subscribers get data the instant it is published, and one message serves all of them.
  • QoS controls delivery effort; retained keeps the last value for late subscribers.

You now have the model, and you have watched it work. Time to use it for real: next you will publish live temperature to a broker over an encrypted connection: one you run, or siliconwit.io, the worked example.

Found this useful?

Discussion

    Comments show your name. Be kind, stay on the topic.