Electronics for Building Things · Lesson 21 · Connectivity
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.
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).
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.
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.hivemq.com are fine for
learning, but they're open and insecure — never send real home data through them.
| HTTP | MQTT | |
|---|---|---|
| Pattern | Request / response | Publish / subscribe |
| Who starts | You ask (poll) | Device pushes (event) |
| Devices | One-to-one | Many-to-many via broker |
| Best for | Checking one gadget | A whole smart home |
See also: Glossary · Connectivity cheat-sheet · Rules of thumb