Here I will make project on Home Automation Using ESP8266.
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 using Home Automation Using ESP8266.
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 for Home Automation Using ESP8266
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 of Home Automation Using ESP8266
Code Explain for Home Automation Using ESP8266
In line no 9, we are storing the full
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(
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,
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 of Home Automation Using ESP8266
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | /* 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 Home Automation Using ESP8266
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.