Introduction: ESP8266 Manual and Server Controlled Home Automation Prototype.

ESP8266 is very popular IOT platform it is very easy to use and easily available,Hence i got one for myself and started to play with it and after some coding and looking into it i decided to make a simple IOT based home automation system with manual switch control capablities(WITHOUT USING A TWO WAY OR THREE WAY SWITCH).

Basically i used ESP8266 NodeMCU GPIO pins for external switching and using the same concept we can control numerous main power source devices using RELAY.

I have divided this instructables in two parts , In first part i have explained all the basic of this project and in the second part i used main power line for the same set of instruction.

So,Lets get started

Supplies

Parts Required(for testing)-

1.ESP8266 NodeMCU module

2.LEDs

3.Switch

4.Wires

6.Breadboard

7.Resistor 47ohm

(For final project)

6.Relay(for using with mains supply)

7.Bulb.

Step 1: Do Connection According to This Diagram.

This is for test purpose.

Step 2: How the Code Works

*I have tried to explain all the import aspects of this code so if you any any queries do let me know in comments down below*

First and the foremost part of any code is Header file so lets start with adding a header file (remember to add ESP8266 library to Arduino IDE before starting to code)

#include //header file

after selecting a header file we have to select the wifi name and password

const char* ssid="Kakashi"; //SSID=service set identifier(basically name of wifi)(change it with your wifi name)
const char* password="0987654321"; //password of wifi(change with your wifi password)

Now we havet o create a server for the all the conections

WiFiServer server(80); //Create a server that listen to incoming connections

now we are going to connect to wifi network for that we will use WiFi.begin() function

WiFi.begin(ssid,password); //here wifi is intitalized


Now while it is getting connected

This loop will go on until it gets connected to a network.

while(WiFi.status()!=WL_CONNECTED)

{

delay(500);

Serial.println(".");

}

Serial.println("");

Serial.println("Connected");


//Printing the IP address copy and paste this address in your browser to access the sever
Serial.print("Use this ip in address bar to connect:");

Serial.print("http://");

Serial.println(WiFi.localIP());

Serial.println("/");


This is the part where we are going to control our LED/BULB manually.

This while loop will only work when there is no client available if there is a client available then it will take command from client at that time this loop won't work.

button pin is pulled high and it is connected to switch other terminal of switch is connected to ground so when ever switch is kept on button pin will read ground or LOW and vice versa hence the code will work according to it.


while(!client)

{

if(digitalRead(button)==LOW)

{current=1;}

else

{current=0;}

if(last==current)

{return;}

digitalWrite(LED,current);

last=current;

}


//Read the first line of request
String request=client.readStringUntil('/r');

Serial.println(request);

This line is pretty important because our manual control while() loop is working because of this only this line will disconnect the client as soon as command is executed.

client.flush();

The next section of if and else statements checks which button was pressed in your web page, and controls the outputs accordingly.

For example, if you’ve press the ON button, the ESP8266 receives a request on the /LED=ON/ URL .So, we can check if the header contains the expression /LED=ON/ on. If it contains, we change the value variable to ON, and the ESP8266 turns the LED on.

This works similarly for the other buttons. So, if you want to add more outputs, you should modify this part of the code to include them.


//Match request

int value=LOW;

if (request.indexOf("/LED=ON") != -1)

{ digitalWrite(LED, HIGH); value = HIGH; }

else if (request.indexOf("/LED=OFF") != -1)

{ digitalWrite(LED,LOW); value = LOW; }

// Set ledPin according to the request

REST OF CODE IS TO MAKE AND STYLE YOUR HTML WEB PAGE I HAVE EXPLAINED MOST OF THAT IN CODE ITSELF USING COMMENTS SO I WON'T BE EXPLAINING ALL THAT HERE.



Step 3: Wrapping Up and Testing:

So,in both ways we can control the LED .

Now its time for leveling up and try to use to control an AC Bulb in similar manner.

Step 4: Controlling AC Light Bulb.

Now remove LED and connect a Relay according to diagram.

Now Please note Your relay won't work with 3.3V from esp so you have to provide different 5V to relay(here i have used ArduinoNano for that purpose).

Thats it.

First Time Author Contest

Participated in the
First Time Author Contest