Demo 14: How to use MQTT and Arduino ESP32 to build a simple Smart home system

1. Introduction
- Currently, there are many IoT protocols such as: CoAP, MQTT, AMQP, … In this tutorial, I will introduce MQTT, one of the famous IoT protocols. This protocol is to control and transfer data between devices in an IoT network
Note: for MQTTS please refer Demo 30: How to use Arduino ESP32 MQTTS with MQTTS Mosquitto broker (TLS/SSL).
MQTT is stand for Message Queuing Telemetry Transport. It has some features:
+    Use Publish/Subscribe/Topic mechanism
+    Lightweight protocol
+    Small code footprint
+    Build on top of the TCP/IP protocol
+    Less network bandwidth.
- The principal of MQTT is traditional Client-Server model. In this model, there is one MQTT Server (also called Broker) and many MQTT Clients. The MQTT Clients always keep connection with MQTT Server. The role of MQTT Server (broker) is to filter and forward the messages to subscribed MQTT Clients. The communication between clients is based on Publish/Subscribe/Topic pattern in which:
+ Message: has a topic.
+ Publish: sending the messages to network.
+ Subscribe: listening messages that contain topic that the client is interested in.
+ Broker: coordinating the communication between publishers and subscribers.
- Topic is an utf-8 string and has one - many levels which is separated by splash "/". For example: "floor1/room1/temp": this topic has 3 levels, human readable and easy to understand (we have floor 1 and in room 1 with temperature sensor). You can refer:
http://www.hivemq.com/blog/mqtt-essentials-part-5-mqtt-topics-best-practices
- Beside that, there are other concept that you need to know:
*QoS (Quality of Service): this indicator perform the guaranty of message exchange between sender and receiver. There are 3 levels:
+ QoS 0 - at most once (this level is the fastest, but not reliable)
+ QoS 1 - at least once (this is the default mode)
+ QoS 2 - exactly once (this level is the most reliable, but slowest)
You can refer at: 
*Retained Messages:  broker will keep the sent message so that when there is new subscriber that subscribe the topic that matches the retained message then that message will be sent to that subscriber.
- Most of MQTT libraries define some standard methods such as:
+    Connect(): connect to MQTT server.
+    Disconnect(): disconnect from MQTT server.
+    Subscribe(): subscribe a topic with MQTT server.
+    UnSubscribe(): unsubscribe a topic with MQTT server
+    Publish(): client publish a topic to network.
- For demo, we create a simple smart home network that have 3 client nodes (Smart phone, WiFi MCU with temperature sensor, WiFi MCU with LED/bulb controller) and 1 server node as a broker (PC or Raspberry Pi). In our application, we want to use smart phone to monitor the temperature and control the LED/bulb on or off.  So we design the MQTT model like below:
Figure: MQTT model for simple smart home application
2. Hardware
- To implement the model above, I will collect Node2 and Node3 into one node and this node is our ESP32 with DHT22 sensor and LED (bulb). Finally, we have 2 nodes: SM node and ESP32 node. We re-use the hardware schematic of Demo 13: How to display temperature/humidity using Google Chart/Jquery and control LED through Arduino ESP32 Web Server. In this demo we do not use microSD so please ignore it.
- Connections:
Connect VCC and GND of DHT22 (VCC=3.3V) to VCC (Vcc=3.3V) and GND of ESP32.
[ESP32 IO15 - DHT22 DATA]
[ESP32 IO2 - LED ANODE]
[ESP32 GND - LED CATHODE]
Figure: esp32 + dht22 + LED for MQTT smart home demo
3. Software
3.1 MQTT Client side

SM node: I will use an Android Smartphone with a MQTT client application (IoT MQTT Dashboard) that is available on Google Play. You can download it here:
https://play.google.com/store/apps/details?id=com.thn.iotmqttdashboard
ESP2 node: I will use a MQTT client library (Pubsubclient). You can download it here:
https://github.com/knolleary/pubsubclient
Then unzip the downloaded file and copy it to Arduino/libraries folder.
- This library supports some standard functions that are mentioned above. To use these function we create an instance PubSubClient client(wifiClient). Because MQTT is built on top of the TCP/IP protocol so the input of this constructor is a TCP WiFiClient object.
3.2 MQTT server side
- I will use a popular MQTT server called Mosquito. You can download and install it here:
- Windows user
http://www.eclipse.org/downloads/download.php?file=/mosquitto/binary/win32/mosquitto-1.4.11-install-win32.exe
After finishing, from command line just run this command to start mosquito server: "mosquitto"
-  Ubuntu user:
From command line type the command below: 
sudo apt-get install mosquitto mosquitto-clients
This will install mosquito as a service. You can check whether the service is start or not by using command line: 
sudo service --status-all 2>&1 | grep mosquitto
-  Other OS, just follow:  https://mosquitto.org/download/
Note: when we install mosquito, it also install 2 client programs called "mosquitt_sub" and "mosquito_pub" that we can use for debugging. 
For example:
- To monitor all topic on network using:
mosquitto_sub -v -h broker_ip -p 1883 -t "#" (change broker_ip to mqtt server ip)
- To publish a topic (publish topic room1/temp with value 30) using:
mosquitto_pub -t 'room1/temp' -m 30
 3.3 Assign roles
