ESP32 Web Server Android APP| Home Automation | Build Application

ESP32 Web Server Android APP| Home Automation | Build Application

This project is very simple but a amazing  IOT homeautomation project, we control our homeappliances by an Android APP within a local area network. 

Components Required

1.ESP32 Board

2. Designed PCB( you could use relay instead of PCB)

3.BC547 Transistor.

4. IN4007 Diode

5. 1k ohm Transistor.

6. Relay(5volt)


CODE







#include <WiFi.h> // Uncomment this if you are using ESP32
//Web Server App Control Home Automation
//#include <ESP8266WiFi.h> //Uncomment this if you are using NodeMCU
WiFiClient client;
WiFiServer server(80);

/* WIFI settings */
const char* ssid = "REPLACE_WITH_YOUR_SSID";   //WIFI SSID
const char* password = "REPLACE_WITH_YOUR_PASSWORD";    //WIFI PASSWORD

/* data received from application */
String  data =""; 


int Relay1 = 26;    
int Relay2 = 27;    
int Relay3 = 14;     
int Relay4 = 12;
int Relay5 = 13;      


void setup()
{
  /* initialize motor control pins as output */
  pinMode(Relay1, OUTPUT);
  pinMode(Relay2, OUTPUT); 
  pinMode(Relay3, OUTPUT);  
  pinMode(Relay4, OUTPUT);
  pinMode(Relay5, OUTPUT);

  digitalWrite(Relay1,LOW);
  digitalWrite(Relay2,LOW);
  digitalWrite(Relay3,LOW);
  digitalWrite(Relay4,LOW);
  digitalWrite(Relay5,LOW);
  
  /* start server communication */
  Serial.begin(115200);
  connectWiFi();
  server.begin();
}

void loop()
{
    /* If the server available, run the "checkClient" function */  
    client = server.available();
    if (!client) return; 
    data = checkClient ();
Serial.print(data);
/************************ Run function according to incoming data from application *************************/

    
    if (data == "Relay1on")
    { 
      digitalWrite(Relay1,HIGH);
      }
    
    else if (data == "Relay1off")
    {
      digitalWrite(Relay1,LOW);
      }

    else if (data == "Relay2on")
    {
      digitalWrite(Relay2,HIGH);
      }
      
    else if (data == "Relay2off")
    {
      digitalWrite(Relay2,LOW);
      }
      
    else if (data == "Relay3on")
    {
      digitalWrite(Relay3,HIGH);
      }
      
    else if (data == "Relay3off")
    {
      digitalWrite(Relay3,LOW);
      }
      
    else if (data == "Relay4on")
    {
      digitalWrite(Relay4,HIGH);
      }
      
    else if (data == "Relay4off")
    {
      digitalWrite(Relay4,LOW);
      }
       else if  (data == "Relay5on")
    { 
      digitalWrite(Relay1,HIGH);
      }
    
    else if (data == "Relay5off")
    {
      digitalWrite(Relay1,LOW);
      }
      
} 

void connectWiFi()
{
  Serial.println("Connecting to WIFI");
  WiFi.begin(ssid, password);
  while ((!(WiFi.status() == WL_CONNECTED)))
  {
    delay(300);
    Serial.print("..");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("ESP32 Local IP is : ");
  Serial.print((WiFi.localIP()));
}
/********************************** RECEIVE DATA FROM the APP ******************************************/
String checkClient (void)
{
  while(!client.available()) delay(1); 
  String request = client.readStringUntil('\r');
  request.remove(0, 5);
  request.remove(request.length()-9,9);
  return request;
}

Network Credentials

Insert your network credentials in the following variables.

const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";

Video Tutorial

Leave a Reply

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