Close Menu
Circuit of Things
  • Home
  • Projects
    • Raspberry Pi Projects
    • Arduino Projects
    • ESP8266 Projects
    • ESP32 Projects
    • IoT Tutorials
    • Sensors & Modules
    • IoT Basics
  • About Us
  • Get In Touch
  • 3D Print
  • Shop Now !
  • Electro Calc

Stay in the Loop!

Subscribe to get the latest IoT tutorials, Arduino projects, and electronics guides delivered straight to your inbox.

Instagram WhatsApp
  • Terms and Conditions
  • Disclaimer
  • Privacy Policy
Circuit of Things Circuit of Things
  • Home
  • Projects
    • Raspberry Pi Projects
    • Arduino Projects
    • ESP8266 Projects
    • ESP32 Projects
    • IoT Tutorials
    • Sensors & Modules
    • IoT Basics
  • About Us
  • Get In Touch
  • 3D Print
  • Shop Now !
  • Electro Calc
Hire us
Circuit of Things
Home»ESP32 Projects»ESP32 MQTT Tutorial: Publish and Subscribe with HiveMQ
ESP32 Projects

ESP32 MQTT Tutorial: Publish and Subscribe with HiveMQ

Sai Preetham KoyyalaBy Sai Preetham KoyyalaMay 4, 2026No Comments2 Mins Read
Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
Share
Facebook Twitter LinkedIn Pinterest Email
Contents
  • Introduction
  • MQTT Architecture
  • Required Libraries
  • Complete Code
  • Testing
  • Conclusion

Introduction

MQTT is the backbone of modern IoT communication. It is a lightweight publish-subscribe protocol perfect for the ESP32. In this tutorial we connect to a free public broker, publish sensor data, and receive control commands.

MQTT Architecture

  • Broker: Central server routing messages (e.g. broker.hivemq.com).
  • Publisher: ESP32 sending sensor readings to a topic.
  • Subscriber: Dashboard or phone receiving the data.

Required Libraries

Install via Arduino IDE Library Manager: PubSubClient by Nick O’Leary and DHT sensor library by Adafruit.

Complete Code

#include <WiFi.h>
#include <PubSubClient.h>
#include <DHT.h>
#define DHTPIN 4
#define DHTTYPE DHT11
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
const char* mqtt_server = "broker.hivemq.com";
WiFiClient espClient;
PubSubClient client(espClient);
DHT dht(DHTPIN, DHTTYPE);
void callback(char* topic, byte* payload, unsigned int len) {
  String msg = ""; for (int i=0;i<len;i++) msg+=(char)payload[i];
  Serial.println("Received: " + msg);
}
void reconnect() {
  while (!client.connected()) {
    if (client.connect("ESP32Client")) { client.subscribe("home/control"); }
    else delay(5000);
  }
}
void setup() {
  Serial.begin(115200); 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();
  float t = dht.readTemperature(), h = dht.readHumidity();
  if (!isnan(t)) {
    String p = "{\"t\":" + String(t) + ",\"h\":" + String(h) + "}";
    client.publish("home/sensors", p.c_str());
  }
  delay(5000);
}

Testing

Download MQTT Explorer, connect to broker.hivemq.com:1883 and subscribe to home/sensors. You will see live readings every 5 seconds!

Conclusion

MQTT is the most efficient IoT protocol. Once mastered, you can connect dozens of sensors feeding into Node-RED or Home Assistant dashboards in real time.

Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
Previous ArticleHow to Connect ESP32 to Wi-Fi: Complete Beginner Guide
Next Article HC-SR04 Ultrasonic Sensor Arduino: Distance Measurement and Projects
Sai Preetham Koyyala
  • Website
  • Facebook
  • X (Twitter)
  • Instagram
  • LinkedIn

Hello, I am Sai Preetham Koyyala. I'm an electronics engineer by profession, a DIY enthusiast by passion. ESP32 and Arduino are the main topics of my work.

Related Posts

ESP8266 Projects

NodeMCU ESP8266 Sensor Data to ThingSpeak IoT Platform

May 4, 2026
ESP32 Projects

ESP32 Firebase Realtime Database: Send and Receive Data from the Cloud

May 4, 2026
ESP32 Projects

ESP32 OTA Updates: Program Your Board Over Wi-Fi with Arduino IDE

May 4, 2026
Add A Comment
Leave A Reply Cancel Reply


KSP Electronics
✓ Trusted Store
Everything for Your
Next IoT Build
Boards, sensors, modules & kits delivered fast across India.
ESP32 Arduino Sensors Modules DIY Kits
500+
Products
Fast
Delivery
★★★★★
Rated
🛒 Shop Now →
kspelectronics.in
💻
● Available for Projects
Hire Me for
IoT Projects
ESP32, LoRaWAN, cloud dashboards & custom hardware — end-to-end IoT solutions for your needs.
ESP32 LoRaWAN MQTT Arduino Cloud PCB Design
3+
Yrs Exp
20+
Projects
★★★★★
Rated
💬 Chat on WhatsApp
View services →
Top Posts

Raspberry Pi: How to Control a DC Motor with L298N and PWN on a Web Server

July 14, 2023

Relay Module with Arduino: Control High Voltage Appliances Safely

May 4, 2026

Esp8266 / NodeMCU Pinout: A Comprehensive Guide for Beginners

June 25, 2023
Follow Us
  • Telegram

Stay in the Loop!

Subscribe to get the latest IoT tutorials, Arduino projects, and electronics guides delivered straight to your inbox.

Most Popular

Raspberry Pi: How to Control a DC Motor with L298N and PWN on a Web Server

July 14, 2023

Relay Module with Arduino: Control High Voltage Appliances Safely

May 4, 2026

Esp8266 / NodeMCU Pinout: A Comprehensive Guide for Beginners

June 25, 2023
Our Picks

Relay Module with Arduino: Control High Voltage Appliances Safely

May 4, 2026

NodeMCU ESP8266 Sensor Data to ThingSpeak IoT Platform

May 4, 2026

ESP32 Firebase Realtime Database: Send and Receive Data from the Cloud

May 4, 2026

Stay in the Loop!

Subscribe to get the latest IoT tutorials, Arduino projects, and electronics guides delivered straight to your inbox.

© 2026 Circuit of Things. All Rights Reserved. Built with ❤️ for the Maker Community.

  • About
  • Privacy
X (Twitter) Instagram YouTube WhatsApp
  • Book an Appointment
  • My Bookings
  • Support Us
© 2026 Circuit of Things. Designed by Sai Preetham Koyyala.

Type above and press Enter to search. Press Esc to cancel.