Home Automation

Wifi Home Automation using NodeMCU

If, you are looking for a project that can make your home a little more smarter, then this project is for you.Here, you will learn to make a wifi home automation system where can automate your home appliances using smartphone via Wi-Fi connection. The knowledge gained in this project will benefit you in making other Wi-Fi based or IOT based projects. We will be using an ESP based development board i.e. NodeMCU ESP8266 12E, which is the cheapest and the most useful dev board for Internet of Things Projects. You can choose other ESP8266 based boards too, all the procedure and working will be same, with some little modification in connection and code according to the boards’s architecture.

Project Level:           Intermediate

Prerequisite Required:

Basics of NodeMCU, MIT App Inventor (for making your own edition of App) and Relay. *Don’t worry, we will guides you

Warning: This project involve interfacing HIGH VOLTAGE supply, so check connection twice and seek the help from elder or someone, who have better knowledge of handling High Voltage supply.

So, let’s get started!

Components Required:

  • NodeMCU ESP 12E (Amazon/Ebay/Flipkart/AliExpress)
  • Relay Module (Amazon/Ebay/Flipkart/AliExpress)
  • Jumper Wires (Amazon/Ebay/Flipkart/AliExpress)
  • 5v Power Module(optional) (Amazon/Ebay/Flipkart/AliExpress)
  • Connecting wire/Switch Board
*Please note: These are affiliate links. I may make a commission if you buy the components through these links.
I would appreciate your support in this way to help this website!

Connection:

App Making: Using MIT App Inventor

Layout:

Blocks:

App Project File /Direct App : Download

Code Section:

//Visit www.roboshala.com for more details on NodeMCU and other projects.

#include <ESP8266WiFi.h>
 
const char* ssid = "CPH1613"; // SSID i.e. Service Set Identifier is the name of your WIFI
const char* password = "12345678"; // Your Wifi password, in case you have open network comment the whole statement.
 
int R1=D1; // GPIO13 or for NodeMCU you can directly write D7 
int R2=D2;
int R3=D3;
int R4=D4;
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("/Relay1On") != -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("/Relay1Off") != -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("/Relay2On") != -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("/Relay2Off") != -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("/Relay3On") != -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("/Relay3Off") != -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("/Relay4On") != -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("/Relay4Off") != -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);
  }

}
 

 

Code : Download

Tags
Show More

Related Articles

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button
Close