
IoT-Based Smart Home Temperature & Humidity Monitoring System Using ESP32
Introduction
This project builds a real-time temperature and humidity monitoring system using the ESP32 and a DHT22 sensor. The ESP32 connects to Wi-Fi and sends sensor data to an IoT dashboard (such as Blynk or ThingsBoard) for remote monitoring. This project is ideal for smart home automation and environmental monitoring applications.
Learning Objectives
By completing this project, you will:
- Learn how to interface a DHT22 sensor with ESP32.
- Understand IoT data transmission using Wi-Fi.
- Set up a real-time IoT dashboard.
- Work with MQTT or HTTP protocols.
- Learn ESP32 power management techniques.
Tools and Components
Hardware Required:
- ESP32 Development Board
- DHT22 Temperature & Humidity Sensor
- 10kΩ Pull-up Resistor
- 16×2 I2C LCD Display (Optional)
- Power Supply (5V via USB or battery)
- Jumper Wires
- Breadboard
Software Required:
- Arduino IDE with ESP32 board support
- IoT platform (Blynk or ThingsBoard)
- MQTT broker (Mosquitto or public broker)
- Required libraries: DHT sensor library, WiFi, PubSubClient (for MQTT)
Background & Theory
ESP32 Overview:
The ESP32 is a Wi-Fi and Bluetooth-enabled microcontroller with multiple GPIOs, ADCs, and power-efficient modes. It is widely used in IoT applications due to its built-in Wi-Fi module.
DHT22 Sensor Overview:
The DHT22 sensor measures temperature and humidity using a capacitive sensor and a thermistor. It communicates digitally with microcontrollers using a one-wire protocol.
IoT Data Transmission:
Data can be sent to the cloud using:
- MQTT (Message Queuing Telemetry Transport): A lightweight protocol widely used in IoT.
- HTTP POST Requests: Used for sending data to web servers or cloud platforms.
Circuit Design & Wiring
ESP32 to DHT22 Sensor Connections:
- Connect the VCC pin of the DHT22 to the 3.3V pin on the ESP32.
- Connect the GND pin of the DHT22 to GND on the ESP32.
- Connect the Data pin of the DHT22 to GPIO4 on the ESP32.
- Place a 10kΩ pull-up resistor between the VCC and Data pin of the DHT22.
Optional: Connecting a 16×2 I2C LCD Display
- Connect the SDA pin of the LCD to GPIO21 on the ESP32.
- Connect the SCL pin of the LCD to GPIO22 on the ESP32.
- Connect the GND and VCC of the LCD to the GND and 5V of the ESP32.
Step-by-Step Implementation
Step 1: Setting up ESP32 in Arduino IDE
- Open Arduino IDE and install ESP32 board support.
- Install the necessary libraries:
DHT.hfor sensor communicationAdafruit_Sensor.hfor sensor handlingWiFi.hfor Wi-Fi connectivityPubSubClient.hfor MQTT communication
Step 2: Coding the ESP32 for IoT Data Transmission
#include <WiFi.h>
#include <PubSubClient.h>
#include "DHT.h"
#define DHTPIN 4 // DHT22 data pin connected to GPIO4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
const char* ssid = "Your_SSID"; // WiFi Network
const char* password = "Your_Password";
const char* mqtt_server = "broker.hivemq.com"; // Public MQTT broker
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
client.setServer(mqtt_server, 1883);
dht.begin();
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
String tempString = String(temperature);
String humString = String(humidity);
client.publish("home/temperature", tempString.c_str());
client.publish("home/humidity", humString.c_str());
Serial.print("Temperature: "); Serial.println(tempString);
Serial.print("Humidity: "); Serial.println(humString);
delay(5000);
}
void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect("ESP32_Client")) {
Serial.println("Connected to MQTT Broker");
} else {
Serial.print("Failed, rc=");
Serial.print(client.state());
Serial.println(" Retrying in 5 seconds...");
delay(5000);
}
}
}
Step 3: IoT Dashboard Setup
Option 1: Using Blynk
- Install the Blynk app on your phone.
- Create a new project and select ESP32 as the device.
- Add “Gauge” widgets to display temperature and humidity.
- Link the ESP32 with the Blynk Authentication Token.
Option 2: Using ThingsBoard
- Sign up on ThingsBoard.io.
- Create a new device and generate an Access Token.
- Use MQTT or HTTP requests to send data.
Testing & Debugging
- Open the Serial Monitor to check ESP32’s connection status.
- If Wi-Fi fails to connect, verify the SSID & Password settings.
- If no sensor data is received, check the DHT22 wiring and the pull-up resistor.
- If MQTT disconnects frequently, try a different MQTT broker.
Extensions & Enhancements
- Add a Relay Module to control home appliances remotely.
- Use an OLED Display to show temperature locally.
- Deploy on Battery and implement deep sleep mode to save power.
- Send Alerts via IFTTT or Telegram Bot when temperature exceeds a threshold.
Conclusion
This project demonstrates how to use ESP32 for real-time IoT-based temperature and humidity monitoring. By integrating MQTT and a cloud dashboard, users can remotely access environmental data. This concept can be expanded to smart home automation, greenhouse monitoring, and industrial IoT applications.
Would you like to integrate Firebase for cloud storage or control devices based on sensor data? 🚀