Embedded track
MQTT

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.

FreeIn your browser2 courses, 12 lessons, about 9 hours
Overview

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.

Outcomes

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.
Your route

The courses and lessons to take

Every lesson opens in the editor right away, signed in or not, and the simulator checks each step.

  1. Step 1 · 7 lessons · 4 h

    Connected devices

    The MQTT lesson, with the WiFi before it and the reconnect after it.

    1. 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.
    2. 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.
    3. 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.
    Every lesson in the course
  2. Step 2 · 5 lessons · 5 h

    Home Assistant devices

    A room sensor that reports to Home Assistant.

    1. A room multisensorTemperature, humidity, light and motion on one board: the sensor Home Assistant users build first, and how each reading becomes a message.
    Every lesson in the course
Keep going

Guides and tools on the same subject

Track

Embedded 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 track
Questions

Questions 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.