Introduction: ESP8266 ESP-01 Remote Multi Socket
Hardware :
1. (1) ESP8266 - ESP01
2. (1) Relay 220V (5V Control)
3. (1) Multi Socket
How to make :
See Above Photo
ESP8266 Programming :
- upload the following code to ESP-01
- Modify SSID and Password
- Modify Home Route Port Following to control it if you are outside.
More Information : iMediaBank - http://www.imediabank.com
<p>/*<br> * SANKI 2016 03 31 * * This sketch demonstrates how to set up a simple HTTP-like server. * The server will set a GPIO pin depending on the request * <a href="http://server_ip/gpio0" rel="nofollow"> http://server_ip/gpio0 </a> will set the GPIO0 high/low, * <a href="http://server_ip/gpio2" rel="nofollow"> http://server_ip/gpio0 </a> will set the GPIO2 high/low * server_ip is the IP address of the ESP8266 module, will be * printed to Serial when the module is connected. * * Before RUN : * Change SSID and Password * Change port number * */</p><p>#include #include #include #include </p><p>#include </p><p>const char* ssid = ""; const char* password = "";</p><p>ESP8266WebServer server(1102);</p><p>const int led = 0; String sMessage = ""; void handleNotFound(){ digitalWrite(LED_BUILTIN , 1); String message = "File Not Found\n\n"; message += "URI: "; message += server.uri(); message += "\nMethod: "; message += (server.method() == HTTP_GET)?"GET":"POST"; message += "\nArguments: "; message += server.args(); message += "\n"; for (uint8_t i=0; i</p><p>void handleMessage(int gpio0, int gpio2){ digitalWrite(LED_BUILTIN , 1);</p><p> // Prepare the response //String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html"; String s = "\r\n\r\n\r\n \r\n"; //s += " "; // Color Only</p><p> s += " "; s += " </p><p>"; s += "</p><p>"; s += " ";</p> s += ""; // s += " <div>"; /* s += "GPIO2 is now "; s += (val)?"high":"low"; s += ""; */ s += "<p>ESP-01 Relay </p>"; s += "" + sMessage + ""; if (gpio0 == 0) s += "<p>Relay 0 (gpio 0) OFF</p>"; else s += "<p>Relay 0 (gpio 0) ON</p>"; if (gpio2 == 0) s += "<p>Relay 0 (gpio 2) OFF</p>"; else s += "<p>Relay 0 (gpio 2) ON</p>"; s += "<p>Reset : All ONAll OFF</p>"; //s += "</div> ";<p> //s += m + "\n"; s += "</p><p>\n"; server.send(200, "text/html", s); digitalWrite(LED_BUILTIN , 0); }</p><p>void handleRoot() { digitalWrite(LED_BUILTIN , 1); //server.send(200, "text/plain", "hello from esp8266! ---- \nTest Port /gpio"); handleMessage(digitalRead(0), digitalRead(2)); digitalWrite(LED_BUILTIN , 0); }</p><p>void handleReset(int onoff) { digitalWrite(LED_BUILTIN , 1); //server.send(200, "text/plain", "hello from esp8266! ---- \nTest Port /gpio"); if (onoff == 0) { digitalWrite(0, 0); digitalWrite(2, 0); handleMessage(0 ,0); } if (onoff == 1) { digitalWrite(0, 1); digitalWrite(2, 1); handleMessage(1, 1); } digitalWrite(LED_BUILTIN , 0); }</p><p>String ipToString(IPAddress ip){ String s=""; for (int i=0; i<4; i++) s += i ? "." + String(ip[i]) : String(ip[i]); return s; } //- See more at: <a href="http://www.esp8266.com/viewtopic.php?p=25466#sthash.TxIeuo7o.dpuf" rel="nofollow"> http://server_ip/gpio0 ></p><p>void setup(void){ pinMode(LED_BUILTIN, OUTPUT); digitalWrite(LED_BUILTIN, 0); Serial.begin(115200); WiFi.begin(ssid, password); Serial.println("");</p><p> // Wait for connection while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); }</p><p> server.begin(); Serial.println("HTTP server started"); Serial.println(""); Serial.print("Connected to "); Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP());</p><p> if (MDNS.begin("esp8266")) { Serial.println("MDNS responder started"); }</p><p> uint8_t MAC_array[6]; char MAC_char[18];</p><p> WiFi.macAddress(MAC_array); sprintf(MAC_char, "%02x:%02x:%02x:%02x:%02x:%02x", MAC_array[0],MAC_array[1],MAC_array[2],MAC_array[3],MAC_array[4],MAC_array[5]); Serial.print("MAC Address : "); Serial.println(MAC_char);</p><p> sMessage += "[SSID : " + String(ssid) + "] [Server IP : " + ipToString(WiFi.localIP()) + "] [MAC : " + MAC_char + "]"; // prepare GPIO2 pinMode(2, OUTPUT); digitalWrite(2, 0); pinMode(0, OUTPUT); digitalWrite(0, 0);</p><p> server.on("/", handleRoot);</p><p> server.on("/reseton", [](){ //server.send(200, "text/plain", "this works as well"); handleReset(1); });</p><p> server.on("/resetoff", [](){ //server.send(200, "text/plain", "this works as well"); handleReset(0); });</p><p> server.on("/gpio2", [](){ String m = "GPIO 2 : "; int val = digitalRead(2); if (val == 0) { val = 1; m += "ON"; } else { val = 0; m += "OFF"; } digitalWrite(2, val); //server.send(200, "text/plain", m); handleMessage(digitalRead(0), val); });</p><p> server.on("/gpio0", [](){ String m = "GPIO 0 : "; int val = digitalRead(0); if (val == 0) { val = 1; digitalWrite(0, 1); m += "ON"; } else { val = 0; digitalWrite(0, 0); m += "OFF"; } //digitalWrite(led, val); //server.send(200, "text/plain", m); handleMessage(val, digitalRead(2)); }); /* server.on("/gpio2/0", [](){ server.send(200, "text/plain", "Turn OFF GPIO 2"); digitalWrite(2, 0); }); */ server.onNotFound(handleNotFound);</p><p> }</p><p>void loop(void){ server.handleClient(); }</p>