/* PROGRAMMINFO Funktion: ESP32 WEB Server mit mDNS http://khfweb.local Version: 01.03.2021 (x) C++ Arduino IDE V1.8.13 Board: ESP32vn IoT UNO V1.0.4 Einstellungen: https://dl.espressif.com/dl/package_esp32_index.json http://dan.drown.org/stm32duino/package_STM32duino_index.json https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_dev_index.json Librarys - WiFi.h V0.16.1 #include #include #include */ #include #include #include const char* ssid = "xxx"; const char* password = "xxx"; WiFiServer server(80); void setup(void) { Serial.begin(115200); // Verbinde mir WiFi WiFi.begin(ssid, password); Serial.println(""); // Warte auf Verbindung 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()); // Domain-Name khfweb.local if (!MDNS.begin("khfweb")) { Serial.println("Error setting up MDNS responder!"); while(1) { delay(1000); } } Serial.println("mDNS responder started"); // Start TCP (HTTP) server.begin(); Serial.println("TCP server started"); // Start MDNS-SD Dienst MDNS.addService("http", "tcp", 80); } void loop(void) { // Überprüfe Client Verbindung WiFiClient client = server.available(); if (!client) { return; } Serial.println(""); Serial.println("New client"); // Warten, bis Daten vom Client verfügbar sind while(client.connected() && !client.available()){ delay(1); } // Lesen der ersten Zeile der HTTP-Anforderung String req = client.readStringUntil('\r'); // Erste Zeile der HTTP-Anforderung: "GET /path HTTP/1.1" // Abrufen des "/Pfad"-Teils durch Suchen der Leerzeichen int addr_start = req.indexOf(' '); int addr_end = req.indexOf(' ', addr_start + 1); if (addr_start == -1 || addr_end == -1) { Serial.print("Invalid request: "); Serial.println(req); return; } req = req.substring(addr_start + 1, addr_end); Serial.print("Request: "); Serial.println(req); String s; if (req == "/") { IPAddress ip = WiFi.localIP(); String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]); s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n\r\nHello, hier ist der khf WEB-Server mit der IP "; s += ipStr; s += "\r\n\r\n"; Serial.println("Sending 200"); } else { s = "HTTP/1.1 404 Not Found\r\n\r\n"; Serial.println("Keine Verbindung 404"); } client.print(s); client.stop(); Serial.println("Client fertig"); }