Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <WiFiClient.h>
- #include <ESP8266WebServer.h>
- #include <ESP8266mDNS.h>
- const char* ssid = "your_ssid";
- const char* password = "your_password";
- String htmlOutput;
- String onStringColor;
- String offStringColor;
- ESP8266WebServer server(80);
- void handleRoot() {
- if(server.arg(0) == "on") {
- digitalWrite(14, HIGH);
- Serial.println("on");
- offStringColor = "black";
- onStringColor = "white";
- } else {
- digitalWrite(14, LOW);
- Serial.println("off");
- offStringColor = "white";
- onStringColor = "black";
- }
- htmlOutput = "<html>";
- htmlOutput += "<head>";
- htmlOutput += "<style>";
- htmlOutput += "body { font-size: 36px;}";
- htmlOutput += "button { min-width: 200px; min-height: 200px; font-size: 36px; }";
- htmlOutput += "</style>";
- htmlOutput += "</head>";
- htmlOutput += "<body>";
- htmlOutput += "<h1>Hello Switch of the board's led!</h1>";
- htmlOutput += "<a href='/?state=on'><button style='background-color: green; color: " + onStringColor + "'>ON</button></a>";
- htmlOutput += "<br>";
- htmlOutput += "<a href='/?state=off'><button style='background-color: red; color: " + offStringColor + "'>OFF</button></a>";
- htmlOutput += "</body>";
- htmlOutput += "</html>";
- server.send(200, "text/html", htmlOutput);
- }
- void handleNotFound(){
- String message = "File Not Found\n\n";
- message += "URI: ";
- message += server.uri();
- message += "\nMethod: ";
- message += (server.method() == HTTP_GET)?"GET":"POST";
- message += "\nArguments: ";
- message += server.args();
- message += "\n";
- for (uint8_t i=0; i<server.args(); i++){
- message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
- }
- server.send(404, "text/plain", message);
- }
- void setup(void){
- pinMode(14, OUTPUT);
- Serial.begin(115200);
- 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());
- Serial.print("MAC: ");
- Serial.println(WiFi.macAddress());
- server.on("/", handleRoot);
- server.onNotFound(handleNotFound);
- server.begin();
- Serial.println("HTTP server started.");
- }
- void loop(void){
- server.handleClient();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement