Advertisement
Guest User

esp webserver

a guest
Dec 27th, 2016
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.08 KB | None | 0 0
  1. /*
  2.  * 115200. Connect GPIO 0 of your ESP8266 to VCC and reset the board
  3.  */
  4.  
  5. #include <ESP8266WiFi.h>
  6. #include <WiFiClient.h>
  7. #include <ESP8266WebServer.h>
  8. #include <ESP8266mDNS.h>
  9.  
  10. MDNSResponder mdns;
  11.  
  12. // Network id and pw
  13. const char* ssid = "MY_ID";
  14. const char* password = "MY_PASSWORD";
  15.  
  16. ESP8266WebServer server(80);
  17.  
  18. String webPage = "";
  19.  
  20. int gpio0_pin = 0;
  21. int gpio2_pin = 2;
  22.  
  23. void setup(void)
  24. {
  25.   webPage += "<h1>ESP8266 Web Server</h1><p>Socket #1 <a href=\"socket1On\"><button>ON</button></a>&nbsp;<a href=\"socket1Off\"><button>OFF</button></a></p>";
  26.   webPage += "<p>Socket #2 <a href=\"socket2On\"><button>ON</button></a>&nbsp;<a href=\"socket2Off\"><button>OFF</button></a></p>";
  27.  
  28.   // preparing GPIOs
  29.   pinMode(gpio0_pin, OUTPUT);
  30.   digitalWrite(gpio0_pin, LOW);
  31.   pinMode(gpio2_pin, OUTPUT);
  32.   digitalWrite(gpio2_pin, LOW);
  33.  
  34.   delay(1000);
  35.   Serial.begin(115200);
  36.   WiFi.begin(ssid, password);
  37.   Serial.println("");
  38.  
  39.   // Wait for connection
  40.   while (WiFi.status() != WL_CONNECTED)
  41.   {
  42.     delay(500);
  43.     Serial.print(".");
  44.   }
  45.   Serial.println("");
  46.   Serial.print("Connected to ");
  47.   Serial.println(ssid);
  48.   Serial.print("IP address: ");
  49.   Serial.println(WiFi.localIP());
  50.  
  51.   if (mdns.begin("esp8266", WiFi.localIP()))
  52.     Serial.println("MDNS responder started");
  53.  
  54.   server.on("/", []()
  55.   {
  56.     server.send(200, "text/html", webPage);
  57.   });
  58.  
  59.   server.on("/socket1On", []()
  60.   {
  61.     server.send(200, "text/html", webPage);
  62.     digitalWrite(gpio0_pin, HIGH);
  63.     delay(1000);
  64.   });
  65.  
  66.   server.on("/socket1Off", []()
  67.   {
  68.     server.send(200, "text/html", webPage);
  69.     digitalWrite(gpio0_pin, LOW);
  70.     delay(1000);
  71.   });
  72.  
  73.   server.on("/socket2On", []()
  74.   {
  75.     server.send(200, "text/html", webPage);
  76.     digitalWrite(gpio2_pin, HIGH);
  77.     delay(1000);
  78.   });
  79.  
  80.   server.on("/socket2Off", []()
  81.   {
  82.     server.send(200, "text/html", webPage);
  83.     digitalWrite(gpio2_pin, LOW);
  84.     delay(1000);
  85.   });
  86.   server.begin();
  87.   Serial.println("HTTP server started");
  88. }
  89.  
  90. void loop(void)
  91. {
  92.   server.handleClient();
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement