Home Automation Using ESP8266 NodeMCU with Relay

Home Automation Using ESP8266 NodeMCU with Relay

Share it

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

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

Watch Video Tutorial

Share it

Leave Comment