
IoT-Based Smart Home Temperature & Humidity Monitor using ESP32
📌 Introduction
This project demonstrates how to use an ESP32 to measure temperature and humidity using a DHT22 sensor and send the data to an MQTT broker. The collected data can be accessed remotely via a web-based dashboard or a mobile app, making it ideal for applications like smart homes, weather stations, and agriculture monitoring.
🎯 Learning Objectives
By completing this project, you will learn:
- How to interface DHT22 with ESP32.
- How to set up an MQTT broker for IoT communication.
- How to send real-time sensor data to a cloud platform.
- How to visualize sensor data on a web dashboard.
🛠 Required Components & Software
Hardware Components
The project requires an ESP32 Dev Board, which serves as the main microcontroller. A DHT22 sensor is used for measuring temperature and humidity. To ensure stable data communication, a 10kΩ pull-up resistor is connected between the DHT22 data pin and VCC. Jumper wires and a breadboard are used for circuit assembly. The setup is powered by a 5V USB adapter or battery pack.
Software & Libraries
To program the ESP32, the Arduino IDE is used. The ESP32 communicates via MQTT, requiring an MQTT broker such as Mosquitto (local) or Adafruit IO (cloud-based). For visualization, tools like Blynk, Node-RED, or ThingSpeak can be used.
The required libraries are:
- Adafruit DHT Sensor Library for reading sensor data.
- PubSubClient for MQTT communication.
- WiFi.h (built into the ESP32 package) for handling Wi-Fi connectivity.
📖 Background Concepts
ESP32 Overview
ESP32 is a Wi-Fi and Bluetooth-enabled microcontroller with multiple GPIO pins, built-in ADC/DAC, and low power consumption—making it perfect for IoT projects.
MQTT Protocol
MQTT (Message Queuing Telemetry Transport) is a lightweight IoT messaging protocol. It follows a publish/subscribe model where the ESP32 publishes sensor data to an MQTT broker, and other devices can subscribe to receive updates.
DHT22 Sensor
The DHT22 is a digital temperature and humidity sensor with high accuracy (±0.5°C for temperature, ±2% for humidity) and a wide range (-40°C to 80°C for temperature, 0-100% for humidity). It uses a single-wire communication protocol, making it easy to interface with microcontrollers.
🔌 Circuit Diagram & Wiring
Wiring Instructions
- Connect the VCC pin of the DHT22 sensor to the 3.3V pin on the ESP32.
- Connect the GND pin of the DHT22 sensor to the GND pin on the ESP32.
- Connect the DATA pin of the DHT22 sensor to GPIO4 on the ESP32.
- Place a 10kΩ pull-up resistor between the VCC and DATA pins of the DHT22 sensor for stable communication.
📌 Circuit Breakdown:
The ESP32 reads temperature and humidity data from the DHT22 sensor and transmits the data wirelessly via MQTT to a cloud dashboard. The pull-up resistor ensures reliable data transmission.
📜 Step-by-Step Guide
1️⃣ Setting up Arduino IDE for ESP32
- Open Arduino IDE and install the ESP32 Board by adding the following URL to the Boards Manager:
https://dl.espressif.com/dl/package_esp32_index.json - Install the required libraries in Arduino IDE by going to Sketch → Include Library → Manage Libraries and searching for:
- Adafruit DHT Sensor Library
- PubSubClient
- WiFi.h (already included in the ESP32 board package)
2️⃣ Writing the ESP32 Code
Below is the Arduino code to read data from the DHT22 sensor and publish it to an MQTT broker:
#include <WiFi.h>
#include <PubSubClient.h>
#include <DHT.h>
// Wi-Fi Credentials
const char* ssid = "Your_WiFi_SSID";
const char* password = "Your_WiFi_Password";
// MQTT Broker details
const char* mqtt_server = "broker.hivemq.com"; // Public broker
const char* topic = "home/temperature";
// Initialize DHT Sensor
#define DHTPIN 4 // DHT22 data pin connected to GPIO4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// Initialize WiFi and MQTT Client
WiFiClient espClient;
PubSubClient client(espClient);
// Function to connect to WiFi
void setup_wifi() {
delay(10);
Serial.println("Connecting to WiFi...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("\nWiFi Connected!");
}
// Function to connect to MQTT Broker
void reconnect_mqtt() {
while (!client.connected()) {
Serial.println("Connecting to MQTT...");
if (client.connect("ESP32_Client")) {
Serial.println("Connected to MQTT!");
} else {
Serial.print("Failed, retrying in 5 seconds...");
delay(5000);
}
}
}
void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
dht.begin();
}
void loop() {
if (!client.connected()) {
reconnect_mqtt();
}
client.loop();
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
if (!isnan(temperature) && !isnan(humidity)) {
String payload = "{\"temperature\":" + String(temperature) + ", \"humidity\":" + String(humidity) + "}";
Serial.println("Publishing data: " + payload);
client.publish(topic, payload.c_str());
} else {
Serial.println("Failed to read from DHT sensor!");
}
delay(5000);
}
📡 Setting Up the MQTT Broker
Local MQTT Broker (Mosquitto)
To run a local MQTT broker, install Mosquitto MQTT on your PC or Raspberry Pi and start the broker using:
CopyEditmosquitto -v
Cloud MQTT Broker (Adafruit IO)
For a cloud-based solution, sign up at Adafruit IO, create a new feed for home/temperature, and update the ESP32 code with your MQTT credentials.
🔎 Testing & Debugging
- Open Serial Monitor in Arduino IDE to verify data transmission.
- Use an MQTT client such as MQTTX or MQTT Explorer to subscribe to
home/temperatureand view the sensor data. - If the connection fails, check the Wi-Fi credentials, MQTT broker address, and sensor wiring.
🚀 Extensions & Improvements
- Web Dashboard: Integrate Blynk, Node-RED, or ThingSpeak for real-time visualization.
- Alerts & Notifications: Set up email or SMS alerts for extreme temperature conditions.
- Multiple Sensors: Expand the system to include gas, motion, or light sensors.
- Home Automation: Use relays to control devices based on temperature thresholds.
🎉 Conclusion
This project demonstrates how to integrate ESP32 with MQTT, Wi-Fi, and cloud-based dashboards for real-time monitoring. It serves as a foundation for smart home automation, industrial monitoring, and agricultural applications. 🚀