We define topics: 
-  Topic1: smarthome/room1/bulb #value : value can take 0 or 1 means on/off the LED (bulb).
- Topic2: smarthome/room1/temperature #value : value can take float number to express temperature.
Example: smarthome/room1/temperature 30 
With these topics SM node can subscribe Topic2 and publish Topic1. ESP32 node can publish Topic2 and subscribe Topic1.
3.4 Steps to run the system
- Start the MQTT server (on Wins invoke it manually, on Linux it is a service so just check the service is started)
- From Terminal run this: mosquitto_sub -v -h broker_ip -p 1883 -t '#' for debugging. You will see all the messages on the network.
- Create an Arduino project and Save as esp32mqtt with code:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* Here ESP32 will keep 2 roles: 
1/ read data from DHT11/DHT22 sensor
2/ control led on-off
So it willpublish temperature topic and scribe topic bulb on/off
*/

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

/* change it with your ssid-password */
const char* ssid = "dd-wrt";
const char* password = "0000000000";
/* this is the IP of PC/raspberry where you installed MQTT Server 
on Wins use "ipconfig" 
on Linux use "ifconfig" to get its IP address */
const char* mqtt_server = "192.168.1.103";

/* define DHT pins */
#define DHTPIN 14
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
float temperature = 0;

/* create an instance of PubSubClient client */
WiFiClient espClient;
PubSubClient client(espClient);

/*LED GPIO pin*/
const char led = 12;

/* topics */
#define TEMP_TOPIC    "smarthome/room1/temp"
#define LED_TOPIC     "smarthome/room1/led" /* 1=on, 0=off */

long lastMsg = 0;
char msg[20];

void receivedCallback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message received: ");
  Serial.println(topic);

  Serial.print("payload: ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();
  /* we got '1' -> on */
  if ((char)payload[0] == '1') {
    digitalWrite(led, HIGH); 
  } else {
    /* we got '0' -> on */
    digitalWrite(led, LOW);
  }

}

void mqttconnect() {
  /* Loop until reconnected */
  while (!client.connected()) {
    Serial.print("MQTT connecting ...");
    /* client ID */
    String clientId = "ESP32Client";
    /* connect now */
    if (client.connect(clientId.c_str())) {
      Serial.println("connected");
      /* subscribe topic with default QoS 0*/
      client.subscribe(LED_TOPIC);
    } else {
      Serial.print("failed, status code =");
      Serial.print(client.state());
      Serial.println("try again in 5 seconds");
      /* Wait 5 seconds before retrying */
      delay(5000);
    }
  }
}

void setup() {
  Serial.begin(115200);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  /* set led as output to control led on-off */
  pinMode(led, OUTPUT);

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  /* configure the MQTT server with IPaddress and port */
  client.setServer(mqtt_server, 1883);
  /* this receivedCallback function will be invoked 
  when client received subscribed topic */
  client.setCallback(receivedCallback);
  /*start DHT sensor */
  dht.begin();
}
void loop() {
  /* if client was disconnected then try to reconnect again */
  if (!client.connected()) {
    mqttconnect();
  }
  /* this function will listen for incomming 
  subscribed topic-process-invoke receivedCallback */
  client.loop();
  /* we measure temperature every 3 secs
  we count until 3 secs reached to avoid blocking program if using delay()*/
  long now = millis();
  if (now - lastMsg > 3000) {
    lastMsg = now;
    /* read DHT11/DHT22 sensor and convert to string */
    temperature = dht.readTemperature();
    if (!isnan(temperature)) {
      snprintf (msg, 20, "%lf", temperature);
      /* publish the message */
      client.publish(TEMP_TOPIC, msg);
    }
  }
}
- From Android smart phone, open IoT MQTT Dashboard) and follow steps below to set up it a MQTT client:
Figure: Configure MQTT Server that it will connect to
Figure: After configuring server, choose it 
Figure: Choose Subscribe tab and create topic temp
 Figure: Choose Publish tab to create a Switch for toogling LED
 Figure: Fill topic led for Switch
