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.
To set up the ESP8266 and relay, follow these steps:
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");
}
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.