Advertisement
metalx1000

ESP8266 Wemos Relay Shield Module Web GUI Arduino IDE Code

Feb 19th, 2020
720
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.01 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiClient.h>
  3. #include <ESP8266WebServer.h>
  4. #include <ESP8266mDNS.h>
  5.  
  6. #ifndef STASSID
  7. #define STASSID "SID"
  8. #define STAPSK  "PASS-PHRASE"
  9. #endif
  10.  
  11. const char* ssid = STASSID;
  12. const char* password = STAPSK;
  13.  
  14. ESP8266WebServer server(80);
  15.  
  16. const int led = D1;
  17.  
  18. void handleRoot() {
  19.   digitalWrite(led, 1);
  20.     server.send(200, "text/html", "<a href='/off'><button style='background-color:#44c767;border-radius:26px;border:1px solid #18ab29;display:inline-block;cursor:pointer;color:#ffffff;font-family:Arial;font-size:280px;padding:32px 76px;text-decoration:none;text-shadow:0px 2px 0px #2f6627;'>OFF</button></a>");
  21.  
  22. }
  23.  
  24. void handleNotFound() {
  25.   digitalWrite(led, 1);
  26.   String message = "File Not Found\n\n";
  27.   message += "URI: ";
  28.   message += server.uri();
  29.   message += "\nMethod: ";
  30.   message += (server.method() == HTTP_GET) ? "GET" : "POST";
  31.   message += "\nArguments: ";
  32.   message += server.args();
  33.   message += "\n";
  34.   for (uint8_t i = 0; i < server.args(); i++) {
  35.     message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  36.   }
  37.   server.send(404, "text/plain", message);
  38.   digitalWrite(led, 0);
  39. }
  40.  
  41. void setup(void) {
  42.   pinMode(led, OUTPUT);
  43.   digitalWrite(led, 0);
  44.   Serial.begin(115200);
  45.   WiFi.mode(WIFI_STA);
  46.   WiFi.begin(ssid, password);
  47.   Serial.println("");
  48.  
  49.   // Wait for connection
  50.   while (WiFi.status() != WL_CONNECTED) {
  51.     delay(500);
  52.     Serial.print(".");
  53.   }
  54.   Serial.println("");
  55.   Serial.print("Connected to ");
  56.   Serial.println(ssid);
  57.   Serial.print("IP address: ");
  58.   Serial.println(WiFi.localIP());
  59.  
  60.   if (MDNS.begin("esp8266")) {
  61.     Serial.println("MDNS responder started");
  62.   }
  63.  
  64.   server.on("/", handleRoot);
  65.  
  66.   server.on("/off", []() {
  67.     digitalWrite(led, 0);
  68.     server.send(200, "text/html", "<a href='/'><button style='background-color:#44c767;border-radius:26px;border:1px solid #18ab29;display:inline-block;cursor:pointer;color:#ffffff;font-family:Arial;font-size:280px;padding:32px 76px;text-decoration:none;text-shadow:0px 2px 0px #2f6627;'>ON</button></a>");
  69.   });
  70.  
  71.   server.on("/gif", []() {
  72.     static const uint8_t gif[] PROGMEM = {
  73.       0x47, 0x49, 0x46, 0x38, 0x37, 0x61, 0x10, 0x00, 0x10, 0x00, 0x80, 0x01,
  74.       0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x2c, 0x00, 0x00, 0x00, 0x00,
  75.       0x10, 0x00, 0x10, 0x00, 0x00, 0x02, 0x19, 0x8c, 0x8f, 0xa9, 0xcb, 0x9d,
  76.       0x00, 0x5f, 0x74, 0xb4, 0x56, 0xb0, 0xb0, 0xd2, 0xf2, 0x35, 0x1e, 0x4c,
  77.       0x0c, 0x24, 0x5a, 0xe6, 0x89, 0xa6, 0x4d, 0x01, 0x00, 0x3b
  78.     };
  79.     char gif_colored[sizeof(gif)];
  80.     memcpy_P(gif_colored, gif, sizeof(gif));
  81.     // Set the background to a random set of colors
  82.     gif_colored[16] = millis() % 256;
  83.     gif_colored[17] = millis() % 256;
  84.     gif_colored[18] = millis() % 256;
  85.     server.send(200, "image/gif", gif_colored, sizeof(gif_colored));
  86.   });
  87.  
  88.   server.onNotFound(handleNotFound);
  89.  
  90.   server.begin();
  91.   Serial.println("HTTP server started");
  92. }
  93.  
  94. void loop(void) {
  95.   server.handleClient();
  96.   MDNS.update();
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement