Getting Started with ESP32 and MQTT: A Beginner's Guide
January 30, 2026

Connecting Your ESP32 to the IoT World with MQTT
The ESP32 is a powerful, low-cost microcontroller with integrated Wi-Fi, making it a perfect choice for IoT projects. One of the most effective ways to make your ESP32 project truly "smart" is by connecting it to an MQTT broker. This allows your device to send data and receive commands from other clients.
This guide will walk you through the entire process, from setting up your hardware and software to writing the Arduino code to publish sensor data and control an LED, creating a foundational smart home device.
Project Goal
We will build a simple IoT device using an ESP32 that reads temperature data from a sensor and publishes it to an MQTT topic. We will also subscribe to another topic to control an onboard LED remotely.
Prerequisites
- An ESP32 development board.
- A DHT11 or DHT22 temperature and humidity sensor.
- An LED and a 220Ω resistor.
- A breadboard and some jumper wires.
- The Arduino IDE installed on your computer.
- Access to an MQTT broker (e.g., a public one, or your own broker on a Raspberry Pi).
Step 1: Hardware Setup
The wiring is straightforward. Connect the components on your breadboard as shown in the diagram below.
Step 2: Arduino IDE Setup
- Add ESP32 Board Manager: Add the official Espressif URL in
File > Preferences. - Install Libraries: In the Library Manager, search for and install
PubSubClientby Nick O'Leary, and theDHT sensor libraryby Adafruit.
Step 3: The Arduino Code
Here is the complete code. Replace the placeholder values for your Wi-Fi credentials and MQTT broker details.
#include <WiFi.h>
#include <PubSubClient.h>
#include <DHT.h>
// --- WiFi & MQTT Configuration ---
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
const char* mqtt_server = "broker.hivemq.com"; // Or your private broker
// --- Topics ---
const char* temp_topic = "esp32/temperature";
const char* led_topic = "esp32/led/control";
// --- Hardware Pins ---
#define DHTPIN 4
#define DHTTYPE DHT11
#define LED_PIN 2
WiFiClient espClient;
PubSubClient client(espClient);
DHT dht(DHTPIN, DHTTYPE);
void callback(char* topic, byte* payload, unsigned int length) {
String message;
for (int i = 0; i < length; i++) { message += (char)payload[i]; }
if (String(topic) == led_topic) {
if (message == "1") { digitalWrite(LED_PIN, HIGH); }
else if (message == "0") { digitalWrite(LED_PIN, LOW); }
}
}
void reconnect() {
while (!client.connected()) {
if (client.connect("ESP32Client")) {
client.subscribe(led_topic);
} else {
delay(5000);
}
}
}
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
dht.begin();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) { delay(500); }
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) { reconnect(); }
client.loop();
// Publish temperature every 5 seconds
static unsigned long lastMsg = 0;
if (millis() - lastMsg > 5000) {
lastMsg = millis();
float t = dht.readTemperature();
if (!isnan(t)) {
client.publish(temp_topic, String(t).c_str(), true);
}
}
}
Step 4: Upload and Test
- Configure & Upload: Fill in your details, select your ESP32 board/port, and upload.
- Monitor: Open the Serial Monitor to see the connection status.
- Test: Use an MQTT client to subscribe to
esp32/temperatureand publish1or0toesp32/led/control.
Congratulations! You have built a basic but fully functional IoT device that integrates seamlessly with the greater MQTT ecosystem.