Figure: After finishing, here is the GUI for Publish tab, one Switch
 Figure: You can see smart phone received temp topic 
4. Result 

Post a Comment

228 Comments

sportyjacket said…
Great guide on using MQTT with Arduino ESP32 for smart home projects! I was reading while wearing my max verstappen jacket—felt inspired to automate my lights and coffee machine.
belly said…
Great post! If you’re looking for an engaging live video call experience, Eloelo offers real-time group video and audio chatrooms, exciting multiplayer games like Ludo, Tambola. With 100M+ downloads and full multilingual support, it’s the perfect way to connect, play, and interact anytime.
Jason Todd said…
If you’re in the UK and looking for elegance, Italian Living Style offers the finest Italian sofa set. Their designs perfectly balance modern comfort with timeless luxury—I couldn’t be happier with my choice.
Jason Todd said…
Cash and Carry Beds offers some of the best Italian cabinet in the UK. The attention to detail and premium finishes truly make these cabinets stand out in any living space.
Jason Todd said…
If you’re in the United States and want to elevate your brand identity, WBSoft Tech’s Stationery Design Services are top-notch and highly reliable.
Jason Todd said…
Innova Designz offers top-notch 2D Character Art Services that perfectly blend imagination with professionalism. If you’re in the United States and need eye-catching characters, this team is highly recommended!
Jason Todd said…
Shaffer has truly elevated men’s fashion with their Stitched Kurta collection. I recently bought one while in the UK, and it gave me the authentic elegance of Pakistan’s traditional wear. The comfort and craftsmanship are top-notch.
Jason Todd said…
Grace Fabrics truly delivers premium mens unstitched fabric. I’ve shopped in the UK, but their styles and textures stand out compared to many local brands. A must-visit for anyone who values elegance and comfort.
Daniyal Taqi said…
Great tutorial on using MQTT with Arduino ESP32 for a smart home system! Very clear explanation of topics, QoS, and client-server communication. I usually explore IoT and gaming side by side—if anyone is interested, you can also check out the latest Minecraft updates and APK downloads here: https://minecraft-apk.tr/
rewrwerewr said…
This demo really simplifies how MQTT works with Arduino ESP32 in smart home projects. The breakdown of publish/subscribe and real hardware setup is very useful. Alongside my IoT interest, I also run a blog about gaming—if you’re into Minecraft, feel free to explore the latest APK and updates here: https://minecraft-apk.tr/
rewrwerewr said…
Minecraft son sürüm, oyunculara heyecan verici yeni özellikler ve geliştirmeler sunuyor. Bu güncelleme ile oyunun grafikleri daha akıcı hale gelirken, yeni yaratıklar, bloklar ve biyomlar da eklendi. Minecraft tutkunları için keşfetmeye değer birçok yenilik barındıran bu sürüm, hem tek oyunculu hem de çok oyunculu modlarda daha zengin bir deneyim sağlıyor. Eğer hâlâ denemediyseniz, Minecraft son sürüm mutlaka göz atmanız gereken bir güncelleme!
apk hub said…
**APK Hub** is a popular platform where users can download the latest Android apps, games, and modded APK files in one place. It provides a wide collection of trending apps, premium tools, entertainment apps, and games for free, making it a go-to source for Android users. The platform ensures fast downloads, regular updates, and secure APK files, allowing users to enjoy the newest features without any hassle. Whether you're looking for productivity apps, social media mods, or gaming APKs, **APK Hub** offers a convenient and reliable solution for all your Android app needs.
Minecraft apk said…
**Minecraft APK** is the Android version of the popular sandbox game developed by Mojang Studios, allowing players to explore, build, and survive in a blocky, open-world environment right on their mobile devices. With the APK, you can enjoy various game modes like **Survival**, **Creative**, **Adventure**, and **Spectator**, giving you endless possibilities to create and explore. It offers smooth performance, multiplayer support, regular updates, and customization options through skins, mods, and texture packs. Whether you want to craft tools, build structures, or fight mobs, **Minecraft APK** delivers a fun and immersive experience for Android users.
Albeerto said…
Fill up your resume with in-demand data skills: Python programming, NumPy, pandas, data preparation - data collection, data cleaning, data preprocessing, data visualization; data analysis, data analytics
Acquire a big picture understanding of the data analyst role
Learn beginner and advanced Python
Study mathematics for Python
minecraft 1.21.110
We will teach you NumPy and pandas, basics and advanced
Be able to work with text files
Understand different data types and their memory usage
Albeerto said…
Most data analyst, data science, and coding courses miss a critical practical step. They don’t teach you how to work with raw data, how to clean, and preprocess it. This creates a sizeable gap between the skills you need on the job and the abilities you have acquired in training. Truth be told, real-world data is messy, so you need to know how to overcome this obstacle to become an independent data professional. minecraft v1.17.200 apk multiplayer cracked and enjoy free online gameplay without server limits.
August 6, 2025 at 5:0
Albeerto said…
The AWS Certified Data Engineer Associate Exam (DEA-C01 or DE1-C01) is one of the most challenging associate-level certification exams you can take from Amazon Web Services, and even among the most challenging overall. Passing it tells employers in no uncertain terms that your knowledge of data pipelines is wide and deep. But, even experienced technologists need to prepare heavily for this exam. This course sets you up for success, by covering all of the data ingestion, transformation, and orchestration technologies on the exam and how they fit together.

