ESP-01 Home Made Remote MultiPlug

    This page is to Setup and Config ESP-01 Home Made Remote MultiPlug
 

Hardware and Software

Hardware 
    ESP8266 ESP-01, Relay 220V, Home Multi-plug, 5V Adapter.

Software 
    Arduino IDE


Sanki Notes

  • https://sites.google.com/a/imediabank.com/sanki-os-developer/esp-01-home-made-remote-multiplug/esp002.jpg?attredirects=0https://sites.google.com/a/imediabank.com/sanki-os-developer/esp-01-home-made-remote-multiplug/esp003.jpg?attredirects=0
    https://sites.google.com/a/imediabank.com/sanki-os-developer/esp-01-home-made-remote-multiplug/esp004.jpg?attredirects=0
    https://sites.google.com/a/imediabank.com/sanki-os-developer/esp-01-home-made-remote-multiplug/esp005.jpg?attredirects=0

Program Code 

          is ..........

  1. /*
     * 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
     *    http://server_ip/gpio0 will set the GPIO0 high/low,
     *    http://server_ip/gpio2 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
     *  
     */

    #include <ESP8266WiFi.h>
    #include <WiFiClient.h>
    #include <ESP8266WebServer.h>
    #include <ESP8266mDNS.h>

    #include <SoftwareSerial.h>

    const char* ssid = "<Your SSID>";
    const char* password = "<SSID Password>";

    ESP8266WebServer server(1102);

    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<server.args(); i++){
        message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
      }
      server.send(404, "text/plain", message);
      digitalWrite(LED_BUILTIN , 0);
    }

    void handleMessage(int gpio0, int gpio2){
      digitalWrite(LED_BUILTIN , 1);

      
      // Prepare the response
      //String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html";
      String s = "\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\n";
      //s += "<body style=\"background-color:lightgrey;\">";   // Color Only

      s += "<head>";
      s += "<style>";
    // ------------------------------ Button
      s += ".button {";
      //s += "    background-color: #4CAF50; /* Green */";
      s += "    border: none;";
      s += "    color: white;";
      s += "    padding: 15px 32px;";
      s += "    text-align: center;";
      s += "    text-decoration: none;";
      s += "    display: inline-block;";
      s += "    font-size: 16px;";
      s += "    margin: 4px 2px;";
      s += "    cursor: pointer;";
      s += "    -webkit-transition-duration: 0.4s; /* Safari */";
      s += "    transition-duration: 0.4s;";
      s += "}";
      s += "";
      s += ".button1 {";
      s += "     background-color: #4CAF50; font-size: 24px  box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);";
      s += "}";
      s += "";
      s += ".button2 {";
      s += "    background-color: #f44336; border=radius:12px font-size: 24px box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);";
      s += "}";  
      s += ".reseton {";
      s += "    background-color: #4CAF50; border-radius: 12px;";
      s += "}";  
      s += ".resetoff {";
      s += "    background-color: #f44336; border-radius: 12px;";
      s += "}"; 

      
      s += "  #wrap{";
      s += "width:1000px;";
      s += "margin:0 auto;";
      s += "}";
      s += "body {";
      s += "    background-image: url(\"http://www.imediabank.com/_/rsrc/1406774830204/home/sankidevice.png\");";
      s += "    background-repeat: no-repeat;";
      s += "    background-position: center top;";
      s += "    background-color: lightgrey";
      s += "    margin-right: 20px;";
      s += "    margin-left: 20px;"; 
      s += "    margin-top: 180px;";
      s += "}";
      s += "h1 {";
      s += "    background-color: green;";
      s += "}";
      s += "";
      s += "div {";
      s += "    background-color: lightblue;";
      s += "}";
      s += "";
      s += "p {";
      s += "    background-color: yellow;";
      s += "    font-size: 48px;";
      s += "}";
      s += "  p.ridge {border-style: ridge;}";
      s += "</style>";
      s += "</head>";
      
      s += "<body align=center>";

      s += "";
     // s += "  <div class=\"wrap\">";
      /*
      s += "<h1>GPIO2 is now ";
      s += (val)?"high":"low";
      s += "</h1>";
      */
      
      s += "<p>ESP-01 Relay </p>";
        s += "" + sMessage + "";

          if (gpio0 == 0) 
          s += "<p class=\"ridge\">Relay 0 (gpio 0) <a href=\"\\gpio0\"><button  class=\"button button2\">OFF</button></a></p>";
      else
          s += "<p class=\"ridge\">Relay 0 (gpio 0) <a href=\"\\gpio0\"><button  class=\"button button1\">ON</button></a></p>";

      
        if (gpio2 == 0) 
          s += "<p class=\"ridge\">Relay 0 (gpio 2) <a href=\"\\gpio2\"><button  class=\"button button2\">OFF</button></a></p>";
      else
          s += "<p class=\"ridge\">Relay 0 (gpio 2) <a href=\"\\gpio2\"><button  class=\"button button1\">ON</button></a></p>";
      
     s += "<p class=\"ridge\">Reset :  <a href=\"\\reseton\"><button  class=\"button reseton\">All ON</button></a><a href=\"\\resetoff\"><button  class=\"button resetoff\">All OFF</button></a></p>";

      //s += "</div>";

      //s += m + "\n";
      s += "</body></html>\n";
      server.send(200, "text/html", s);
      digitalWrite(LED_BUILTIN , 0);
    }

    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);
    }

    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);
    }

    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: http://www.esp8266.com/viewtopic.php?p=25466#sthash.TxIeuo7o.dpuf


    void setup(void){
      pinMode(LED_BUILTIN, OUTPUT);
      digitalWrite(LED_BUILTIN, 0);
      Serial.begin(115200);
      WiFi.begin(ssid, password);
      Serial.println("");

      // Wait for connection
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
      }

       server.begin();
      Serial.println("HTTP server started");
      
      Serial.println("");
      Serial.print("Connected to ");
      Serial.println(ssid);
      Serial.print("IP address: ");
      Serial.println(WiFi.localIP());

      if (MDNS.begin("esp8266")) {
        Serial.println("MDNS responder started");
      }

      uint8_t MAC_array[6];
      char MAC_char[18];

      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);

      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);



      server.on("/", handleRoot);

      server.on("/reseton", [](){
        //server.send(200, "text/plain", "this works as well");
        handleReset(1);
      });

      server.on("/resetoff", [](){
        //server.send(200, "text/plain", "this works as well");
        handleReset(0);
      });

      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);
        
      });


       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);

     
    }

    void loop(void){
      server.handleClient();
    }
ą
esp001.jpg
(333k)
Sanki Poon,
2016年4月6日 上午2:11
ą
esp002.jpg
(305k)
Sanki Poon,
2016年4月6日 上午2:11
ą
esp003.jpg
(253k)
Sanki Poon,
2016年4月6日 上午2:11
ą
esp004.jpg
(321k)
Sanki Poon,
2016年4月6日 上午2:11
ą
esp005.jpg
(346k)
Sanki Poon,
2016年4月6日 上午2:11