• 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

---

Vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <WiFi.h>
// 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:

Vim
1
2
3
4
5
6
7
8
9
...
  // 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/
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

  • Arduino and LED Bar Display : Circuit Diagram, Code

    Here is a Guide Explaining the Basics, Circuit Diagram, Code on Arduino and LED Bar Display. LED Bar Display is Actually Like Multiple LED.

  • How to Control Multiple Relays With Single Arduino ESP32?

    Before How to Control Multiple Relays With Single Arduino ESP32 Testing, You Need to Learn How to Create Multiple MQTT Channels & Fetch Data.

  • WROOM ESP32 Example Codes For IBM Watson IoT Platform

    Here Are Few WROOM ESP32 Example Codes For IBM Watson IoT Platform So That Anyone Can Get Started With Both of Them Without Huge Experience.

  • Detect Smartwatch With ESP32 on IBM Watson IoT Widget

    In our previous guide, we have shown that we can trigger ESP32 (with Arduino IDE) to send message to IBM Watson IoT in Presence of a Particular Samsung Galaxy Smartwatch. That process involves BLE and WiFi. In our one series of articles on Samsung Smartwatch as Proximity Switch, we triggered a local event, such as […]

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

  • Hybrid Multi-Cloud Environments Are Becoming UbiquitousJuly 12, 2023
  • Data Protection on the InternetJuly 12, 2023
  • Basics of BJT TransistorJuly 11, 2023
  • What is Confidential Computing?July 11, 2023
  • How a MOSFET WorksJuly 10, 2023
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