Home Automation Using ESP8266 Wi-Fi Module






ESP8266 WiFi Module is a self contained SOC with integrated TCP/IP protocol stack that can give any microcontroller access to your WiFi network. The ESP8266 is capable of either hosting an application or offloading all Wi-Fi networking functions from another application processor. Each ESP8266 module comes pre-programmed with an AT command set firmware, meaning, you can simply hook this up to your Arduino device and get about as much WiFi-ability as a WiFi Shield offers (and that's just out of the box)! The ESP8266 module is an extremely cost effective board with a huge, and ever growing, community.

This module has a powerful enough on-board processing and storage capability that allows it to be integrated with the sensors and other application specific devices through its GPIOs with minimal development up-front and minimal loading during runtime. Its high degree of on-chip integration allows for minimal external circuitry, including the front-end module, is designed to occupy minimal PCB area. The ESP8266 supports APSD for VoIP applications and Bluetooth co-existance interfaces, it contains a self-calibrated RF allowing it to work under all operating conditions, and requires no external RF parts.
ESP8266 PIN Diagram
 
Fig.1

Wire Diagram of ESP8266
 
Fig.2


Vcc ==> with 3.3V of  board.
GND ==> with GND of board.
Rx ==> with Tx of board.
Tx ==> with Rx of board.
CH_PD ==>  with 3.3V of  board.

Wire Diagram of Relay module 
 
Fig.3

Now connect your Arduino UNO board and ESP8266 module  and Relay Board as you seen in Fig.3 , After that open Arduino IDE and select the board “Arduino UNO” like Fig.4.
 
Fig.4

Now, put the below code in your sketch and save the sketch ,and compile the code and  upload  sketch on ArduinoUno board. 
                                                         

Fig.5

Arduino Code:

---------------------------------------------------------------------------------------------------------------------
volatile char MY_DATA1[60];
volatile unsigned char k1 = 0,CC1=0,RX1 = 0,D = 0;
void setup()
{
// relay out put pin control.//
pinMode(A0,OUTPUT);
pinMode(A1,OUTPUT);
pinMode(A2,OUTPUT);
pinMode(A3,OUTPUT);
digitalWrite(A0,HIGH);
digitalWrite(A1,HIGH);
digitalWrite(A2,HIGH);
digitalWrite(A3,HIGH);
// Initial setup //
Serial.begin(115200);
// Initial ESP8266 //
ESP8266_INIT();
}
void loop()
{
// receive data from ESP8266 //
if(Serial.available())
{
MY_DATA1[k1] = Serial.read(); // read receive data from wi-fi module.
CC1 = MY_DATA1[k1];
if(CC1 == ':')D = k1;// check weather data scan start or not.
k1++;
if(CC1 == '\n'){k1=0;RX1 = 1;} // chaeck new line character are receive or not.
}
// Processing  data which are receiving  from ESP8266 //
if(RX1 == 1)
{
if(MY_DATA1[D] == ':')
{
MY_DATA1[D] = '0';
if(MY_DATA1[D+1] == 'F')
{
MY_DATA1[D+1] = '0';

if(MY_DATA1[D+2] == 'A')//switch 1 on
{
MY_DATA1[D+2] = '0';
digitalWrite(A0,LOW);
RX1 = 0;
}
else if(MY_DATA1[D+2] == 'B')//switch 1 off
{
MY_DATA1[D+2] = '0';
digitalWrite(A0,HIGH);
RX1 = 0;
}
else if(MY_DATA1[D+2] == 'C')//switch 2 on
{
MY_DATA1[D+2] = '0';
digitalWrite(A1,LOW);
RX1 = 0;
}
else if(MY_DATA1[D+2] == 'D')//switch 1 off
{
MY_DATA1[D+2] = '0';
digitalWrite(A1,HIGH);
RX1 = 0;
}
else if(MY_DATA1[D+2] == 'E')//switch 3 on
{
MY_DATA1[D+2] = '0';
digitalWrite(A2,LOW);
RX1 = 0;
          }
else if(MY_DATA1[D+2] == 'F')//switch 1 off
{
MY_DATA1[D+2] = '0';
digitalWrite(A2,HIGH);
RX1 = 0;
}
else if(MY_DATA1[D+2] == 'G')//switch 1 on
{
MY_DATA1[D+2] = '0';
digitalWrite(A3,LOW);
RX1 = 0;
}
else if(MY_DATA1[D+2] == 'H')//switch 1 off
{
MY_DATA1[D+2] = '0';
digitalWrite(A3,HIGH);
RX1 = 0;
}
     }
}
RX1 = 0;
}
}
void ESP8266_INIT()// initializing wi-fi module.
{
Serial.println("AT");// check AT mode.
delay(1000);
Serial.println("AT+RST");//RESET module.
delay(3000);
Serial.println("AT+CWSAP=\"Home Automation\",\"4d_innovations\",3,2");//set the ssid and password.
 delay(1500);
Serial.println("AT+CWMODE=3");// set ESP8266 in MODE 3 (Both mode AP+station).
delay(1400);
Serial.println("AT+CIPMUX=1");// set ESP8266 in MUX 1 (allow multiple connection).
delay(1400);
Serial.println("AT+CIPSERVER=1,80");// start communication ESP8266 on PORT80.
delay(1500);
}

-------------------------------------------------------------------------------------------------------------------

After the uploading sketch on the Board you can see the blue blinkey LED on the ESP8266 board .after the same time open the WI-fi on your phone.before that you must be install the “socket control” widget from the google play store,for install this Application click on this link: 
When you click on this link you can find this page on you desktop or phone.
                                                          
Fig.6

Install this App and open this from your widget setting like Fig.7
 
Fig.7
 
Fig.8

When you click the socket control it will display on your home page, open this widget by clicking the name below the button , it’s look like the Fig.9
 
Fig.9
 
Fig.10
Add the IP address of your ESP8266 module, hear IP Address is :”192.168.4.1”,and Port Address of module, hear PORT address is: “80” and last one is DATA which you want to send, hear data is:”FA”. you can see in Fig.11
 
Fig.11
You can Add Identical 8 button on your home page ,hear also fill the same IP address and PORT address,but DATA will be difference, as per the code DATA list for 8 button is listed below.
Switch-1 ON ==> data is “FA”.
Switch-1 OFF ==> data is “FB”.
Switch-2 ON ==> data is “FC”.
Switch-2 OFF ==> data is “FD”.
Switch-3 ON  ==> data is “FE”.
Switch-3 OFF ==> data is “FF”.
Switch-4 ON  ==> data is “FG”.
Switch-4 OFF ==> data is “FH”.

Now open the WI-FI control setting on your phone and turn on the WI-FI .Scan the new near by WI-FI network around you. when the scan is completed you can see the “Home Automation” SSID in your scan list . Click on this SSID and connect this SSID using the “4d_innovations”  password.like Fig.12
 
Fig.12


Now you can easily control your home Application using the ESP8266 Wi-Fi module.if this tutorial helpful for you then like and subscribe to my youtube channel for more helpful tutorials thank you.










Post a Comment

46 Comments

  1. Note: Remove The Rx and Tx pin form ESP2866 module while uploading sketch in board,Other wise it's gives the error in Arduino IDE. after that reconnect both pin as it is.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Hi buddy, i'm really pleased by your work and appreciate your hard work working with this project.I have a query and i hope you'll help me to sort it out.
    Can it be possible to control the device with only one switch instead of using two switch for ON/OFF operations? I believe that that will be much convenient to use,also it will save the space of a page on mobile getting used by each widget,also suggest me how to make my own application so that i could arrange Buttons in more specific way and according to requirement.I hope you'll look into my case and revert with an appropriate solution.Thanks!

    ReplyDelete
    Replies
    1. yes, it's possible to NO/OFF relay by one button. but for that you make some little change in code.you can used toggle sign for that.

      Delete
    2. This comment has been removed by the author.

      Delete
  4. thanku buddy for sharing your info about this.. it works fine but when AC 220V load is connected to the relay module. during switching the load, wifi module automatically restart and doesn't work properly.
    kindly guide me,

    ReplyDelete
    Replies
    1. yes,i sued with 220/230V AC supply with proper isolation.in your case any noise will triggering the reset signal of ESP8266 module.to avoid noise please check the proper grounding and also use the high current(2A or above 2A ) power supply.you can also put the extra capacitor near the ESP8266 module.

      Delete
  5. thank you soo much bro ...
    thanks a lot.. for this wonderfull idea

    ReplyDelete
  6. I am getting this error msg, what does it mean? Where is the erroe , and how to correct it?
    avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x41
    avrdude: stk500_getsync() attempt 2 of 10: not in sync: resp=0x54
    avrdude: stk500_getsync() attempt 3 of 10: not in sync: resp=0x2b
    avrdude: stk500_getsync() attempt 4 of 10: not in sync: resp=0x52
    avrdude: stk500_getsync() attempt 5 of 10: not in sync: resp=0x53
    avrdude: stk500_getsync() attempt 6 of 10: not in sync: resp=0x54
    avrdude: stk500_getsync() attempt 7 of 10: not in sync: resp=0x0d
    avrdude: stk500_getsync() attempt 8 of 10: not in sync: resp=0x0a
    avrdude: stk500_getsync() attempt 9 of 10: not in sync: resp=0x41
    avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0x54
    An error occurred while uploading the sketch

    ReplyDelete
  7. The error msg were probably coming from the tx rx pins which i forgot to remove, now the error msg are gone, code uploaded successfully, but the led on esp is not blinking also the wifi on the chip is not turned on.. What should i do?

    ReplyDelete
  8. I tried to update the firmware of the esp chip again, it is showing "invalid head of packet", pls tell me where am i going wrong.

    ReplyDelete
  9. first load the simple serial transmit and receive code on Arduino UNO.(remove "Rx" & "Tx" pin of ESP8266 module while code uploading on board ). then send the simple AT command(AT,AT+RST,AT+CWMODE=?,etc) to ESP8266 module and check what you receive on serial port. if all command work well then no need to update firmware.

    ReplyDelete
  10. Arduino: 1.6.13 (Windows 10), Board: "Arduino/Genuino Uno"

    Sketch uses 2,592 bytes (8%) of program storage space. Maximum is 32,256 bytes.
    Global variables use 350 bytes (17%) of dynamic memory, leaving 1,698 bytes for local variables. Maximum is 2,048 bytes.
    An error occurred while uploading the sketch
    avrdude: ser_open(): can't open device "\\.\COM4": The system cannot find the file specified.

    This report would have more information with
    "Show verbose output during compilation"
    option enabled in File -> Preferences.

    ReplyDelete
  11. I TRIED TO INCREASE THE NUMBER OF DEVICES BY GIVING SOME PIN MODES AND ALTERING THE CODE I UPLOADED IT BUT WEN I SWITCH IT OWN IT SHOWS OK IN PHONE AND DEVICE DOES'NT TURN ON,HELP ME !!!

    ReplyDelete
  12. make sure that the data you send to ESP module is valid on switch case or not ?..
    also check the received string for cross verification.

    ReplyDelete
  13. This comment has been removed by a blog administrator.

    ReplyDelete
  14. This comment has been removed by a blog administrator.

    ReplyDelete
  15. i have made all connections still not working. plz help

    ReplyDelete
  16. Sir,
    We uploaded sketch through Uno eg: EspWifiScan, Blinky. But now ESP8266 is not responding to AT commands. So we're unable to upload your sketch. So has our module damaged or is it fine? How should we proceed?

    ReplyDelete
  17. working voltage of ESP8266 is 3.3V,do not give Vcc 5V it may damage you ESPmodule.
    check:
    1) Rx and Tx connection.
    2) check simple AT command manually.
    3) hard reset.
    4) flash ESP8266 module again.(it may work)

    ReplyDelete
  18. This comment has been removed by the author.

    ReplyDelete
  19. I did everything as you said but when using application I face with "1 command failed" . So I couldnt solve where is my fault?

    ReplyDelete
    Replies
    1. check your cell phone is connected to ESP8266 module or not?
      also check IP address and port address you entered in Button Setting.

      Delete
  20. Sir can you suggest firmware & flshtool for esp8266 for this project? Once i changed firmware so now i cant use this code so please suggest firware & flashtool require for this project.

    ReplyDelete
  21. I have seen your video and its very nice.I have done all the things just as like in ur video.My phone is also connected to wifi but whenever i send command through socket control widget its shows command failed.I have also seen my ip address by AT command its shows 192.168.4.2 and i have write same ip in socket control widget but still its shows same "command fail".
    Please help me sir.

    ReplyDelete
  22. check.
    1) your port address.
    2) check firmware of ESP8266-01 module.

    ReplyDelete
  23. AFTER UPLOADING THE WIFI IS NOT VISIBLE ON WIFI MENU

    ReplyDelete
  24. set ESP in MODE 3 (Both mode AP+Station) using AT+CWMODE=3 command.

    ReplyDelete
  25. Hello,
    I have made all the connections as per the video. but I cant find the network (that is Home automation) in my mobile. Is that code only enough for the project? please tel me the solution.

    ReplyDelete
  26. Can I use WIFI MCU app instead of that app?

    ReplyDelete
  27. the coded you are provided has an error for me. here:

    Arduino: 1.6.9 (Windows 7), Board: "Generic ESP8285 Module, 80 MHz, 115200, 1M (512K SPIFFS)"

    The sketch name had to be modified. Sketch names can only consist
    of ASCII characters and numbers (but cannot start with a number).
    They should also be less than 64 characters long.










    C:\Users\ShaunRed\Desktop\new_repaly\new_repaly.ino: In function 'void setup()':

    new_repaly:7: error: 'A1' was not declared in this scope

    pinMode(A1,OUTPUT);

    ^

    new_repaly:8: error: 'A2' was not declared in this scope

    pinMode(A2,OUTPUT);

    ^

    new_repaly:9: error: 'A3' was not declared in this scope

    pinMode(A3,OUTPUT);

    ^

    C:\Users\ShaunRed\Desktop\new_repaly\new_repaly.ino: In function 'void loop()':

    new_repaly:54: error: 'A1' was not declared in this scope

    digitalWrite(A1,LOW);

    ^

    new_repaly:60: error: 'A1' was not declared in this scope

    digitalWrite(A1,HIGH);

    ^

    new_repaly:66: error: 'A2' was not declared in this scope

    digitalWrite(A2,LOW);

    ^

    new_repaly:72: error: 'A2' was not declared in this scope

    digitalWrite(A2,HIGH);

    ^

    new_repaly:78: error: 'A3' was not declared in this scope

    digitalWrite(A3,LOW);

    ^

    new_repaly:84: error: 'A3' was not declared in this scope

    digitalWrite(A3,HIGH);

    ^

    exit status 1
    'A1' was not declared in this scope

    This report would have more information with
    "Show verbose output during compilation"
    option enabled in File -> Preferences.

    ReplyDelete
  28. dear sir
    I am getting this error msg, what does it mean? Where is the erroe , and how to correct it?
    Please solve this problem....

    C:\Users\MAA\Documents\Arduino\sketch_nov14a\sketch_nov14a.ino: In function 'void loop()':

    sketch_nov14a:96: error: a function-definition is not allowed here before '{' token

    {

    ^

    sketch_nov14a:110: error: expected '}' at end of input

    }

    ^

    exit status 1
    a function-definition is not allowed here before '{' token

    ReplyDelete
  29. Sir i have to used .. wifi scane code .. but my esp8266 was not work .. serial moniter can say.. wifi shield not present... what can i do to solv it

    ReplyDelete
  30. A debt of gratitude is in order for giving late reports with respect to the worry, I anticipate read more. alarm companies mcallen texas

    ReplyDelete
  31. After uploading, it will work but when I connect to wifi second time, it will not work, if i connect it with retyping the password then only it starts working ,please send solution

    ReplyDelete
    Replies
    1. i think it is problem of your phone wi-fi ,try with other smart phone and check this.

      Delete
    2. But it is happening with all phones, any changes in program??

      Delete
  32. Dear Janak
    while compiling sketch i found following error kindly sole it.

    Arduino: 1.8.5 (Windows 7), Board: "Arduino/Genuino Uno"

    C:\Users\B M SINGH\Documents\Arduino\wify_home_automation_with_widget\wify_home_automation_with_widget.ino: In function 'void setup()':

    wify_home_automation_with_widget:17: error: 'ESP8266_INIT' was not declared in this scope

    ESP8266_INIT();

    ^

    C:\Users\B M SINGH\Documents\Arduino\wify_home_automation_with_widget\wify_home_automation_with_widget.ino: In function 'void loop()':

    wify_home_automation_with_widget:80: error: expected '}' at end of input

    }

    ^

    wify_home_automation_with_widget:80: error: expected '}' at end of input

    wify_home_automation_with_widget:80: error: expected '}' at end of input

    wify_home_automation_with_widget:80: error: expected '}' at end of input

    exit status 1
    'ESP8266_INIT' was not declared in this scope

    This report would have more information with
    "Show verbose output during compilation"
    option enabled in File -> Preferences.

    ReplyDelete
    Replies
    1. define ESP8266_INIT(); function above void setup() function

      Delete
  33. i don't know how to get IP address of ESP 8266. pl. help me.

    ReplyDelete
  34. This comment has been removed by the author.

    ReplyDelete