Source: https://mokxi.com/learn/mqtt-tutorial
Updated: 2026-11-11

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.

Free In your browser 2 courses, 12 lessons, about 9 hours

Start free Joining WiFi See the course

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.

- Step 1 · 7 lessons · 4 h

### Connected devices

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

- Joining WiFi The 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 PubSubClient Publish 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 time A 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
- Step 2 · 5 lessons · 5 h

### Home Assistant devices

A room sensor that reports to Home Assistant.

- A room multisensor Temperature, 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

Library

### PubSubClient library

Open it

Guide

### Home Assistant and ESPHome devices

Open it

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.
