Home Automation Using Arduino and ESP8266 Module
Home Automation Using Arduino and ESP8266 Module
HOME / PROJECTS, EMBEDDED, ARDUINO, IOT, ESP8266 / HOME AUTOMATION USING ARDUINO AND ESP8266 MODULE
In this project we are going to make a home automation system using ESP8266 WiFi module
Contents
and Arduino Uno. Using this we will be able to control lights, electric fan and other home
appliances through a web browser using your PC or mobile. These AC mains appliances will be 1 ESP-01 ESP8266 Module
connected to relays which are controlled by the Arduino. ESP8266 and Arduino together acts as 2 Circuit Diagram and Explanation
a Web Server and we will send control commands through a Web Browser like Google Chrome 3 Program
or Mozilla Firefox. ESP8266 is the one of the most popular and low cost wi module available in 3.1 Arduino Sketch
the market today. You can ready more about it here, ESP8266 – WiFi SoC. 3.1.1 Explanation
3.2 HTML Code
3.2.1 jQuery
ESP-01 ESP8266 Module 3.2.2 Explanation
4 How to make it work ?
5 Video
ESP-01 is the one of the most popular ESP8266
module available in the market. ESP8266 is a self
contained SoC with integrated TCP/IP stack which helps any microcontroller having UART
to access a wi network. It can act as both WiFi access point as well as a WiFi client. It is
pre-programmed with AT commands, so we can easily access and con gure it using a
microcontroller.
ESP8266 runs on 3.3V and its input pins are not 5V tolerant. So we need to reduce the 5V
output of the Arduino Tx pin to 3.3V by using voltage dividing resistors to connect to Rx
pin of ESP8266 module. Arduino TTL input pins will detect 3.3V as logic high, so we can
directly connect 3.3V output of ESP8266 Tx to Arduino Rx pin.
https://electrosome.com/home-automation-arduino-esp8266/ 1/13
12/13/2019 Home Automation using Arduino and ESP8266 Module
First we can connect ESP8266 with the Arduino Uno. The ESP8266 runs on 3.3V, it may damage if you connect it directly to 5V from
Arduino. The pin out of the ESP-01 ESP8266 module is shown below.
Connect the VCC and CH_PD of the ESP8266 to the 3.3V output pin of Arduino.
CH_PD is Chip Power Down pin, which is active low. So we will give 3.3V to it,
which will enable the chip. Then connect the TXD pin of the ESP8266 with the
digital pin 2 of the Arduino. Then make a voltage divider to make 3.3V for the
RXD of the ESP8266 which is connected to the pin 3 of Arduino. Here we are
using software UART through digital pins 2 & 3 of Arduino. Lastly, connect the
ground of the ESP8266 with the ground of the Arduino.
ESP-01 ESP8266 Module Pinout
Now we can connect relays to Arduino. Connect three relays to pins 11, 12 and
13 of the Arduino. Also connect 5V and ground from the Arduino to power the relay. Note that here I am using relay modules which
having built in transistor driver. So don’t forget to add driver when you are using bare relays. We can connect AC devices to the
output terminals of those relays. First connect one wire (Phase) of the AC source with the common terminal (COM) of all relays and
the second wire (Neutral) of AC source to one terminal of AC devices. Then connect the other terminal of AC devices to the NO
(Normally Open) terminal of relays.
Program
Arduino Sketch
https://electrosome.com/home-automation-arduino-esp8266/ 2/13
12/13/2019 Home Automation using Arduino and ESP8266 Module
void setup()
{
Serial.begin(9600); // Setting the baudrate to 9600
esp8266.begin(9600); // Set it according to your esp’s baudrate. Different esp’s have different baud rates.
pinMode(11,OUTPUT); // Setting the pin 11 as the output pin.
digitalWrite(11,LOW); // Making it low.
pinMode(12,OUTPUT); // Setting the pin 12 as the output pin..
digitalWrite(12,LOW); // Making pin 12 low.
pinMode(13,OUTPUT); // Setting the pin 13 as the output pin.
digitalWrite(13,LOW); // Making pin 13 low.
sendData("AT+RST\r\n",2000,DEBUG); //This command will reset module to default
sendData("AT+CWMODE=2\r\n",1000,DEBUG); // This will configure the mode as access point
sendData("AT+CIFSR\r\n",1000,DEBUG); // This will get ip address and will show it
sendData("AT+CIPMUX=1\r\n",1000,DEBUG); // This will configure the ESP8266 for multiple connections
sendData("AT+CIPSERVER=1,80\r\n",1000,DEBUG); // This will set the server on port 80
}
void loop()
{
if(esp8266.available()) // Checking that whether the esp8266 is sending a message or not (Software UART Data)
{
if(esp8266.find("+IPD,"))
{
delay(1000); // Waiting for 1 sec
int connectionId = esp8266.read()-48; // Subtracting 48 from the character to get the number.
esp8266.find("pin="); // Advancing the cursor to the "pin="
int pinNumber = (esp8266.read()-48)*10; // Getting the first number which is pin 13
pinNumber += (esp8266.read()-48); // This will get the second number. For example, if the pin number is 13 then the
2nd number will be 3 and then add it to the first number
digitalWrite(pinNumber, !digitalRead(pinNumber)); // This will toggle the pin
// The following commands will close the connection
String closeCommand = "AT+CIPCLOSE=";
closeCommand+=connectionId;
closeCommand+="\r\n";
sendData(closeCommand,1000,DEBUG); // Sending the data to the ESP8266 to close the command
}
}
}
String sendData(String command, const int timeout, boolean debug) // Function to send the data to the esp8266
{
String response = "";
esp8266.print(command); // Send the command to the ESP8266
long int time = millis();
while( (time+timeout) > millis()) // ESP8266 will wait for some time for the data to receive
{
while(esp8266.available()) // Checking whether ESP8266 has received the data or not
{
char c = esp8266.read(); // Read the next character.
response+=c; // Storing the response from the ESP8266
}
}
if(debug)
{
Serial.print(response); // Printing the response of the ESP8266 on the serial monitor.
}
return response;
}
Explanation
The code is very long but easy to understand as it is well commented. First we will initialize the software uart with digital pins 2 & 3
of Arduino for the communication with ESP8266. After that we will initialize pins to which we will connect relays as output pins. Then
we will con gure ESP8266 in access point mode. Arduino + ESP8266 is programmed as a web server such that we can control those
https://electrosome.com/home-automation-arduino-esp8266/ 3/13
12/13/2019 Home Automation using Arduino and ESP8266 Module
HTML Code
<html>
<head>
<title>Home Automation System</title> <!-- This will be the page title -->
</head>
<body> <!-- All the data in it will be shown on the page -->
<button id="11" class="led">Toggle Pin 11</button> <!-- This will create the button for pin 11 -->
<button id="12" class="led">Toggle Pin 12</button> <!-- This will create the button for pin 12 -->
<button id="13" class="led">Toggle Pin 13</button> <!-- This will create the button for pin 13 -->
<script src="jquery.min.js"></script> <!-- This will read the script from the jquery -->
<script type="text/javascript">
$(document).ready(function(){
$(".led").click(function(){
var p = $(this).attr('id'); // Getting the id value that which pin to toggle, pin 13 or pin 13 or pin 11
// Sending the request to the ip address of the server with the pin number to toggle the
pin
$.get("http://192.168.4.1:80/", {pin:p}); // Sending the get request
});
});
</script>
</body>
</html>
jQuery
The above HTML code uses the open source javascript library JQuery, so we need to obtain that. You can get it from this link and
save it by right clicking on it. Save it in the same directory where you are going to save the above HTML code. Save the JQuery library
le as the jquery.min and the full name of the le will be “jquery.min.js”.
Then copy above HTML code in a le and save it as an HTML le, say ‘control.html’. You can open this HTML le in a web browser,
which will looks as shown below.
Explanation
Whenever a button is pressed in the web browser, a HTTP GET request is send to the Arduino-ESP8266 webserver to toggle a
particular pin. In the Arduino code, it looks for the string “+IPD” again and again to check that if any request is coming or not. When
it gets a request it reads the next character which is the connection id. Connection id is required to close the particular TCP
connection after processing the request. Then it looks for the pin number to toggle by checking the character after the string “?pin=”.
The Arduino then toggles that particular digital pin, which toggles the relay.
https://electrosome.com/home-automation-arduino-esp8266/ 4/13
12/13/2019 Home Automation using Arduino and ESP8266 Module
First connect your system to the access point created by ESP8266 module (ESP_xxxxxx). Update the IP address in the
control.html le created above. Now open the le using your web browser (Google Chrome or Mozilla Firefox). Now you can control
your home appliances very easily.
Note : You may change the default SSID (ESP_xxxxxx) of ESP8266 WiFi module by using AT+CWSAP command.
Video
https://electrosome.com/home-automation-arduino-esp8266/ 5/13
12/13/2019 Home Automation using Arduino and ESP8266 Module
Related Posts:
Author
Ali Hamza
https://electrosome.com/home-automation-arduino-esp8266/ 6/13
12/13/2019 Home Automation using Arduino and ESP8266 Module
Comments (30)
Toobeeraj Panchoo
Hello, I would like to have a full circuit including the breadboard with its resistors and more on this home automation,
anyone please its terribly urgent
January 21, 2017 at 10:57 am
Toobeeraj Panchoo
where u got UART software
January 21, 2017 at 11:37 am
Jon Arotzarena
Can you provide details on ashing the esp8266? What do I need to do to get the ESP to connect to my home network?
February 1, 2017 at 12:39 am
Ayodeji
Can i use a 4 channel relay for this project. (4 Channel 5V Relay Module 10A)
February 22, 2017 at 4:03 pm
Lim Wsing
how to design ESP8266 using protues software?
February 23, 2017 at 6:22 pm
ASHIK LAL
HI,i had tried this
everything is alright but my esp-01 is not showing anything after opening serial monitor what can i do relays are on when
connecting power supply
March 31, 2017 at 11:35 am
Wejdan
I faced the same problem
did you solve it?
April 15, 2017 at 9:27 pm
Ken
can html in esp8266?
April 21, 2017 at 11:04 pm
berlin benilo
sir,when a DC source was provided instead of this Ac source by connecting with the DC motor and bulb for an mini level
project,did any fault obtained for the design….?.please answer me sir
August 22, 2017 at 11:02 pm
sanjaysan
it can be controlled by any android apps
September 22, 2017 at 10:28 pm
https://electrosome.com/home-automation-arduino-esp8266/ 7/13
12/13/2019 Home Automation using Arduino and ESP8266 Module
sanjaysan
it can be controlled by any apps
September 22, 2017 at 10:48 pm
Abdul Rehman
I want to control 8 relays and controll it by android app
October 4, 2017 at 4:33 pm
shubham rajguru
no prolem to do this .i also do this mini project
December 5, 2017 at 7:24 pm
Ligo George
Try this : https://electrosome.com/home-automation-bluetooth-mobile-app/
December 28, 2017 at 9:57 am
Ganesh
Sir, I getting the Warnings in the Arduiono Sketch.
:My Project FilesAruinoProgram8266sketch_jan08bsketch_jan08b.ino:26:28: warning: deprecated conversion from string
constant to ‘char*’ [-Wwrite-strings]
if(esp8266. nd(“+IPD,”))
^
January 8, 2018 at 4:07 pm
somnath panja
https://uploads.disquscdn.com/images/e3d442748079ca056a4a432861aed82e148bae0c306b40044bc580c57192424c.png
https://uploads.disquscdn.com/images/2c66cd5e116863bd1d63e0a446e5966e514ef36b94ec5cbbc3ab7bfe8157344a.png
do not come ip address…why ??? arduino program is ok ????
January 10, 2018 at 11:08 am
Vidit Naithani
Dude how did you solve this problem
February 11, 2018 at 10:46 pm
Pramod Bhogi
hloo sir,
I executed this programme,but nothing is showing for me on serial monitor.
so what to do..?
February 14, 2018 at 7:54 pm
Dahen Ece
why my esp does not show anything to serial monitor and also it does not response to any command!
February 21, 2018 at 1:00 am
Omar Akar
hi , I’m working on a senior project which i can control the controller of hdmi switch by using relays and arduino , I connect
the wires as the picture above , and copied the arduino code , but there is no output on serial monitor . what is the problem
? thank you .
https://electrosome.com/home-automation-arduino-esp8266/ 8/13
12/13/2019 Home Automation using Arduino and ESP8266 Module
david K
Instead of using pin number {pin:p} directly in the code i want to activate it by a code. Say when i send “onrelay1” it should
activate pin.
E.g 192.168.4.1/?pin=onrelay1
Instead of 192.168.4.1/?pin=11
June 28, 2018 at 6:23 pm
Somto Akuekwe
i must thank you for such a wonderful project. Please can this be used to control relays from as far o as a persons o ce
June 28, 2018 at 7:31 pm
Karthik Raja
Hi for me also the serial monitor showing empty…
How you upload the program? Is Esp-01 module need to be program separately and arduino program seperately???
Or Is the given single programm enough?
Can you please explain in detail for programming steps
July 7, 2018 at 8:25 pm
abdul gha ar
i am getting the same blank serial monitor…can some help me please…
August 1, 2018 at 11:17 am
chetan gautam
hello sir ,
I want to get IP address without using serial PORT . How can we do this. If is it possible . Can we print on Web page.
September 12, 2018 at 3:24 pm
achu s
sir tell me if controll fan speed externally with regulator at same time ..how can poosible???
February 4, 2019 at 10:54 pm
achu s
give some ideas
February 4, 2019 at 10:54 pm
Mahesh b
We are not getting the IP address . Please explain how to get it
March 28, 2019 at 5:28 pm
manu
hi,
I am getting this error:
ssize_t’ does not name a type ssize_t m_swsInstsIdx = -1;
when i try to Verify the above code you have given..Please help to solve ASAP
June 21, 2019 at 12:22 pm
Leave a Reply
Your email address will not be published. Required elds are marked *
https://electrosome.com/home-automation-arduino-esp8266/ 9/13
12/13/2019 Home Automation using Arduino and ESP8266 Module
Comment
Name *
Email *
Website
Save my name, email, and website in this browser for the next time I comment.
POST COMMENT
RELATED POSTS
RECENT POSTS
https://electrosome.com/home-automation-arduino-esp8266/ 10/13
12/13/2019 Home Automation using Arduino and ESP8266 Module
October 8, 2019
FOLLOW US
MOST VIEWED
What is a Microprocessor ?
by Ligo George
Transistor as a Switch
by Ligo George
CATEGORIES
Electronics
555 Circuits
Basic Electronics
https://electrosome.com/home-automation-arduino-esp8266/ 11/13
12/13/2019 Home Automation using Arduino and ESP8266 Module
Opamp
Electronics News
IC
Power Supply
Sensors
What is Inside?
Featured
Mobiles
News
Processors
Projects
Embedded
Arduino
AVR Microcontroller
ESP8266
PIC Microcontroller
Hobby Circuits
IoT
Softwares
Tablets
Technology Transfer
Tutorials
8051 Microcontroller
8085
Arduino
ARM
ARM7
ATMEL AVR
CloudX
ESP8266
MATLAB
OpenWrt
PCB Designing
DipTrace
OrCad
PIC Microcontroller
CCS C Compiler
Hi-Tech C
MikroC
MPLAB XC8
Python
https://electrosome.com/home-automation-arduino-esp8266/ 12/13
12/13/2019 Home Automation using Arduino and ESP8266 Module
Raspberry Pi
Python Rpi
SCILAB
MOST COMMENTED
TAGS
3D 16F877A 555 8051 MICROCONTROLLER ANDROID ARDUINO ARDUINO UNO ATMEGA32 ATMEL DC MOTOR DHT22
ELECTRONICS EMBEDDED ESP8266 GOOGLE HI-TECH C IOT L293D LCD LED MATLAB MICROCONTROLLER MIKROC
MOBILE MOTOR MPLAB MPLAB XC8 OP-AMP PCB PIC PROTEUS PWM PYTHON RASPBERRY PI RFID SAMSUNG
SENSOR SENSORS SERVO MOTOR SMARTPHONE TABLET TRANSISTOR TRANSISTORS TUTORIALS XC8
RECENT COMMENTS
FOLLOW US
https://electrosome.com/home-automation-arduino-esp8266/ 13/13