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.
---
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 = ""; } } } } } } |
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:
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/