Control Home Appliances with a Web Interface

Difficulty Level: Intermediate

In this project, we’ll create a simple home automation system to control appliances like lights or fans using an ESP8266 microcontroller, a relay module, and a web interface. This is a basic IoT (Internet of Things) implementation.

Components Required

Wiring the Circuit

To set up the ESP8266 and relay, follow these steps:

Arduino Code

Use the following Arduino code to set up a web interface for controlling the relay:


#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

const char* ssid = "YourSSID";
const char* password = "YourPassword";

ESP8266WebServer server(80);  // Start a web server on port 80

#define RELAY_PIN 5  // GPIO5 (D1) controls the relay

void setup() {
  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, LOW);  // Start with relay off

  Serial.begin(115200);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }
  Serial.println("Connected!");

  // Define web server routes
  server.on("/", handleRoot);
  server.on("/on", handleOn);
  server.on("/off", handleOff);

  server.begin();
}

void loop() {
  server.handleClient();
}

void handleRoot() {
  server.send(200, "text/html", 
  "

Home Appliance Control

" "
" ""); } void handleOn() { digitalWrite(RELAY_PIN, HIGH); server.send(200, "text/html", "

Appliance ON

Go Back"); } void handleOff() { digitalWrite(RELAY_PIN, LOW); server.send(200, "text/html", "

Appliance OFF

Go Back"); }

Code Explanation

Accessing the Web Interface

  1. Upload the code to your ESP8266 using the Arduino IDE.
  2. Open the Serial Monitor to find the ESP8266's IP address once it connects to Wi-Fi.
  3. Open a browser and enter the IP address to access the control interface.
  4. Click the "Turn ON" or "Turn OFF" buttons to control your appliance.

Conclusion

You’ve created a basic IoT-based home automation system. This project can be expanded with multiple relays to control additional appliances, enhanced security measures, or remote access through a cloud server.