
IoT-Based Smart Home Automation System Using ESP32
1. Introduction
This project demonstrates how to build a Wi-Fi-enabled Smart Home Automation System using an ESP32 microcontroller. The system allows users to control home appliances via a web dashboard or mobile app using MQTT or HTTP protocols.
Real-World Applications:
- Remote Home Automation – Control lights, fans, and other appliances via the internet.
- Energy Management – Monitor power usage and optimize consumption.
- Security Systems – Integrate motion sensors and alarms for added security.
2. Learning Objectives
By the end of this project, you will:
✅ Learn how to connect ESP32 to Wi-Fi and MQTT servers.
✅ Understand relay control for AC appliances.
✅ Develop a web-based control dashboard using HTML and JavaScript.
✅ Implement sensors (DHT11 for temperature/humidity).
✅ Learn about IoT protocols (MQTT, HTTP).
3. Tools & Components Required
Hardware:
- ESP32 Dev Kit (Wi-Fi & Bluetooth support)
- 4-Channel Relay Module (for AC appliance control)
- DHT11/DHT22 Sensor (Temperature & Humidity monitoring)
- AC Bulb & Fan (as appliances for testing)
- 5V Power Supply
- Jumper Wires, Breadboard
Software:
- Arduino IDE (Programming ESP32)
- Blynk App / Web Dashboard (for GUI control)
- MQTT Broker (like Mosquitto)
4. Background & Key Concepts
ESP32 Overview
The ESP32 is a powerful microcontroller with built-in Wi-Fi & Bluetooth, making it ideal for IoT applications. It has GPIO pins to control sensors and relays.
Relay Module for AC Control
A relay acts as an electronic switch to control high-voltage appliances. The ESP32 sends LOW or HIGH signals to activate or deactivate the relay.
MQTT Protocol
MQTT (Message Queuing Telemetry Transport) is a lightweight IoT protocol used for communication between devices. The ESP32 acts as a client publishing or subscribing to topics via an MQTT broker (like Mosquitto).
5. Circuit Diagram & Wiring
ESP32 to Relay Module Connection
- Connect ESP32 GPIO5 to Relay IN1
- Connect ESP32 GPIO18 to Relay IN2
- Connect ESP32 GPIO19 to Relay IN3
- Connect ESP32 GPIO21 to Relay IN4
- Connect ESP32 GND to Relay GND
- Connect ESP32 5V to Relay VCC
ESP32 to DHT11 Sensor
- Connect ESP32 GPIO4 to DHT11 Data Pin
- Connect ESP32 3.3V to DHT11 VCC
- Connect ESP32 GND to DHT11 GND
6. Step-by-Step Implementation
Step 1: Install Required Libraries
Open Arduino IDE, go to Library Manager, and install:
- ESP32WiFi.h (for Wi-Fi connectivity)
- Adafruit MQTT (for MQTT)
- DHT Sensor Library (for temperature/humidity readings)
Step 2: Connect ESP32 to Wi-Fi
#include <WiFi.h>
const char* ssid = "Your_WiFi_Name";
const char* password = "Your_WiFi_Password";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting...");
}
Serial.println("Connected to WiFi");
}
Step 3: Publish & Subscribe to MQTT Broker
#include <WiFiClient.h>
#include <PubSubClient.h>
WiFiClient espClient;
PubSubClient client(espClient);
const char* mqtt_server = "broker.hivemq.com"; // Free MQTT broker
void reconnect() {
while (!client.connected()) {
Serial.println("Attempting MQTT connection...");
if (client.connect("ESP32Client")) {
client.subscribe("home/relay1");
}
}
}
void setup() {
WiFi.begin(ssid, password);
client.setServer(mqtt_server, 1883);
}
void loop() {
if (!client.connected()) reconnect();
client.loop();
}
Step 4: Controlling the Relay via MQTT Messages
#define RELAY_PIN 5
void callback(char* topic, byte* message, unsigned int length) {
if (String(topic) == "home/relay1") {
if (message[0] == '1') {
digitalWrite(RELAY_PIN, HIGH);
} else {
digitalWrite(RELAY_PIN, LOW);
}
}
}
void setup() {
pinMode(RELAY_PIN, OUTPUT);
client.setCallback(callback);
}
Step 5: Read DHT11 Sensor & Send Data to MQTT
#include <DHT.h>
#define DHTPIN 4
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
dht.begin();
}
void loop() {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
client.publish("home/temp", String(temperature).c_str());
client.publish("home/humidity", String(humidity).c_str());
delay(5000);
}
7. Testing & Debugging Tips
✅ ESP32 not connecting to Wi-Fi? Double-check SSID and password.
✅ Relays not working? Ensure relay module gets 5V and is correctly wired.
✅ MQTT messages not received? Verify the MQTT broker is running and check topic names.
✅ DHT11 sensor not responding? Use a 4.7kΩ pull-up resistor on the data pin.
8. Extensions & Future Improvements
- Add a Web Dashboard: Use HTML/CSS & WebSockets for real-time updates.
- Use Blynk for App Control: Control relays via a mobile app.
- Energy Monitoring: Add an ACS712 current sensor to track power consumption.
- Voice Control Integration: Connect with Google Assistant/Alexa using IFTTT.
9. Conclusion
This project demonstrates how to create a Wi-Fi-enabled home automation system using ESP32, MQTT, and relays. By expanding this setup with sensors, energy monitoring, or cloud dashboards, you can develop a fully functional smart home system. 🚀
Would you like circuit diagrams or an app interface guide for this? Let me know! 😊