Electronics for Building Things · Lesson 21 · Connectivity

MQTT — the IoT Messaging Backbone

The lightweight publish/subscribe pattern that smart-home devices speak.

HTTP (Lesson 20) is great for poking one gadget. But a home full of devices that need to announce things and react to each other wants a different model. That model is MQTT — the de facto language of the connected home.

The one win You understand publish/subscribe — broker, topics, publishers, subscribers — and why it fits home automation far better than everyone running web servers.

Publish / subscribe, via a broker

Instead of devices calling each other directly, they all connect to one central broker (a small server). Devices publish messages to named topics, and devices subscribe to the topics they care about. The broker delivers each message to whoever subscribed (HiveMQ: MQTT Essentials).

broker sensor (pub) publishes temp phone (sub) logger (sub)
The sensor publishes once to a topic; the broker fans it out to every subscriber. Nobody needs to know about anyone else.

Topics are organized like paths: home/livingroom/temperature, home/garage/door. A publisher and subscriber only need to agree on the topic name — they're otherwise completely decoupled. Add a new device that subscribes to a topic and it just starts receiving; nothing else changes.

Why it fits home automation

On the ESP32

The PubSubClient library handles it: connect to the broker, publish() a sensor reading to a topic, and subscribe() to a command topic so your gadget reacts when someone publishes to it. Two small extras worth knowing by name: retain (the broker keeps the last value so new subscribers get it immediately) and QoS (how hard to try to deliver a message).

Broker choice & safety Run your own broker (Mosquitto, often right on your Home Assistant box) so your home's data stays local and private. Public test brokers like broker.hivemq.com are fine for learning, but they're open and insecure — never send real home data through them.

HTTP vs MQTT, at a glance

HTTPMQTT
PatternRequest / responsePublish / subscribe
Who startsYou ask (poll)Device pushes (event)
DevicesOne-to-oneMany-to-many via broker
Best forChecking one gadgetA whole smart home

Check yourself

Read this next (primary source) HiveMQ — MQTT Essentials for the concepts, and Random Nerd Tutorials — ESP32 MQTT for the ESP32 code.
I'm your teacher — ask me anything. Want to design a topic structure for your home (what publishes where)? Bring me your devices and we'll lay it out together.

See also: Glossary · Connectivity cheat-sheet · Rules of thumb