ESP32 Home Automation System with Relay Control

Introduction to Home Automation with ESP32

Home automation refers to the use of technology to control appliances and systems in a home, such as lighting, heating, or security. In this project, we will use the ESP32 microcontroller along with a relay module to create a simple home automation system. The ESP32 will host a web server that allows users to control appliances remotely via a smartphone or computer. We'll use a relay to switch appliances on or off based on the web interface commands.

Components Required

For this project, we need an ESP32 development board, a relay module, and a few household appliances (e.g., a light or fan) to control. The ESP32 will be connected to the relay, and a web server will be created to control the relay. This project is ideal for those interested in home automation and IoT.

  • Install the necessary libraries (WiFi, ESPAsyncWebServer, and Relay control) in the Arduino IDE.
  • Connect the relay module to the ESP32 and connect the appliances to the relay's output.
  • Write code to initialize the ESP32 as a web server.
  • Create a simple web page to control the relay (ON/OFF states).
  • Use the ESP32 to read requests from the web interface and control the relay accordingly.
  • Upload the code to the ESP32, test the system by controlling the relay via a web browser.

Formula: Relay Control: If (webRequest == 'on') { relay.setState(ON); } else { relay.setState(OFF); }

Components List

  • 1 × ESP32 Development Board
  • 1 × Relay Module
  • 1 × Lamp, Fan, or any appliance to control
  • 1 × Breadboard and Jumper Wires
  • 1 × Power Supply (e.g., 5V USB power adapter)
  • 1 × Wi-Fi Network
  • 1 × Computer or Smartphone with Web Browser

Pin Configuration

  • GPIO 23 (Relay IN Pin): The control pin of the relay module is connected to GPIO 23 on the ESP32.
  • GPIO 22 (Relay GND Pin): GND pin of the relay module connected to the ESP32 GND.
  • GPIO 21 (Relay VCC Pin): VCC pin of the relay module connected to the 5V pin of the ESP32.

Ensure that the appliance's power rating is compatible with the relay and the relay is capable of handling the load. Be cautious while handling high-voltage appliances.

Wiring and Connections

  • -> GPIO 23 (Control Pin)
  • -> GND
  • -> 5V (Power)
  • ->

Code for ESP32 Web Server

1#include <WiFi.h>
2#include <ESPAsyncWebServer.h>
3
4const char* ssid = "Your_SSID";
5const char* password = "Your_PASSWORD";
6
7const int relayPin = 23;
8
9AsyncWebServer server(80);
10
11void setup() {
12  Serial.begin(115200);
13  WiFi.begin(ssid, password);
14  while (WiFi.status() != WL_CONNECTED) {
15    delay(1000);
16    Serial.println("Connecting to WiFi...");
17  }
18  Serial.println("Connected to WiFi");
19  pinMode(relayPin, OUTPUT);
20  digitalWrite(relayPin, LOW);  // Initial state (OFF)
21
22  server.on("/on", HTTP_GET, [](AsyncWebServerRequest *request) {
23    digitalWrite(relayPin, HIGH);  // Turn ON the appliance
24    request->send(200, "text/html", "<h1>Appliance ON</h1>");
25  });
26
27  server.on("/off", HTTP_GET, [](AsyncWebServerRequest *request) {
28    digitalWrite(relayPin, LOW);  // Turn OFF the appliance
29    request->send(200, "text/html", "<h1>Appliance OFF</h1>");
30  });
31
32  server.begin();
33}
34
35void loop() {
36  // No need for continuous code in loop() as the web server handles it
37}

Code Explanation

  • #include <WiFi.h>: This line includes the WiFi library to enable the ESP32 to connect to a wireless network.
  • #include <ESPAsyncWebServer.h>: Includes the ESPAsyncWebServer library to easily create a web server on the ESP32.
  • server.on("/on", HTTP_GET, [](AsyncWebServerRequest *request) {: Defines the web route for turning on the appliance. When the user accesses the '/on' route, the relay is activated.
  • digitalWrite(relayPin, HIGH);: This command turns the relay ON, thereby powering the appliance connected to it.
  • request->send(200, "text/html", "<h1>Appliance ON</h1>");: Sends an HTML response to the user, confirming that the appliance has been turned on.

Applications

  • Home automation systems for controlling appliances remotely.
  • Automating devices like lamps, fans, or security cameras.
  • IoT-based remote control of household devices via the web or mobile app.
  • Energy-efficient solutions for managing home appliances.

Conclusion

This project demonstrates how to create a home automation system using the ESP32 and a relay module. With this system, you can remotely control your home appliances through a web browser, making it a powerful addition to any smart home setup. The project can be expanded by adding more relays for additional devices, integrating with mobile apps, or implementing voice control features.