top of page
Writer's pictureRamesh G

IoT Based Home automation

Updated: Jul 2, 2021



In This tutorial to build a standalone ESP8266 NodeMCU Web Server that controls any relay module. We’ll create an ESP8266 Web Server and it can be accessed with any device with a Web browser in your local network.


Circuit Diagram

Components Required

ESP8266 12E

4 Channel Relay Module

Jumper wires


4 Channel Isolated 5A or 10A Relay HW-316

The 4 Channel Relay Breakout is an easy way to use your Arduino, Raspberry Pi, or other microcontroller to switch high voltages and high current loads. ... Each relay has the common, normally open, and normally closed pin broken out to a convenient 5.0mm pitch screw terminal.

While this board can be used to switch mains power, it should be done so using extreme caution. This should not be done without the aid of an experienced electrician.

  • Equiped with high-current relay, AC250V 10A ; DC30V 10A

  • 5V 4-Channel Relay interface board, and each one needs 50-60mA Driver Current

  • Be able to control various appliances, and other equipments with large current

  • Application:Supports all MCU control, The industrial field, PLC control, Smart home control

  • Indication LED's for Relay output status


Installing the ESP8266_Arduino_Library

Click here to download the ESPAsyncWebServer library

Click here to download the AsyncTCP library (ESP32)

Click here to download the ESPAsyncTCP library (ESP8266 NodeMCU)

Subscribe and Download code.

In your Arduino IDE, to install the libraries go to Sketch > Include Library > Add .ZIP library… and select the library you’ve just downloaded.

After installing the required libraries, copy the following code to your Arduino IDE.

Subscribe and Download code.


Arduino Code


#include "ESP8266WiFi.h"

#include "ESPAsyncWebServer.h"


// Set to true to define Relay as Normally Open (NO)

#define RELAY_NO true


// Set number of relays

#define NUM_RELAYS 4


// Assign each GPIO to a relay

int relayGPIOs[NUM_RELAYS] = {5, 4, 14, 12};

// WiFi login parameters - network name and password

const char* ssid = "TP-Link_3200"; // your SSID

const char* password = "9500112137"; // your WIFi Password


const char* PARAM_INPUT_1 = "relay";

const char* PARAM_INPUT_2 = "state";


// Create AsyncWebServer object on port 80

AsyncWebServer server(80);


const char index_html[] PROGMEM = R"rawliteral(

<!DOCTYPE HTML><html>

<head>

<meta name="viewport" content="width=device-width, initial-scale=1">

<style>

html {font-family: Arial; display: inline-block; text-align: center;}

h2 {font-size: 2.0rem;}

p {font-size: 2.0rem;}

body {max-width: 900px; margin:0px auto; padding-bottom: 25px;}

.switch {position: relative; display: inline-block; width: 120px; height: 68px}

.switch input { opacity: 0; width: 0; height: 0;}

.slider {position: absolute; top: 0; left: 0; right: 0; bottom: 0; background-color: #ff3737; -webkit-transition: .4s; transition: .4s;}

.slider:before {position: absolute; content: ""; height: 52px; width: 52px; left: 8px; bottom: 8px; background-color: #e6e6e6; -webkit-transition: .4s; transition: .4s; }

input:checked+.slider {background-color: #21f37e}

input:checked+.slider {box-shadow: 0 0 1px #21f37e}

input:checked+.slider:before {-webkit-transform: translateX(52px); -ms-transform: translateX(52px); transform: translateX(52px)}

</style>

</head>

<body>

<h2>ESP8266 Home Automation Web Sever</h2>

%BUTTONPLACEHOLDER%

<script>function toggleCheckbox(element) {

var xhr = new XMLHttpRequest();

if(element.checked){ xhr.open("GET", "/update?relay="+element.id+"&state=1", true); }

else { xhr.open("GET", "/update?relay="+element.id+"&state=0", true); }

xhr.send();

}</script>

</body>

</html>

)rawliteral";


// Replaces placeholder with button section in your web page

String processor(const String& var){

//Serial.println(var);

if(var == "BUTTONPLACEHOLDER"){

String buttons ="";

for(int i=1; i<=NUM_RELAYS; i++){

String relayStateValue = relayState(i);

buttons+= "<h4>Device : " + String(i) + "</h4><label class=\"switch\"><input type=\"checkbox\" onchange=\"toggleCheckbox(this)\" id=\"" + String(i) + "\" "+ relayStateValue +"><span class=\"slider\"></span></label>";

}

return buttons;

}

return String();

}


String relayState(int numRelay){

if(RELAY_NO){

if(digitalRead(relayGPIOs[numRelay-1])){

return "";

}

else {

return "checked";

}

}

else {

if(digitalRead(relayGPIOs[numRelay-1])){

return "checked";

}

else {

return "";

}

}

return "";

}


void setup(){

// Serial port for debugging purposes

Serial.begin(115200);


// Set all relays to off when the program starts - if set to Normally Open (NO), the relay is off when you set the relay to HIGH

for(int i=1; i<=NUM_RELAYS; i++){

pinMode(relayGPIOs[i-1], OUTPUT);

if(RELAY_NO){

digitalWrite(relayGPIOs[i-1], HIGH);

}

else{

digitalWrite(relayGPIOs[i-1], LOW);

}

}

// Connect to Wi-Fi

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {

delay(1000);

Serial.println("Connecting to WiFi..");

}


// Print ESP8266 Local IP Address

Serial.println(WiFi.localIP());


// Route for root / web page

server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){

request->send_P(200, "text/html", index_html, processor);

});


// Send a GET request to <ESP_IP>/update?relay=<inputMessage>&state=<inputMessage2>

server.on("/update", HTTP_GET, [] (AsyncWebServerRequest *request) {

String inputMessage;

String inputParam;

String inputMessage2;

String inputParam2;

// GET input1 value on <ESP_IP>/update?relay=<inputMessage>

if (request->hasParam(PARAM_INPUT_1) & request->hasParam(PARAM_INPUT_2)) {

inputMessage = request->getParam(PARAM_INPUT_1)->value();

inputParam = PARAM_INPUT_1;

inputMessage2 = request->getParam(PARAM_INPUT_2)->value();

inputParam2 = PARAM_INPUT_2;

if(RELAY_NO){

Serial.print("NO ");

digitalWrite(relayGPIOs[inputMessage.toInt()-1], !inputMessage2.toInt());

}

else{

Serial.print("NC ");

digitalWrite(relayGPIOs[inputMessage.toInt()-1], inputMessage2.toInt());

}

}

else {

inputMessage = "No message sent";

inputParam = "none";

}

Serial.println(inputMessage + inputMessage2);

request->send(200, "text/plain", "OK");

});

// Start server

server.begin();

}

void loop() {


}

After a successful upload, open the Serial Monitor at a baud rate of 115200. Press the “EN/RST” button on the ESP8266 board. Now it should print its IP address.






129 views1 comment

1件のコメント


Christian Johnson
Christian Johnson
2023年12月09日

Thanks to Local Locksmith Charlotte company for their expertise in smart automation security. The installation was done with precision, and their team was courteous throughout the process. Highly recommend their services for enhanced home security.

いいね!
bottom of page