ESP32: Smart Home Controller
Build a foundational smart home system using ESP32, allowing you to control devices remotely via a simple web interface or a dedicated mobile application (integration concepts). This project will cover basic IoT principles, network communication, and device control.
Things Needed
- ESP32 Development Board ₹450
- 4-Channel Relay Module ₹200
- DHT11 Temperature & Humidity Sensor ₹80
- Breadboard & Jumper Wires ₹120
- Micro USB Cable ₹50
- (Optional) Small Enclosure ₹150
Recommended Purchase Links:
Steps to Build
Step 1: Setup Arduino IDE & ESP32 Board
First, ensure your Arduino IDE is set up for ESP32. Install the ESP32 board manager and necessary libraries (e.g., AsyncWebServer, DHT sensor library).
Arduino IDE Board Manager URL:
https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_index.json
Libraries to Install (via Library Manager):
ESP Async WebServer
DHT sensor library (by Adafruit)
Step 2: Wiring the Components
Connect the relay module and DHT11 sensor to your ESP32. Pay close attention to power (VCC, GND) and signal pins.
Example wiring diagram (Refer to actual pinouts for your module)
- ESP32 GPIO 2 (D2) to Relay IN1
- ESP32 GPIO 4 (D4) to DHT11 Data Pin
- ESP32 3.3V to VCC of DHT11 and Relay
- ESP32 GND to GND of DHT11 and Relay
Step 3: Develop the Firmware (Arduino Sketch)
Write the Arduino sketch that initializes Wi-Fi, creates a web server, reads sensor data, and controls the relays.
`smart_home_controller.ino`
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#include <DHT.h>
// WiFi credentials
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
// Relay and DHT pins
const int RELAY_PIN = 2; // Example GPIO pin for relay
const int DHT_PIN = 4; // Example GPIO pin for DHT11
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHT_PIN, DHTTYPE);
AsyncWebServer server(80); // Create AsyncWebServer object on port 80
void setup() {
Serial.begin(115200);
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, HIGH); // Turn off relay initially (if active low)
dht.begin();
// Connect to Wi-Fi
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi..");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("Connected! IP Address: ");
Serial.println(WiFi.localIP());
// Route for web page
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(200, "text/html",
"<h1>ESP32 Smart Home</h1>"
"<p>Temperature: " + String(dht.readTemperature()) + " °C</p>"
"<p>Humidity: " + String(dht.readHumidity()) + " %</p>"
"<p>Relay State: " + (digitalRead(RELAY_PIN) == LOW ? "ON" : "OFF") + "</p>" // Assuming active LOW relay
"<a href=\"/toggle\">Toggle Relay</a>"
);
});
// Route for toggling relay
server.on("/toggle", HTTP_GET, [](AsyncWebServerRequest *request){
digitalWrite(RELAY_PIN, !digitalRead(RELAY_PIN)); // Toggle the relay
request->redirect("/"); // Redirect back to home
});
server.begin(); // Start server
Serial.println("HTTP server started");
}
void loop() {
// DHT11 reading (can be done periodically or on request)
// For this simple example, we read on web request.
// In a real app, you might read every X seconds and store.
}
Remember to replace `YOUR_WIFI_SSID` and `YOUR_WIFI_PASSWORD` with your actual Wi-Fi credentials.
Found an issue or error in the code. Just copy the error and paste in message box in footer. We will send you updated and fixed code.
Step 4: Upload the Code & Test
Select the correct ESP32 board and COM port in Arduino IDE, then upload the sketch. Once uploaded, open the Serial Monitor to see the assigned IP address. Navigate to this IP address in your web browser to control your smart home system.
Ready to Flash?
Use our integrated web flasher to upload the compiled binary directly to your ESP32 board. Ensure your board is in flashing mode.
Note: Web Flasher requires a compatible browser (like Chrome, Edge, Opera) and specific drivers for your board.