// lade Wi-Fi Bibliothek #include // Netzwerkzugangsdaten const char* ssid = "Euer Netzwerkname"; const char* password = "Euer Passwort"; // Webserver an Port 80 anlegen WiFiServer server ( 80 ); // Variable um HTTP Antwort zu speichern String header; // Hilfsvariablen für den Output Status String output_red_State = "on"; String output_yellow_State = "on"; String output_green_State = "on"; // Ausgänge den GPIO Pins zuordnen const int output_red = 13; const int output_yellow = 12; const int output_green = 14; // aktuelle Zeit unsigned long currentTime = millis (); // letzte Zeitmarke unsigned long previousTime = 0; // Timeout in Millisekunden (example: 2000ms = 2s) const long timeoutTime = 2000; void setup () { Serial.begin ( 115200 ); // Initialisiere Outputs pinMode ( output_red, OUTPUT ); pinMode ( output_yellow, OUTPUT ); pinMode ( output_green, OUTPUT ); // Setze Outputs auf LOW digitalWrite ( output_red, LOW ); digitalWrite ( output_yellow, LOW ); digitalWrite ( output_green, LOW ); // Verbindung mit dem Netzwerk Serial.print ( "Verbinde mit " ); Serial.println ( ssid ); WiFi.begin ( ssid, password ); while ( WiFi.status () != WL_CONNECTED ) { delay ( 500 ); Serial.print ( "." ); } // gebe IP-Adresse aus und verbinde Webserver Serial.println ( "" ); Serial.println ( "WiFi verbunden." ); Serial.println ( "IP Addresse: " ); Serial.println ( WiFi.localIP ()); server.begin (); } void loop () { WiFiClient client = server.available (); // Listen for incoming clients if ( client ) { // If a new client connects, Serial.println ( "New Client." ); // print a message out in the serial port String currentLine = ""; // make a String to hold incoming data from the client currentTime = millis (); previousTime = currentTime; while ( client.connected () && currentTime - previousTime <= timeoutTime) { // loop while the client's connected currentTime = millis (); if ( client.available ()) { // if there's bytes to read from the client, char c = client.read (); // read a byte, then Serial.write ( c ); // print it out the serial monitor header += c; if ( c == '\n' ) { // if the byte is a newline character // if the current line is blank, you got two newline characters in a row. // that's the end of the client HTTP request, so send a response: if (currentLine.length() == 0) { // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK) // and a content-type so the client knows what's coming, then a blank line: client.println("HTTP/1.1 200 OK"); client.println("Content-type:text/html"); client.println("Connection: close"); client.println(); // turns the GPIOs on and off if ( header.indexOf ( "GET /5/on" ) >= 0 ) { Serial.println ( "GPIO 5 on" ); output_red_State = "on"; digitalWrite ( output_red, LOW ); } else if ( header.indexOf ( "GET /5/off" ) >= 0 ) { Serial.println ( "GPIO 5 off" ); output_red_State = "off"; digitalWrite ( output_red, HIGH ); } else if ( header.indexOf ("GET /4/on" ) >= 0 ) { Serial.println ( "GPIO 4 on" ); output_yellow_State = "on"; digitalWrite ( output_yellow, LOW ); } else if ( header.indexOf ( "GET /4/off" ) >= 0 ) { Serial.println ( "GPIO 4 off" ); output_yellow_State = "off"; digitalWrite ( output_yellow, HIGH ); } else if ( header.indexOf ("GET /3/on" ) >= 0 ) { Serial.println ( "GPIO 3 on" ); output_green_State = "on"; digitalWrite ( output_green, LOW ); } else if ( header.indexOf ( "GET /3/off" ) >= 0 ) { Serial.println ( "GPIO 3 off" ); output_green_State = "off"; digitalWrite ( output_green, HIGH ); } // HTML Seite anzeigen client.println(""); client.println(""); client.println(""); // CSS to style the on/off buttons // Feel free to change the background-color and font-size attributes to fit your preferences client.println(""); // Web Page Kopf client.println("

ESP8266 Web Server

"); // rote Ampel anzeigen if (output_red_State=="off") { client.println("

"); } else { client.println("

"); } // gelbe Ampel anzeigen if (output_yellow_State=="off") { client.println("

"); } else { client.println("

"); } // grüne Ampel anzeigen if (output_green_State=="off") { client.println("

"); } else { client.println("

"); } client.println(""); // The HTTP response ends with another blank line client.println(); // Break out of the while loop break; } else { // if you got a newline, then clear currentLine currentLine = ""; } } else if (c != '\r') { // if you got anything else but a carriage return character, currentLine += c; // add it to the end of the currentLine } } } // Clear the header variable header = ""; // Close the connection client.stop(); Serial.println("Client disconnected."); Serial.println(""); } }