Getting Started with ESP32 and MQTT: A Beginner's Guide

January 30, 2026

Getting Started with ESP32 and MQTT: A Beginner's Guide

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.

Prerequisites

  1. An ESP32 development board.
  2. A DHT11 or DHT22 temperature and humidity sensor.
  3. An LED and a 220Ω resistor.
  4. A breadboard and some jumper wires.
  5. The Arduino IDE installed on your computer.
  6. 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.

graph TD subgraph "ESP32 Wiring" ESP32(ESP32 Board) DHT(DHT11/22 Sensor) LED(LED) Resistor(220Ω Resistor) ESP32 -- "3.3V" --> DHT ESP32 -- "GND" --> DHT ESP32 -- "GPIO 4 (D4)" --> DHT ESP32 -- "GPIO 2 (D2)" --> Resistor Resistor --> LED ESP32 -- "GND" --> LED end

Step 2: Arduino IDE Setup

  1. Add ESP32 Board Manager: Add the official Espressif URL in File > Preferences.
  2. Install Libraries: In the Library Manager, search for and install PubSubClient by Nick O'Leary, and the DHT sensor library by 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

  1. Configure & Upload: Fill in your details, select your ESP32 board/port, and upload.
  2. Monitor: Open the Serial Monitor to see the connection status.
  3. Test: Use an MQTT client to subscribe to esp32/temperature and publish 1 or 0 to esp32/led/control.

Congratulations! You have built a basic but fully functional IoT device that integrates seamlessly with the greater MQTT ecosystem.



Frequently Asked Questions (FAQ)