Best-selling Udemy instructors Frank Kane and Stéphane Maarek have teamed up to deliver the most comprehensive and hands-on prep course we've seen. Together, they've taught over 4 million people around the world. This course combines Stéphane's depth on AWS with Frank's experience in wrangling massive data sets, gleaned during his 9-year career at Amazon itself.

The world of data engineering on AWS includes a dizzying array of technologies and services. Just a sampling of the topics we cover in-depth are nail art designs
Albeerto said…
Are you ready to embark on a rewarding career as a Data Analyst? Whether you're a beginner or an experienced professional looking to enhance your skills, this Complete Data Analyst Bootcamp is your one-stop solution. This course is meticulously designed to equip you with all the essential tools and techniques needed to excel in the field of data analysis.

What You Will Learn:

Python Programming for Data Analysis
Dive into Python, the most popular programming language in data science. You'll learn the basics, including data types, control structures, and how to manipulate data with powerful libraries like Pandas and NumPy. nail art designs
Tagsen said…
Fantastic post! It was a pleasure to read, and you’ve shared it beautifully. I’ll keep your blog on my list to revisit later. Do get in touch with us if you want tailored umbrella with logo printing​​​.
Albeerto said…
download minecraft versi 1.17.1 mod apk​ This web page requires data that you entered earlier in order to be properly displayed. You can send this data again, but by doing so you will repeat any action this page previously performed.
Albeerto said…
Job Title: SEO Expert
Location: Multan (Office-Based Job)
Experience Required: 6 Months to 2 Years

Key Responsibilities:
Perform On-Page and Off-Page SEO to improve rankings and visibility.
Conduct keyword research and competitor analysis to identify growth opportunities.
Manage and monitor websites using Google Search Console and Google Analytics.
Work with SEO tools like Ahrefs, SEMrush, and others to track performance.
Develop and implement link-building strategies.
Optimize content to improve search engine performance and user engagement.download minecraft versi 1.17.1 mod apk​
Prepare and analyze performance reports to guide strategy.
hina altaf said…
I simply love online game platforms, and 1jj game appears to be a great choice. Users can make real money and enjoy a seamless experience by accessing the official website.
lisa lim said…
P999 game official website is a wonderful destination for those interested in playing thrilling games. It provides entertainment, trustworthiness, and genuine earning possibilities for serious gamblers.
Aleena amy said…
If you are on the lookout for an actual money gaming site, seven game is a place to go. The official website gives the users a secure, thrilling, and rewarding experience every single day.
Easy Careers said…
When deadlines approach, matlab homework help ensures students complete programming projects and simulations accurately. For writing tasks, essaypro provides professional essays and assignment support. Both platforms focus on quality, reliability, and timely delivery. They save time and reduce stress for students. Get expert online help today and improve academic performance with ease.
Anonymous said…
Cool off with the Jack in the Box Oreo Cookie Ice Cream, creamy vanilla blended with crunchy Oreo cookie pieces.
ive said…
Unlock exclusive features with the MagisTV Live Premium TV App.
Oldest Older 201 – 228 of 228