Title
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
#include <ESP8266WiFi.h> #include <WiFiClient.h> #include <ESP8266WebServer.h> #include <ESP8266mDNS.h> MDNSResponder mdns; ESP8266WebServer server(80); // Replace with your network credentials const char* ssid = ""; const char* password = ""; String webPage = ""; int test_pin = 16; void setup(void){ webPage += "<!DOCTYPE HTML> <html> <head> <title>G1Tech: ESP8266 Web Server</title>"; webPage += "<style type=\"text/css\">"; webPage += ".label {display: inline; padding: .2em .6em .3em; font-size: 75%; font-weight: 700; line-height: 1; color: #fff; text-align: center; white-space: nowrap; vertical-align: baseline; border-radius: .25em;}"; webPage += ".label-default {background-color: #777;}"; webPage += ".btn {background-color: #4CAF50; border: none; color: white; padding: 16px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; -webkit-transition-duration: 0.4s; transition-duration: 0.4s; cursor: pointer;}"; webPage += ".btn-on {background-color: white; color: black; border: 2px solid #4CAF50;}"; webPage += ".btn-on:hover {background-color: #4CAF50; color: white;}"; webPage += ".btn-off {background-color: white; color: black; border: 2px solid #f44336;}"; webPage += ".btn-off:hover {background-color: #f44336; color: white;}"; webPage += "</style> </head> <body>"; webPage += "<h1>G1Tech: ESP8266 Web Server</h1><h1><p class=\"label label-default\">LED #16 </p><p><a href=\"LEDOn\"><button class=\"btn btn-on\">ON</button></a> <a href=\"LEDOff\"><button class=\"btn btn-off\" >OFF</button></a></p></h1></body></html>"; pinMode(test_pin, OUTPUT); digitalWrite(test_pin, LOW); delay(1000); Serial.begin(9600); WiFi.begin(ssid, password); Serial.println(""); // Wait for connection while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.print("Connected to "); Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP()); if (mdns.begin("esp8266", WiFi.localIP())) { Serial.println("MDNS responder started"); } server.on("/", [](){ server.send(200, "text/html", webPage); }); server.on("/LEDOn", [](){ server.send(200, "text/html", webPage); digitalWrite(test_pin, LOW); Serial.println(digitalRead(test_pin)); delay(1000); }); server.on("/LEDOff", [](){ server.send(200, "text/html", webPage); digitalWrite(test_pin, HIGH); Serial.println(digitalRead(test_pin)); delay(1000); }); server.begin(); Serial.println("HTTP server started"); } void loop(void){ server.handleClient(); } |
[collapse]