
IoT Project: Smart Home Temperature and Humidity Monitoring System Using ESP32
1. Introduction
This project demonstrates how to build a Smart Home Temperature and Humidity Monitoring System using the ESP32 microcontroller. The system reads temperature and humidity data using a DHT22 sensor and sends the data to a cloud server via Wi-Fi, enabling remote monitoring through a mobile app or web interface. This system can be expanded to include alerts and integration with smart home systems.
Real-world applications:
- Monitoring greenhouse environments.
- Home climate monitoring and HVAC optimization.
- Data collection for IoT-enabled smart devices.
2. Learning Objectives
By completing this project, you will:
- Understand how to interface sensors with ESP32.
- Learn to use the ESP32’s Wi-Fi capabilities to connect to cloud services.
- Gain experience in publishing data to an IoT platform like ThingSpeak or Adafruit IO.
- Explore the MQTT protocol for IoT communication.
3. Tools and Components
Hardware:
- ESP32 Development Board
- DHT22 (Temperature and Humidity Sensor)
- Breadboard
- Jumper Wires
- USB Cable (for programming ESP32)
- Power Source (e.g., USB adapter or battery pack)
Software:
- Arduino IDE
- ESP32 board manager package for Arduino IDE
- IoT platform account (ThingSpeak, Adafruit IO, etc.)
4. Background/Definitions
- ESP32: A microcontroller with built-in Wi-Fi and Bluetooth, making it ideal for IoT projects.
- DHT22 Sensor: A digital sensor capable of measuring temperature and humidity with high accuracy.
- MQTT (Message Queuing Telemetry Transport): A lightweight messaging protocol designed for IoT.
- IoT Platform: A cloud service where IoT data can be stored, visualized, and analyzed.
5. Step-by-Step Guide
Step 1: Circuit Diagram
Below is a textual description of the connections:
- DHT22 Connections:
VCC: Connect to ESP32’s 3.3V pin.GND: Connect to ESP32’s GND pin.DATA: Connect to GPIO4 of ESP32 via a 10kΩ pull-up resistor.
Full Circuit Description:
- Place the ESP32 and DHT22 sensor on the breadboard.
- Use jumper wires to make the connections as described above.
- Connect the ESP32 to your computer using a USB cable for programming.
Step 2: Code Implementation
- Install Required Libraries:
- Open the Arduino IDE.
- Go to Sketch > Include Library > Manage Libraries, then search and install:
DHT sensor libraryAdafruit Unified SensorWiFiPubSubClient(for MQTT)
- Upload the Code:
cppCopy code#include <WiFi.h>
#include <PubSubClient.h>
#include "DHT.h"
// Wi-Fi credentials
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
// MQTT broker details
const char* mqtt_server = "broker.hivemq.com";
const char* mqtt_topic = "home/temperature_humidity";
WiFiClient espClient;
PubSubClient client(espClient);
// DHT22 configuration
#define DHTPIN 4
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
dht.begin();
// Connect to Wi-Fi
connectToWiFi();
// Set up MQTT server
client.setServer(mqtt_server, 1883);
}
void loop() {
if (!client.connected()) {
reconnectMQTT();
}
client.loop();
// Read temperature and humidity
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
if (!isnan(temperature) && !isnan(humidity)) {
// Publish data to MQTT topic
String payload = String("{\"temperature\":") + temperature +
String(", \"humidity\":") + humidity + String("}");
client.publish(mqtt_topic, payload.c_str());
Serial.println("Published data: " + payload);
} else {
Serial.println("Failed to read from DHT sensor!");
}
delay(5000); // Publish every 5 seconds
}
void connectToWiFi() {
Serial.print("Connecting to Wi-Fi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("\nConnected to Wi-Fi");
}
void reconnectMQTT() {
while (!client.connected()) {
Serial.print("Connecting to MQTT...");
if (client.connect("ESP32Client")) {
Serial.println("connected");
} else {
Serial.print("failed with state ");
Serial.println(client.state());
delay(2000);
}
}
}
Step 3: Configure IoT Platform
- Create an account on an IoT platform like Adafruit IO or ThingSpeak.
- Set up a new feed or channel to visualize temperature and humidity data.
- Ensure the MQTT topic and credentials match those in the code.
6. Testing and Debugging Tips
- Connection Issues: Verify Wi-Fi credentials and ensure the router is operational.
- Sensor Readings: Check for loose connections or damaged DHT22.
- Debugging: Use the Serial Monitor to observe real-time logs for troubleshooting.
- MQTT Errors: Confirm the broker’s address and check firewall settings.
7. Extensions
- Add Alerts: Integrate push notifications or email alerts when temperature or humidity exceeds thresholds.
- Expand Sensors: Include additional sensors like CO2, light, or motion sensors.
- Actuation: Control devices like fans or humidifiers based on sensor data.
- Web Interface: Build a custom web app to control and monitor the system.
- Battery Operation: Use a LiPo battery and power management system for portability.
This project introduces the fundamental concepts of IoT and equips you with the skills to build more advanced systems. Let me know if you’d like additional features or improvements!