top of page

ESP32 WiFi Controlled Home Automation and Android

Updated: Jul 27


In this tutorial, I 'll Publish how to controlling LED On/Off using web server over WiFi using ESP32 and android application. An Android application using the MIT app inventor that allows you to control the ESP32 GPIO pins and digital read the GPIO pins status.


Components Required

ESP32S Development Board - 1 no

LED - 4 nos

330ohms Resistor - 4nos

Jumper wires


Circuit Diagram



Installing the library

To install the library navigate to the Sketch > Include Library > Manage Libraries… Wait for Library Manager to download libraries index and update list of installed libraries.


Download  WiFi manger Library , we need to use this library for WiFi function.


In your Arduino IDE, to install the libraries go to Sketch > Include Library > Add .ZIP library… and select the library you’ve just downloaded.


After Arduino IDE installed, there is no package to support ESP32-S2, we need to install the ESP32 package in Arduino IDE to continue.


 Select “File>Preferences>settings>Additional Boards Manager URLs” to fill the link: https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_dev_index.json 



arduino code

**************************************

#include <WiFiManager.h>


#define LED1 13

#define LED2 12

#define LED3 27

#define LED4 32


String State = "";

WiFiServer server(80);


#include "soc/soc.h"

#include "soc/rtc_cntl_reg.h"


void setup() {


Serial.begin(115200);

pinMode(LED1, OUTPUT);

pinMode(LED2, OUTPUT);

pinMode(LED3, OUTPUT);

pinMode(LED4, OUTPUT);

WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //disable brownout detector

// WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP

// it is a good practice to make sure your code sets wifi mode how you want it.

//WiFiManager, Local intialization. Once its business is done, there is no need to keep it around

WiFiManager wm;


// reset settings - wipe stored credentials for testing

// these are stored by the esp library

// wm.resetSettings();


// Automatically connect using saved credentials,

// if connection fails, it starts an access point with the specified name ( "AutoConnectAP"),

// if empty will auto generate SSID, if password is blank it will be anonymous AP (wm.autoConnect())

// then goes into a blocking loop awaiting configuration and will return success result


bool res;

// res = wm.autoConnect(); // auto generated AP name from chipid

// res = wm.autoConnect("AutoConnectAP"); // anonymous ap

res = wm.autoConnect("ConnectAP","1234567890"); // your choice AP & password protected ap


if(!res) {

Serial.println("Failed to connect");

// ESP.restart();

}

else {

//if you get here you have connected to the WiFi

Serial.println("connected...yeey :)");

}

Serial.println("Connected with WiFi");

server.begin();

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

Serial.println(WiFi.localIP());

}


void loop() {


WiFiClient client = server.available();

if (!client) {

return;

}


String req = client.readStringUntil('\r');

Serial.println(req);

req.replace("+", " ");

req.replace(" HTTP/1.1", "");

req.replace("GET /", "");


if (req.indexOf("1on") != -1) {digitalWrite(LED1, HIGH); State = "11,,,";}

if (req.indexOf("1off") != -1) {digitalWrite(LED1, LOW); State = "10,,,";}

if (req.indexOf("2on") != -1) {digitalWrite(LED2, HIGH); State = "21,,,";}

if (req.indexOf("2off") != -1) {digitalWrite(LED2, LOW); State = "20,,,";}

if (req.indexOf("3on") != -1) {digitalWrite(LED3, HIGH); State = "31,,,";}

if (req.indexOf("3off") != -1) {digitalWrite(LED3, LOW); State = "30,,,";}

if (req.indexOf("4on") != -1) {digitalWrite(LED4, HIGH); State = "41,,,";}

if (req.indexOf("4off") != -1) {digitalWrite(LED4, LOW); State = "40,,,";}

if (req.indexOf("Status") != -1){

State ="";

if (digitalRead(LED1) == HIGH) {State = "11,";} else {State = "10,";}

if (digitalRead(LED2) == HIGH) {State = State + "21,";} else {State = State + "20,";}

if (digitalRead(LED3) == HIGH) {State = State + "31,";} else {State = State + "30,";}

if (digitalRead(LED4) == HIGH) {State = State + "41,";} else {State = State + "40,";}

}


client.println("HTTP/1.1 200 OK");

client.println("Content-Type: text/html");

client.println("");

client.println(State);


client.flush();

client.stop();

Serial.println("Client disconnected.");

}


**************************************



After a successful upload, open the Serial Monitor at a baud rate of 115200. Press the “EN/RST” button on the ESP32 board.



ANDROID APPLICATION

The Android app developers generally use JAVA language, but this Android app can also build without knowing the Java language. This app inventor is specially designed for Block programmers those who don’t know the JAVA language.


MIT main page






Demo:



Comments


bottom of page