MQTT on an ESP32: publish, subscribe and stay connected
Topics, retained state and PubSubClient on a simulated ESP32, then the reconnect loop every real device needs.
How you will learn it
MQTT is the messaging protocol most IoT devices use: a device publishes a reading to a topic, a broker passes it on, and anything subscribed to that topic gets it. It is small, simple and survives bad networks, which is why Home Assistant and most cloud platforms speak it.
The MQTT lesson in Connected devices writes the publish and subscribe code with PubSubClient and walks its connect loop and error codes. The simulator has no broker of its own, so the lesson then sends the same reading over HTTP to show it arriving. The lessons around it add WiFi first, then reconnecting and NTP time so the device keeps working when the network does not.
What you will learn
- Explain brokers, topics, publish and subscribe in plain terms.
- Write the PubSubClient connect, publish and subscribe code, and read its error codes.
- Reconnect cleanly when WiFi or the broker drops.
- Wire an MQTT device into Home Assistant.
The courses and lessons to take
Every lesson opens in the editor right away, signed in or not, and the simulator checks each step.
- Step 1 · 7 lessons · 4 h
Connected devices
The MQTT lesson, with the WiFi before it and the reconnect after it.
- Joining WiFiThe connect loop every connected device starts with: WiFi.begin, the states a station walks through, the address DHCP hands out, and what a wrong password and a wrong network name look like.
- MQTT with PubSubClientPublish and subscribe through a broker: topics, payloads, QoS and retained messages, the PubSubClient connect loop with its error codes, and the same reading sent over HTTP where the simulator has no broker.
- Reconnecting and NTP timeA clock that gets the time from NTP, keeps counting on its own, shows your time zone, and notices when WiFi drops and joins again without a reset.
- Step 2 · 5 lessons · 5 h
Home Assistant devices
A room sensor that reports to Home Assistant.
- A room multisensorTemperature, humidity, light and motion on one board: the sensor Home Assistant users build first, and how each reading becomes a message.
Guides and tools on the same subject
Home Assistant and ESPHome devices
Open itEmbedded track
Arduino, ESP32, Raspberry Pi Pico and STM32: pins, timing, buses, motors, WiFi, registers and debugging, every lesson checked on a simulated board.
See the whole trackQuestions people ask
What is MQTT used for?
Sending small messages between devices and servers: sensor readings, on and off commands and status. It is the usual way IoT devices talk to Home Assistant and to cloud dashboards.
What is the difference between MQTT and HTTP?
HTTP is a request and a reply. MQTT keeps one connection open and pushes messages to whoever subscribed, which suits devices that report often or must react quickly.
Which library does the lesson use?
PubSubClient, the most widely used MQTT client for Arduino and ESP32.