Blog
ESP32 MQTT: Broker, Topics, Home Assistant
Broker or client, topics and payloads, Home Assistant on the other end — MQTT questions from people who just want sensor data somewhere useful. The free ESP32 simulator lets you walk the whole loop before hardware arrives.
Can You Use ESP32 as MQTT Broker?
Technically yes, practically no. Small brokers like uMQTTBroker compile for the ESP32 and move messages between two nodes on your desk, but the chip has ~520KB of RAM, no disk, and one radio — it stops being a broker the moment it reboots. A Pi running Mosquitto is more reliable for the same money. Fine for a classroom demo; not for anything with uptime.
What Is Best MQTT Library for ESP32?
PubSubClient for Arduino beginners — tiny, stable, everywhere. esp-mqtt ships inside the Arduino core and adds TLS plus built-in reconnect; AsyncMqttClient is the pick when loop() must never block. Best depends on your board: 4MB-flash C3 boards stay on PubSubClient, anything needing mTLS goes esp-mqtt.
How to ESP32 MQTT?
Five steps: run a broker, install PubSubClient, connect with a unique client id, subscribe, publish. The canonical sketch is about 40 lines — WiFi.begin(), mqtt.connect(), mqtt.loop(), done. Watch from the terminal: mosquitto_sub -t ‘esp32/#’ shows every message. I’ve run sensor nodes on this pattern for years; it never changed.
What MQTT Topics Should I Use?
Flat and specific: esp32/living-room/temperature beats livingroomtemp. Keep one base topic per device, use + and # wildcards only in subscribers, and keep payloads as plain JSON. Precise topics mean fewer wildcard collisions later.
Which MQTT Broker Should I Use?
Mosquitto for local, HiveMQ Cloud’s free tier for hosted tests, broker.hivemq.com when you want zero signup. Local wins on latency and privacy, cloud wins on reachability from anywhere. Broker limits are rarely the constraint — a Pi handles dozens of ESP32s — though the official docs stay vague about exact numbers. Our KB logs broker setups per board.
Why Does My ESP32 Keep Disconnecting from MQTT?
Keepalive, power, or a client-id clash, in that order. Miss the keepalive window and the broker kills the session; WiFi drops mid-transmit miss keepalives — the WiFi FAQ covers that side of the link. A second node reusing an id produces Mosquitto’s classic “Client <id> already connected, closing old connection”. Fix with unique ids, a solid 5V supply, and reconnect logic with backoff. Every disconnect I chased on my six-node mesh came down to power or a duplicate id.
How to Connect ESP32 to Home Assistant via MQTT?
Install the MQTT integration, point it at your broker, and let discovery do the work. Publish a config topic like homeassistant/sensor/esp32_temp/config and the entity appears in HA automatically — no YAML edits. ESPHome does this for you; the manual MQTT path is the flexible route.
MQTT or HTTP for My Project?
MQTT for streams and two-way traffic, HTTP for request-response. A sensor that wakes, sends a reading, and sleeps is simpler with one HTTP POST; a device that must receive commands while publishing state wants MQTT instead.
How Much Bandwidth Does MQTT Use on an ESP32?
Negligible. A QoS 0 message with a 30-byte payload is roughly 100 bytes on the wire, so thousands of messages a day are nothing to a WiFi link. QoS 1 roughly doubles that with acks; keep QoS 2 for the rare exactly-once cases. Battery devices should care about radio-on time, not bytes.
Is MQTT on ESP32 Secure?
Not by default — plaintext on port 1883, and anyone on the same WiFi can read your topics. Usernames and passwords stop casual listeners; TLS on 8883 stops everyone else at the cost of flash space. Plaintext on a home LAN is a choice that works; the moment a device reaches the internet, enable TLS.
No Broker Emulated — but the Local Pattern Runs
Honest scope: the simulator does not emulate an MQTT broker. What it does run is the firmware pattern most MQTT projects wrap around — a device that joins Wi-Fi and serves its state locally: ESP32 HTTP server (with a page you can actually open) and Wi-Fi connect. Prototype the device logic here; add the broker on real hardware where this FAQ’s guidance applies.