Hey techies, have you ever imagine that you can control your home appliances remotely? In this article, we are going to learn the making of a wifi based home automationusing NodeMCU. You just need to upload the code provided below and make the correct connections so that the system works perfectly. You can read the full article with well-explained logic on our website.
How Does it Work?You can control four different AC or DC appliances using their project. This project is a local area network-based, so you can control it through one device. You have to write your mobile hotspots or network’s SSID and PASSWORD in the code. When the code is uploaded successfully to the NodeMCU then it will generate a local IP address which you can see on the serial monitor screen. When connected to the hotspot it will notify you on the serial monitor screen.
copy the IP address and paste it to access the home automation.
For turning on an appliance tap on the ON button and in a similar way for the other three. Tap OFF for turning off the respective appliance.
- NodeMCU esp8266
- Four-channel relay
- Three AC bulbs and a DC fan
- Jumper wires and a Breadboard
- 220-volts AC supply and 9-vols DC battery
- USB cable for uploading the code
Please take proper safety while working with AC current. Connect the VCC pin of the relay module with the VIN pin of the NodeMCU and the GND pin of the relay module with the GND pin of the NodeMCU. Join one wire of the AC bulb with the 220-volts AC supply and the other to a pin of the first relay module. Repeat this connection for the second and third relay modules. Attach the other wire of the AC supply to the other pin of all three relay modules. For the last relay connect a DC fan with a 9-volts battery connected with it as shown in the diagram. Now connect the IN-1, IN-2, IN-3, and IN-4 pins of the relay module with the digital-0, digital-1, digital-2, and digital-3 pins of the NodeMCU.
WiFi based home automation CodeNOTE: Please upload the code given below to the NodeMCU.
//TECHATRONIC.COM
// ESP8266 LIBRARY
// https://github.com/ekstrand/ESP8266wifi
#include <ESP8266WiFi.h>
const char* ssid = "DESKTOP"; // SSID i.e. Service Set Identifier is the name of your WIFI
const char* password = "asdfghjkl"; // Your Wifi password, in case you have open network comment the whole statement.
int R1=D0; // GPIO13 or for NodeMCU you can directly write D7
int R2=D1;
int R3=D2;
int R4=D3;
WiFiServer server(80); // Creates a server that listens for incoming connections on the specified port, here in this case port is 80.
void setup() {
Serial.begin(115200);
delay(10);
pinMode(R1, OUTPUT);
pinMode(R2, OUTPUT);
pinMode(R3, OUTPUT);
pinMode(R4, OUTPUT);
digitalWrite(R1,HIGH);
digitalWrite(R2,HIGH);
digitalWrite(R3,HIGH);
digitalWrite(R4,HIGH);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.print("Use this URL to connect: ");
Serial.print("http://");
Serial.print(WiFi.localIP()); //Gets the WiFi shield's IP address and Print the IP address of serial monitor
Serial.println("/");
}
void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
Serial.println("new client");
while(!client.available()){
delay(1);
}
// Read the first line of the request
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();
// Match the request
if (request.indexOf("/OFF1") != -1) {
digitalWrite(R1,LOW);
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("");
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("Relay 1 is ON");
client.println("</html>");
client.stop();
delay(1);
}
if (request.indexOf("/ON1") != -1) {
digitalWrite(R1, HIGH);
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("");
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("Relay 1 is OFF");
client.println("</html>");
client.stop();
delay(1);
}
if (request.indexOf("/OFF2") != -1) {
digitalWrite(R2,LOW);
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("");
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("Relay 2 is ON");
client.println("</html>");
client.stop();
delay(1);
}
if (request.indexOf("/ON2") != -1) {
digitalWrite(R2, HIGH);
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("");
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("Relay 2 is OFF");
client.println("</html>");
client.stop();
delay(1);
}
if (request.indexOf("/OFF3") != -1) {
digitalWrite(R3,LOW);
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("");
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("Relay 3 is ON");
client.println("</html>");
client.stop();
delay(1);
}
if (request.indexOf("/ON3") != -1) {
digitalWrite(R3, HIGH);
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("");
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("Relay 3 is OFF");
client.println("</html>");
client.stop();
delay(1);
}
if (request.indexOf("/OFF4") != -1) {
digitalWrite(R4,LOW);
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("");
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("Relay 4 is ON");
client.println("</html>");
client.stop();
delay(1);
}
if (request.indexOf("/ON4") != -1) {
digitalWrite(R4, HIGH);
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("");
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("Relay 4 is OFF");
client.println("</html>");
client.stop();
delay(1);
}
}
Also, check out the latest tutorials on Arduino and Raspberry Pi written by us.
HAPPY LEARNING!
Comments