Advertisement
Macrovision

ESP-01 WIFI SWITCH

May 18th, 2022 (edited)
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.12 KB | None | 0 0
  1. //https://youtu.be/asCd8rSppe0//
  2.  
  3. #include <ESP8266WiFi.h>
  4. #include <WiFiClient.h>
  5. #include <ESP8266WebServer.h>
  6. #include <ESP8266mDNS.h>
  7. #include <WiFiManager.h>
  8. #include <DNSServer.h>
  9.  
  10.  
  11. ESP8266WebServer server(81);
  12. DNSServer dnsServer;
  13. int state =0;
  14. const int output1 = 0;
  15. const int btn = 2;
  16. //int btn;
  17. boolean Relay1 = false;
  18. WiFiManager wifiManager;
  19.  
  20.  
  21. //void AP_staticIP(void);
  22. ///void toggle(void);
  23. void client_staticIP(void);
  24.    void handleRoot() {
  25.  
  26.  
  27.   String cmd;    
  28.       cmd += "<!DOCTYPE HTML>\r\n";
  29.       cmd += "<html>\r\n";
  30.       cmd += "<header><title>Home Automation</title><center><h1>\"Home Automation\"</h1></center></header>";
  31.       cmd += "<style> body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }</style>";
  32.       cmd += "<head>";
  33.       cmd += "<meta http-equiv='refresh' content='5'/>";
  34.       cmd += "</head>";
  35.      
  36.       if(Relay1 ==1 ){
  37.         cmd +=("<br/><center>Relay : ON");
  38.       }
  39.  
  40.        else{
  41.         cmd +=("<br/><center>Relay  : OFF");
  42.       }
  43.      
  44.       cmd += "<html>\r\n";
  45.       server.send(200, "text/html", cmd);
  46.       }
  47.  
  48.  
  49.   void handleNotFound(){
  50.   //digitalWrite(led, 1);
  51.   String message = "File Not Found\n\n";
  52.   message += "URI: ";
  53.   message += server.uri();
  54.   message += "\nMethod: ";
  55.  
  56.   message += (server.method() == HTTP_GET)?"GET":"POST";
  57.   message += "\nArguments: ";
  58.   message += server.args();
  59.   message += "\n";
  60.   for (uint8_t i=0; i<server.args(); i++){
  61.     message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  62.   }
  63.   server.send(404, "text/plain", message);
  64.   //digitalWrite(led, 0);
  65. }
  66.  
  67.  
  68. /*
  69. void AP_staticIP(){
  70.     wifiManager.setAPStaticIPConfig(IPAddress(10,1,1,10), IPAddress(10,1,1,1), IPAddress(255,255,255,0));
  71.     wifiManager.autoConnect("Home Automation", "password");
  72.  }
  73.  */
  74.  
  75.  
  76.  
  77.  
  78. void client_staticIP(){
  79.     wifiManager.setSTAStaticIPConfig(IPAddress(192,168,1,175), IPAddress(192,168,1,1), IPAddress(255,255,255,0));
  80.     wifiManager.autoConnect("Home_Automation","password");
  81.  }
  82.  
  83.  
  84.  
  85.  
  86. /*
  87.  
  88.  
  89. void client_staticIP(){
  90.     wifiManager.setSTAStaticIPConfig(IPAddress(10,0,0,150), IPAddress(10,0,0,1), IPAddress(255,255,255,0));
  91.     wifiManager.autoConnect("Home_Automation","password");
  92.  }
  93. */
  94.  
  95. void toogle(){
  96. digitalWrite(output1,(state)? HIGH:LOW);
  97. //digitalWrite(output1,(!output1));
  98. state=!state;
  99. delay(500);
  100. }
  101.  
  102.  
  103.  
  104. void setup(void){
  105.  
  106.    client_staticIP();
  107.   //AP_staticIP();
  108.  
  109.  
  110.  
  111.  pinMode(output1, OUTPUT);
  112.  digitalWrite(output1, LOW);
  113.  pinMode(btn, INPUT);
  114.  //pinMode(output2, OUTPUT);
  115.  //digitalWrite(output2, LOW);
  116.  
  117.  
  118.  
  119.     server.on("/", handleRoot);
  120.  
  121.  
  122.     server.on("/RL1=1", [](){
  123.     server.send(200, "text/plain","↑");
  124.     digitalWrite(output1, HIGH);
  125.     Relay1 = true;
  126.   });
  127.  
  128.   server.on("/RL1=0", [](){
  129.     server.send(200, "text/plain","↓");
  130.     digitalWrite(output1, LOW);
  131.     Relay1 = false;
  132.   });
  133.  
  134.  
  135.  
  136.   server.onNotFound(handleNotFound);
  137.   server.begin();
  138.   Serial.println("HTTP server started");
  139.  
  140. }//end setup
  141.  
  142. void loop(void){
  143.  if ((!digitalRead(btn))) {
  144.  
  145.     toogle();
  146.  }
  147.  
  148. server.handleClient();
  149.  
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement