Home Automation using Arduino and ESP8266 WiFi Module with Android Application

In this blog I will explain how to make a home automation system using TCP/IP client server here I will use an Android application to demonstrate such implementation. Here I have used Arduino and ESP8266 as main components of this implementation, as I have discussed in detail about ESP8266 Wi-Fi module and its AT commands in my previous blog Getting started with ESP8266 Wi-Fi Module , In this blog I will explain in detail about how to program Arduino uno to send AT command automatically to setup ESP8266 as TCP server to send or receive status as instruction from Android app to Arduino uno and vice versa, to control single relay connected with Arduino.

Transfer Control Protocol

The Transfer Control Protocol is one of the main protocol of the internet protocol suit. The entire protocol is commonly referred to as TCP/IP because it originated the initial network implementation in which it correlated the Internet Protocol

TCP Server

The TCP server is implemented on computer or any network connected device, the router assign it an IP address and port number is assigned to it manually, to set it as a TCP server, it wait until a TCP client device request has been initiated once the TCP connection has been initiated, the server can send and receive data to and from TCP client through the corresponding port assigned. This mode is useful when users want to send or receive data through the same network, port forwarding could be an option.

TCP Client

The TCP client mode is implemented on a device on a network to exchange data with the TCP server, the TCP client has required the IP address of TCP server and its port number to communicate with it, once a TCP session is established between the two, the device server can send or receive data to and from the TCP client. This mode is useful when users want to establish a connection with TCP server to exchange data through the port on same network.

Components

Schematic

All the connection are same as referred to my previous blog Getting started with ESP8266 Wi-Fi Module, only modification is that I have connected the relay circuit to I/O pin 4 of Arduino uno as shown in figure above and I have made this circuit on veroboard as shown below, you can make that circuit on breadboard, if you are beginner to electrical or high voltage circuit be careful about the electric hazard and take help from any electrical expert.


You can also buy 5V ESP8266 Dual WiFi Relay Module Internet Of Things Smart Home Mobile APP Remote Switch and program in most probably same way as it  can be connected to Arduino Uno, it is two channel module.

 Watch the tutorial for good understanding


#include <SoftwareSerial.h>
#define DEBUG true
SoftwareSerial soft(2,3);
void setup(){
soft.begin(9600);
Serial.begin(9600);
pinMode(4,OUTPUT);
//Serial.begin(9600);
sendCommand("AT+RST\r\n",2000,DEBUG); // reset module
sendCommand("AT+CWMODE=1\r\n",1000,DEBUG); // configure as access point
sendCommand("AT+CWJAP=\"STRIVERS\",\"nothingelse\"\r\n",3000,DEBUG);
delay(10000);
sendCommand("AT+CIFSR\r\n",1000,DEBUG); // get ip address
sendCommand("AT+CIPMUX=1\r\n",1000,DEBUG); // configure for multiple connections
sendCommand("AT+CIPSERVER=1,8080\r\n",1000,DEBUG); // turn on server on port 80

Serial.println("Server Ready");
}

void loop()
{
if(soft.available()>0) // check if the esp is sending a message
{

// get the connection id so that we can then disconnect
char connectionId = soft.read(); // subtract 48 because the read() function returns
Serial.print(connectionId); // the ASCII decimal value and 0 (the first decimal number) starts at 48
if(connectionId=='A'){
digitalWrite(4,HIGH);
}
if(connectionId=='B'){
digitalWrite(4,LOW);
}

}
}

String sendCommand(String command, const int timeout, boolean debug)
{
String response = "";

soft.print(command); // send the read character to the Serial

long int time = millis();

while( (time+timeout) > millis())
{
while(soft.available())
{

// The esp has data so display its output to the serial window
char c = soft.read(); // read the next character.
response+=c;
}
}

if(debug)
{
Serial.print(response);
}

return response;
}
 

 

 

Android Application APK File and Code

https://drive.google.com/open?id=1wta1L3A2mAKeOyrm0hN5sOfK1b1G-NL4

https://drive.google.com/open?id=1QIoL_TVjWarAeBJnbfzCLXpCa5M1fsAs

Want your project done? follow me on Fiverr

null

2 thoughts on “Home Automation using Arduino and ESP8266 WiFi Module with Android Application

  1. Hello and thank you very much for this editing and the good explanation, against the application does not work even if we do the IP address and port.
    Also, even if I put the ip on google hrome browser, the relay is closed several times and opened at the same time.
    thanks for checking some parameters.
    thank you again for this beautiful tutorial

  2. Thank you so much for appreciating the effort, it really worth.I would like to tell you that this won’t work on chrome unless you develop tcp/ip client based web application that can run in chrome.
    about the android application, confirm that both Arduino and Android phone are connected to same Wi-Fi network

Leave a Reply

Your email address will not be published. Required fields are marked *

Copied!