Difficulty Level: Intermediate
This project will guide you through building a basic home automation system using the NodeMCU (ESP8266). You will be able to control lights, fans, or other appliances remotely over Wi-Fi through a simple web interface.
Connect the relay module to the NodeMCU as follows:
Each relay channel controls one appliance, and the relays are connected in parallel with the appliances.
The following code will enable control of the relays via a web interface hosted on the NodeMCU.
digitalWrite()
: Sets the GPIO pins high or low to turn the relay on or off.WiFi.begin()
: Connects the NodeMCU to your Wi-Fi network.server.on()
: Defines the web server routes for controlling devices.Here’s the Arduino code for controlling home appliances through relays using NodeMCU:
#include
#include
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
ESP8266WebServer server(80);
// Relay pins
const int relay1 = 5; // D1
const int relay2 = 4; // D2
const int relay3 = 14; // D5
const int relay4 = 12; // D6
void setup() {
Serial.begin(115200);
// Set relay pins as outputs
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);
// Start with all relays off
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
digitalWrite(relay3, HIGH);
digitalWrite(relay4, HIGH);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Define routes for controlling appliances
server.on("/", handleRoot);
server.on("/relay1/on", []() {
digitalWrite(relay1, LOW); // Relay ON
server.send(200, "text/plain", "Relay 1 ON");
});
server.on("/relay1/off", []() {
digitalWrite(relay1, HIGH); // Relay OFF
server.send(200, "text/plain", "Relay 1 OFF");
});
// Similar routes for other relays
server.on("/relay2/on", []() {
digitalWrite(relay2, LOW);
server.send(200, "text/plain", "Relay 2 ON");
});
server.on("/relay2/off", []() {
digitalWrite(relay2, HIGH);
server.send(200, "text/plain", "Relay 2 OFF");
});
server.on("/relay3/on", []() {
digitalWrite(relay3, LOW);
server.send(200, "text/plain", "Relay 3 ON");
});
server.on("/relay3/off", []() {
digitalWrite(relay3, HIGH);
server.send(200, "text/plain", "Relay 3 OFF");
});
server.on("/relay4/on", []() {
digitalWrite(relay4, LOW);
server.send(200, "text/plain", "Relay 4 ON");
});
server.on("/relay4/off", []() {
digitalWrite(relay4, HIGH);
server.send(200, "text/plain", "Relay 4 OFF");
});
server.begin(); // Start the server
}
void loop() {
server.handleClient();
}
void handleRoot() {
String html = "Home Automation Control
"
"Turn ON Relay 1
"
"Turn OFF Relay 1
"
"Turn ON Relay 2
"
"Turn OFF Relay 2
"
"Turn ON Relay 3
"
"Turn OFF Relay 3
"
"Turn ON Relay 4
"
"Turn OFF Relay 4
";
server.send(200, "text/html", html);
}
YOUR_SSID
and YOUR_PASSWORD
with your Wi-Fi credentials.In this project, we built a home automation system using NodeMCU, a relay module, and a web interface. This setup allows you to control multiple appliances remotely over Wi-Fi. You can expand this project further by integrating sensors or adding more devices to the system.