Home Automation Using ESP8266 nodemcu with relay module

Nowadays everything is digital. We can control almost everything using our mobile. We have also WiFi in our home. So why not we can control our home electronics instruments like light fan AC etc.
In this post, I will share how to control a LED Bulb of 220V AC using ESP8266 NodeMCU using Relay Board via Mobile using WiFi.

What we will Learn?

  • How to connect 220V Ac with Relay
  • How to connect NodeMCU with Relay
  • Creating HTTP web server in NodeMCU
  • How to connect ESP8266 to home WiFi Router
  • View NodeMCU IP Address in the Serial Monitor
  • How to set Static IP address in NodeMCU
  • How to control ESP8266 via HTML page

Components Used:(Click to Buy)

Connection Setup

How to connect 220V Ac with Relay

To Connect with AC line we need a small wire. The neutral line will be connected directly to the bulb. Live wire first go to COM port of Relay. Then one small wire will be connected to the bulb from Normally Open(NO) Port. In the following diagram, we have shown a battery instead of the AC line for your understanding.

How to connect NodeMCU with Relay

Connection with ESP8266 NodeMCU is very simple one. Just we have to connect 3.3V to the VCC and Ground to the GND. Then we have to connect data line. Here we are using D2 pin. So we have used pin value as 4.
If you do not know integer pin values of NodeMCU, see this post.

ESP8266 Nodemcu Pins Constant Integer values

Connection Diagram

Home Automation Using ESP8266 NodeMCU with Relay

Code Explain

In line no 9, we are storing the full html page in the program memory. Also we have to include all the required header file for the WiFi.

How to start HTTP web server in NodeMCU

In the 40 & 41 line, enter your WiFi Router SSID & password. In the line 42, we are starting HTTP server on standard TCP port no 80.

How to connect ESP8266 to home WiFi Router

We can connect NodeMCU just using the “WiFi.begin(ssid, password); “ command. But this will be based on DHCP i.e. dynamic IP. If we use non-static IP, everytime we have to see Local IP in the serial monitor.

How to see NodeMCU IP Address

Using the line no 83 command we can see the local IP in the serial monitor.
“Serial.println(WiFi.localIP());”

How to set Static IP address in NodeMCU

From 35 to 38 line, we are passing all the static configuration values. Here noticeable thing is that we have to pass the values using a comma(,) instead of a dot(.) of IP address format. In line 66, we are configuring nodeMCU with static values. Here we have disconnected wifi in line 65 to remove previous DHCP settings.

If you do not have a good knowledge of IPV4 configuration, do not use static config in NodeMCU. The wrong setting will lead to error in connection.

The following code is ideal for Android Mobile Hotspot where NodeMCU IP will be 192.168.43.50

How to control ESP8266 via HTML page

The web interface is designed using simple HTML5 & CSS3.
Using hyperlinks, ledon or led off functions are called in the line no 86 & 87.

Here in 52 & 58 no line, NodeMCU does the opposite thing. Here if we send High signal to the digital pin, it will switch off the LED & vice versa.

Arduino Sketch

/*
https://somtips.com
https://youtube.com/somtips
*/
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
//HTML webpage contents in program memory
const char MAIN_page[] PROGMEM = R"=====(
<!DOCTYPE html>
<html>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<head>
  <style>
    body {background-color: yellow; float:center;text-align: center;}
    h1   {color: blue;}
    h2   {color: red;}
    h3   {color: brown;}
    button  {text-align: center;border-radius: 30px;font-size: 30px;color: white;padding: 20px;}
  </style>
</head>
<body>
<h1>Home Automation</h1>
<h2>Room LED</h2>
<h3>by somtips.com</h3><br>
<a href="ledon"><button style="background-color: green">ON</button></a>
<a href="ledoff"><button style="background-color: red">OFF</button></a>
</body>
</html>
)=====";

#define LED 4  
String s = MAIN_page; 
//Static IP address configuration
IPAddress staticIP(192, 168, 43, 50); //ESP static ip
IPAddress gateway(192, 168, 43, 1);   //IP Address of your WiFi Router
IPAddress subnet(255, 255, 255, 0);  //Subnet mask
IPAddress dns(8, 8, 8, 8);  //DNS
//SSID and Password of your WiFi router
const char* ssid = "Som Tips";
const char* password = "Som Tips";
ESP8266WebServer server(80); //Server on port 80

void Root() {
 Serial.println("home page");

 server.send(200, "text/html", s); 
}

void ledon() { 
 Serial.println("LED on");
 digitalWrite(LED,LOW); //LED on
 server.send(200, "text/html", s); 
}

void ledoff() { 
 Serial.println("LED off");
 digitalWrite(LED,HIGH); //LED off
 server.send(200, "text/html", s); 
}

void setup(void){
  Serial.begin(115200);
  WiFi.begin(ssid, password); 
  WiFi.disconnect();
  WiFi.config(staticIP, subnet, gateway, dns);
  WiFi.begin(ssid, password);
  WiFi.mode(WIFI_STA);
  pinMode(LED,OUTPUT); 
  digitalWrite(LED,HIGH);
  
  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  //If connection successful show IP address in serial monitor
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP()); 

  server.on("/", Root);
  server.on("/ledon", ledon); 
  server.on("/ledoff", ledoff);

  server.begin();
  Serial.println("HTTP server started");
}
void loop(void){
  server.handleClient(); 
}

Output

So, now we have connected our mobile with the same router where we have connected the NodeMCU. First, see the IP address ( If no static IP ) in the Serial Monitor. Now enter the IP in the browser address bar.

Finally we can now control LED bulb using ON / OFF button in the mobile browser screen from anywhere in the home.

Home Automation Using ESP8266 NodeMCU with Relay
httpv://www.youtube.com/watch?v=NKcndiHtwEE

Comment Box