• Home
  • Archive
  • Tools
  • Contact Us

The Customize Windows

Technology Journal

  • Cloud Computing
  • Computer
  • Digital Photography
  • Windows 7
  • Archive
  • Cloud Computing
  • Virtualization
  • Computer and Internet
  • Digital Photography
  • Android
  • Sysadmin
  • Electronics
  • Big Data
  • Virtualization
  • Downloads
  • Web Development
  • Apple
  • Android
Advertisement
You are here: Home » How To Easily Create Android App Controlled Local Devices With ESP32

By Abhishek Ghosh September 3, 2021 8:02 pm Updated on September 3, 2021

How To Easily Create Android App Controlled Local Devices With ESP32

Advertisement

For DIY IoT setup, we already have guides with IBM Watson IoT. In those guides, we have mentioned an Android App named “HTTP Shortcuts” for making the thing a bit smarter and professional.

You’ll not need to set up IoT to only locally control a device, for example, a water pump probably does not need control over the internet. In these cases, you only need Wi-Fi. Here is the code snippet for ESP32 to create a relay-based control via Wi-Fi. The relays are attached to pins 19, 21, 22, and 23. I have also have the sketch on GitHub as a repo for you.

I am not providing circuit diagrams because it is just easy to attach a 4 channel relay module with ESP32. You can simply attach LEDs for testing purpose.

Advertisement

---

#include 
 
// Replace with your network credentials
const char* ssid     = "your ssid";
const char* password = "your password";
 
WiFiServer server(80);                                     // Set web server port number to 80
 
String header;                                             // Variable to store the HTTP request
 
const int relay1 = 19;                                     
const int relay2 = 21;
const int relay3 = 22;                                     
const int relay4 = 23;
const int LED = 2;
 
 
void setup() 
{
  Serial.begin(115200);
 
  pinMode(relay1, OUTPUT);                               // Initialize the output variables as outputs
  digitalWrite(relay1, LOW);                             // Set outputs to LOW
 
  pinMode(relay2, OUTPUT);                               // Initialize the output variables as outputs
  digitalWrite(relay2, LOW);                             // Set outputs to LOW
 
  pinMode(relay3, OUTPUT);                               // Initialize the output variables as outputs
  digitalWrite(relay3, LOW);                             // Set outputs to LOW
 
  pinMode(relay4, OUTPUT);                               // Initialize the output variables as outputs
  digitalWrite(relay4, LOW);                             // Set outputs to LOW
 
  pinMode(LED,OUTPUT);
  digitalWrite(LED,LOW);
 
  // Connect to Wi-Fi network with SSID and password
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  // Print local IP address and start web server
  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  server.begin();
}
 
void loop()
{
  WiFiClient client = server.available();   // Listen for incoming clients
 
  if (client) // If a new client connects,
  {                             
    Serial.println("New Client.");          // print a message out in the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) // loop while the client's connected
    {            
      if (client.available()) // if there's bytes to read from the client,
      {             
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        header += c;
        if (c == '\n') // if the byte is a newline character
        {                    
          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) 
          {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println("Connection: close");
            client.println();
            
            // Relay 1 GPIO control
            if (header.indexOf("GET /1") >= 0) 
            {
              if(digitalRead(relay1)== LOW)
              {
                Serial.println("Relay 1 ON");
                digitalWrite(relay1, HIGH);
                client.print("Relay 1 ON");
              }
              else
              {
                Serial.println("Relay 1 OFF");
                digitalWrite(relay1, LOW);
                client.print("Relay 1 OFF");
              }
            }
 
              // Relay 2 GPIO control
 
              else if (header.indexOf("GET /2") >= 0) 
            {
              if(digitalRead(relay2)== LOW)
              {
                Serial.println("Relay 2 ON");
                digitalWrite(relay2, HIGH);
                client.print("Relay 2 ON");
              }
              else
              {
                Serial.println("Relay 2 OFF");
                
                digitalWrite(relay2, LOW);
                client.print("Relay 2 OFF");
              }
            }
 
              // Relay 3 GPIO control
 
             else if (header.indexOf("GET /3") >= 0) 
            {
              if(digitalRead(relay3)== LOW)
              {
                Serial.println("Relay 3 ON");
                digitalWrite(relay3, HIGH);
                client.print("Relay 3 ON");
              }
              else
              {
                Serial.println("Relay 3 OFF");
                digitalWrite(relay3, LOW);
                client.print("Relay 3 OFF");
              }
            }
 
              // Relay 4 GPIO control
 
              else if (header.indexOf("GET /4") >= 0) 
            {
              if(digitalRead(relay4)== LOW)
              {
                Serial.println("Relay 4 ON");
                digitalWrite(relay4, HIGH);
                client.print("Relay 4 ON");
              }
              else
              {
                Serial.println("Relay 4 OFF");
                digitalWrite(relay4, LOW);
                client.print("Relay 4 OFF");
              } 
            }
 
            // LED GPIO control
 
             else if (header.indexOf("GET /9") >= 0) 
            {
              if(digitalRead(LED)== LOW)
              {
                Serial.println("LED ON");
                
                digitalWrite(LED, HIGH);
                client.print("LED ON");
              }
              else
              {
                Serial.println("LED OFF");
                digitalWrite(LED, LOW);
                client.print("LED OFF");
              } 
            }
            client.stop();
            header = "";
          }
        }
      }    
    }
  }
}
How To Easily Create Android App Controlled Local Devices With ESP32

