Advertisement
Guest User

Switch of the board's led

a guest
Dec 6th, 2018
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.37 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiClient.h>
  3. #include <ESP8266WebServer.h>
  4. #include <ESP8266mDNS.h>
  5.  
  6. const char* ssid = "your_ssid";
  7. const char* password = "your_password";
  8. String htmlOutput;
  9. String onStringColor;
  10. String offStringColor;
  11.  
  12. ESP8266WebServer server(80);
  13.  
  14.  
  15. void handleRoot() {
  16.   if(server.arg(0) == "on") {
  17.     digitalWrite(14, HIGH);
  18.     Serial.println("on");
  19.     offStringColor = "black";  
  20.     onStringColor = "white";
  21.   } else {
  22.     digitalWrite(14, LOW);
  23.     Serial.println("off");
  24.     offStringColor = "white";  
  25.     onStringColor = "black";
  26.   }
  27.   htmlOutput = "<html>";
  28.   htmlOutput += "<head>";
  29.   htmlOutput += "<style>";
  30.   htmlOutput += "body { font-size: 36px;}";
  31.   htmlOutput += "button { min-width: 200px; min-height: 200px; font-size: 36px; }";
  32.   htmlOutput += "</style>";
  33.   htmlOutput += "</head>";
  34.   htmlOutput += "<body>";
  35.   htmlOutput += "<h1>Hello Switch of the board's led!</h1>";
  36.   htmlOutput += "<a href='/?state=on'><button style='background-color: green; color: " + onStringColor + "'>ON</button></a>";
  37.   htmlOutput += "<br>";
  38.   htmlOutput += "<a href='/?state=off'><button style='background-color: red; color: " + offStringColor + "'>OFF</button></a>";
  39.   htmlOutput += "</body>";
  40.   htmlOutput += "</html>";
  41.   server.send(200, "text/html", htmlOutput);
  42. }
  43.  
  44. void handleNotFound(){
  45.   String message = "File Not Found\n\n";
  46.   message += "URI: ";
  47.   message += server.uri();
  48.   message += "\nMethod: ";
  49.   message += (server.method() == HTTP_GET)?"GET":"POST";
  50.   message += "\nArguments: ";
  51.   message += server.args();
  52.   message += "\n";
  53.   for (uint8_t i=0; i<server.args(); i++){
  54.     message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  55.   }
  56.   server.send(404, "text/plain", message);
  57. }
  58.  
  59. void setup(void){
  60.   pinMode(14, OUTPUT);
  61.   Serial.begin(115200);
  62.   WiFi.begin(ssid, password);
  63.   Serial.println("");
  64.  
  65.   // Wait for connection
  66.   while (WiFi.status() != WL_CONNECTED) {
  67.     delay(500);
  68.     Serial.print(".");
  69.   }
  70.   Serial.println("");
  71.   Serial.print("Connected to ");
  72.   Serial.println(ssid);
  73.   Serial.print("IP address: ");
  74.   Serial.println(WiFi.localIP());
  75.   Serial.print("MAC: ");
  76.   Serial.println(WiFi.macAddress());
  77.   server.on("/", handleRoot);
  78.   server.onNotFound(handleNotFound);
  79.   server.begin();
  80.   Serial.println("HTTP server started.");
  81. }
  82.  
  83. void loop(void){
  84.   server.handleClient();
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement