What Is MQTT? Wiring and Errors (2026 Guide)

What Is MQTT? Wiring and Errors (2026 Guide)

MQTT is a lightweight publish/subscribe messaging protocol — the standard way IoT devices talk to each other and to dashboards. Instead of devices connecting to each other directly, everything connects to a central broker, and messages are routed by topics like home/livingroom/temperature.

Quick answer: A device publishes to a topic, and anything subscribed to that topic receives it — the publisher never needs to know who's listening. On an ESP32, connect to WiFi, use the PubSubClient library to reach a broker (free options include test.mosquitto.org), then client.publish("home/temp", value) and the broker fans it out to every subscriber.

Proof of work: the explanation and code below are Soldr's actual answer to "What is MQTT and how do I use it?", asked through Compoden's AI build assistant on 11 August 2026. Published 11 August 2026 · Last updated 11 August 2026.

Soldr's answer explaining MQTT: broker, topics, publish/subscribe, and a PubSubClient sketch reading a DHT sensor
Soldr's build log answering "What is MQTT and how do I use it?" — the publish/subscribe explanation and a PubSubClient sketch generated in the same session.

How does publish/subscribe actually decouple devices?

The publisher never connects to the subscriber directly — it publishes to a topic, and the broker delivers to whoever is subscribed, however many devices that is. Your ESP32 can publish sensor data, and your phone app, a dashboard, or another ESP32 can all subscribe independently, with no code on the publisher's side knowing or caring who's listening. Messages also carry a QoS level (0-2) that controls how hard the broker tries to guarantee delivery.

MQTT at a glance
Property Value
Model Publish/subscribe via a central broker
Routing By topic string, e.g. home/livingroom/temperature
Arduino library PubSubClient (Arduino Library Manager)
Free brokers for learning test.mosquitto.org, HiveMQ public broker, Adafruit IO
Publish call client.publish("topic", value)

How do you publish a sensor reading from an ESP32 over MQTT?

Pick a broker, connect the ESP32 to WiFi, then use PubSubClient to connect to the broker, subscribe to topics, and publish. This is the exact setup from Soldr's session:

#include <WiFi.h>
#include <PubSubClient.h>
#include <DHT.h>

#define DHT_PIN 4
#define DHT_TYPE DHT22
DHT dht(DHT_PIN, DHT_TYPE);

const char* ssid = "your-wifi";
const char* pass = "your-password";
const char* mqtt_server = "test.mosquitto.org";

WiFiClient espClient;
PubSubClient client(espClient);

void setup() {
  Serial.begin(115200);
}

The includes, defines, and the start of setup() are verbatim from the session's Firmware tab; the WiFi/MQTT connect calls and the publish loop were outside the captured window, so they aren't reproduced here as if captured. The pattern the session's own prose describes is: read the sensor, then client.publish("home/temp", value), and the broker fans it out to every subscriber.

Why does my ESP32 never publish anything to the broker?

The most common cause is calling client.publish() before the MQTT connection is actually established — client.connect() has to succeed first, and a dropped WiFi or broker connection needs to be detected and reconnected, not assumed permanent. A second common issue is a topic string typo between publisher and subscriber — MQTT topics are case-sensitive and exact-match by default, so home/temp and Home/Temp are different topics entirely.

What do you need to try this yourself?

An ESP32 30-Pin Development Board (CP2102, WiFi/Bluetooth) (Rs.400) for WiFi built in, and a DHT11 Temperature Humidity Sensor (Rs.45) on GPIO4 for something real to publish. Soldr's session named a DHT22 specifically, but Compoden doesn't currently stock a standalone DHT22 — the DHT11 uses the exact same DHT library and wiring, just with DHT_TYPE set to DHT11 instead.

Frequently asked questions

MQTT broker kya hota hai?

Broker ek central server hai jo saare MQTT messages ko route karta hai. Devices direct ek dusre se connect nahi hote - har device broker se connect hota hai, aur broker hi decide karta hai ki kaunsa message kis subscriber tak pahunchana hai, topic ke naam ke hisaab se.

Is test.mosquitto.org safe to use for a real project?

It's a public broker meant for testing and learning, not production - anyone can publish or subscribe to any topic on it, so don't send sensitive data there. For a real project, run your own Mosquitto broker (even on a Raspberry Pi) or use a managed service like HiveMQ Cloud or Adafruit IO with authentication.

What is the price of an ESP32 and DHT sensor for MQTT in India?

An ESP32 30-Pin Development Board (CP2102, WiFi/Bluetooth) costs Rs.400 and a DHT11 Temperature Humidity Sensor costs Rs.45 at Compoden, both with cash on delivery available across India - Rs.445 total. Prices checked 11 August 2026.

What does the QoS level in MQTT actually control?

QoS 0 sends a message at most once with no confirmation - fastest, least guaranteed. QoS 1 guarantees at-least-once delivery but can deliver duplicates. QoS 2 guarantees exactly-once delivery but is the slowest. Most sensor-reading use cases are fine with QoS 0 or 1; QoS 2 is reserved for cases where a duplicate message would actually cause harm.

Back to blog