In the HTTP Shortcuts app, from the Basic Request Settings, you’ll choose to GET as the method. The web server URL for turn ON/OFF the Relay will be in this format: `http://youripaddress/1`. You’ll get the IP address on Arduino IDE’s serial monitor, notice these lines in the above code:

...
  // Print local IP address and start web server
  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  server.begin();
}
...

Separately you can control a particular light/fan with your Samsung smartwatch’s BLE (read that guide). These systems will give you enough automation without increasing the burden on your router.

Tagged With https://thecustomizewindows com/2021/09/how-to-easily-create-android-app-controlled-local-devices-with-esp32/

This Article Has Been Shared 247 Times!

Facebook Twitter Pinterest

Abhishek Ghosh

About Abhishek Ghosh

Abhishek Ghosh is a Businessman, Surgeon, Author and Blogger. You can keep touch with him on Twitter - @AbhishekCTRL.

Here’s what we’ve got for you which might like :

Articles Related to How To Easily Create Android App Controlled Local Devices With ESP32

  • Create Own ATTiny 85 Arduino Based Wearables

    Here Is How To Plan And Create Own ATTiny 85 Arduino Based Wearables With SMD Basic Electronic Components At Very Cheap Budget At Home On PCB.

  • China Bicycle Speedometer Review

    Here Is China Bicycle Speedometer Review With Infographics Explaining The Functioning Parts, PDF Manual And How It Really Performs In Real Life.

  • What Are Arduino Relay Modules, What They Do?

    What Are Arduino Relay Modules, What They Do? Relay Modules Isolate Two Circuits. We Can Control 230V Appliances With Arduino Using Relay.

  • Multiplexing vs. Charlieplexing : Basics & Example With Arduino

    Multiplexing, Charlieplexing decreases pin count in a cluster of LEDs.Here is Basic Theory on Multiplexing vs. Charlieplexing & Arduino code.

  • Galaxy Watch LTE Versus Active 2 LTE

    They are not comparable as watches. They can be compared only for the specification part. These two watches are different for their look, not exactly they are too different from the specification point of view. May be, Samsung wanted two watches, one with a masculine sports design and another with a normal watch look. Over […]

Additionally, performing a search on this website can help you. Also, we have YouTube Videos.

Take The Conversation Further ...

We'd love to know your thoughts on this article.
Meet the Author over on Twitter to join the conversation right now!

If you want to Advertise on our Article or want a Sponsored Article, you are invited to Contact us.

Contact Us

Subscribe To Our Free Newsletter

Get new posts by email:

Please Confirm the Subscription When Approval Email Will Arrive in Your Email Inbox as Second Step.

Search this website…

 

Popular Articles

Our Homepage is best place to find popular articles!

Here Are Some Good to Read Articles :

  • Cloud Computing Service Models
  • What is Cloud Computing?
  • Cloud Computing and Social Networks in Mobile Space
  • ARM Processor Architecture
  • What Camera Mode to Choose
  • Indispensable MySQL queries for custom fields in WordPress
  • Windows 7 Speech Recognition Scripting Related Tutorials

Social Networks

  • Pinterest (24.3K Followers)
  • Twitter (5.8k Followers)
  • Facebook (5.7k Followers)
  • LinkedIn (3.7k Followers)
  • YouTube (1.3k Followers)
  • GitHub (Repository)
  • GitHub (Gists)
Looking to publish sponsored article on our website?

Contact us

Recent Posts

  • Advantages of Cloud Server Over Dedicated Server for Hosting WordPress March 26, 2023
  • Get Audiophile-Grade Music on Your Smartphone March 25, 2023
  • Simple Windows Security and Privacy Checklist for 2023 March 24, 2023
  • 7 Best Artificial Intelligence (AI) Software March 24, 2023
  • ESP32 Arduino Water Tank Level Monitoring Using Laser ToF Sensor March 23, 2023

About This Article

Cite this article as: Abhishek Ghosh, "How To Easily Create Android App Controlled Local Devices With ESP32," in The Customize Windows, September 3, 2021, March 27, 2023, https://thecustomizewindows.com/2021/09/how-to-easily-create-android-app-controlled-local-devices-with-esp32/.

Source:The Customize Windows, JiMA.in

PC users can consult Corrine Chorney for Security.

Want to know more about us? Read Notability and Mentions & Our Setup.

Copyright © 2023 - The Customize Windows | dESIGNed by The Customize Windows

Copyright  · Privacy Policy  · Advertising Policy  · Terms of Service  · Refund Policy

We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
Do not sell my personal information.
Cookie SettingsAccept
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT