Wi-Fi Based Home Automation

by TECHATRONIC in Circuits > Arduino

18 Views, 0 Favorites, 0 Comments

Wi-Fi Based Home Automation

1629451991773.jpg

Hello Tech freaks, I'm back again with another interesting project based on IoT aka NodeMCU (ESP8266). In today's article, we'll make a Home automation system using NodeMCU and few other active components and modules. But first we need a stable Wi-Fi connection for all the method and program to be able to control. The main components are Relay Module and NodeMCU itself.

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. After uploading code successfully to the NodeMCU then it will generate a local IP address which you can see on the serial monitor screen. As soon as it is connected to the hotspot, it will notify you on the serial monitor screen.

For controlling the Appliance with the App you can download from given also the IP Address need to be copied successfully as it may not work if IP is wrong. The interface of the app is very user-friendly. WARNING: As we're working with AC supply make sure to make proper insulation to all the components and module i.e., making AC & DC supply abstracted from each other, else it may lead to some severe damage. Also for more information and detail about working visit original posts and bookmark that site for pre-access to all further posts.

Material Required

  • NodeMCU ESP8266
  • Four-channel relay
  • Three AC bulbs and a DC fan
  • Jumper wires
  • Breadboard
  • 220-volts AC supply
  • 9-volts DC battery
  • USB cable for uploading the code

Circuit Diagram

image-1024x898.jpg

Code

n1-1.png
16222714134-201x300.jpg
//TECHATRONIC.COM  
 // ESP8266 LIBRARY  
 // https://github.com/ekstrand/ESP8266wifi  
 #include   
 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("");  
    client.println("");  
    client.println("Relay 1 is ON");  
    client.println("");  
    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("");  
    client.println("");  
    client.println("Relay 1 is OFF");  
    client.println("");  
    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("");  
    client.println("");  
    client.println("Relay 2 is ON");  
    client.println("");  
    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("");  
    client.println("");  
    client.println("Relay 2 is OFF");  
    client.println("");  
    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("");  
    client.println("");  
    client.println("Relay 3 is ON");  
    client.println("");  
    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("");  
    client.println("");  
    client.println("Relay 3 is OFF");  
    client.println("");  
    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("");  
    client.println("");  
    client.println("Relay 4 is ON");  
    client.println("");  
    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("");  
    client.println("");  
    client.println("Relay 4 is OFF");  
    client.println("");  
    client.stop();  
    delay(1);  
  }  
 }
For detailed explanation of How the code works also for any troubleshooting visit its original posts.