hallo,
ich habe ein paar Fragen zu ESP32 wifiserver und webserver.
Zunächst ein einfacher code,
hier wird wifiserver auf port 80 gestartet und dann eine website aufgebaut, um eine LED zuschalten:
einigermaßen klar soweit.Code:/* WiFi Web wifiserver LED Blink A simple web wifiserver that lets you blink an LED via the web. This sketch will print the IP address of your WiFi Shield (once connected) to the Serial monitor. From there, you can open that address in a web browser to turn on and off the LED on pin 5. If the IP address of your shield is yourAddress: http://yourAddress/H turns the LED on http://yourAddress/L turns it off This example is written for a network using WPA encryption. For WEP or WPA, change the Wifi.begin() call accordingly. Circuit: WiFi shield attached LED attached to pin 5 created for arduino 25 Nov 2012 by Tom Igoe ported for sparkfun esp32 31.01.2017 by Jan Hendrik Berlin */ #include <WiFi.h> const char* ssid = "WLAN-"; const char* password = "1865844"; IPAddress this_ip(192, 168, 2, 201); // IPAddress gateway(192, 168, 2, 1); // <<< LAN Gateway IP IPAddress subnet(255, 255, 255, 0); // <<< LAN Subnet Mask WiFiServer wifiserver(80); //---------------------------------------------------------- // GPIOs char LED = LED_BUILTIN; //---------------------------------------------------------- void setup() { Serial.begin(115200); pinMode(LED, OUTPUT); // set the LED pin mode delay(10); // We start by connecting to a WiFi network Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.config(this_ip, gateway, subnet, gateway, gateway); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected."); Serial.println("IP address: "); Serial.println(WiFi.localIP()); wifiserver.begin(); } int value = 0; //---------------------------------------------------------- void loop() { handleWebsite(); delay(1); } //---------------------------------------------------------- void handleWebsite() { WiFiClient client = wifiserver.available(); // listen for incoming clients if (client) { // if you get a client, Serial.println("New Client."); // print a message out the serial port String currentLine = ""; // make a String to hold incoming data from the client while (client.connected()) { // loop while the client's connected 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 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(); // the content of the HTTP response follows the header: client.print("Click <a href=\"/H\">here</a> to turn the LED on pin 5 on.<br>"); client.print("Click <a href=\"/L\">here</a> to turn the LED on pin 5 off.<br>"); // 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 } // Check to see if the client request was "GET /H" or "GET /L": if (currentLine.endsWith("GET /H")) { digitalWrite(LED, HIGH); // GET /H turns the LED on } if (currentLine.endsWith("GET /L")) { digitalWrite(LED, LOW); // GET /L turns the LED off } } } // close the connection: client.stop(); Serial.println("Client Disconnected."); } }
Jetzt der nächste Code:
hier wird eine Authentizifierung per login (admin) und pwd (esp32) verlangt, bevor man auf die nachfolgende website kommt. Die zählt nur in kurzen Abständen einen Counter hoch.
Dazu wird aber die Verbindung über einen webserver(!) auf port 80 hergestellt
- das verstehe ich schon mal nicht.
edit:
ebenfalls seltsam: in der loop() wird webserver.handleClient() aufgerufen, aber es gibt gar keine entsprechende Funktion (nur webserver.handleRoot() ) - verstehe ich ebenfalls nicht.
Aber immerhin, es funktioniert.
Nun aber das eigentliche Problem:Code:/* HTTP Advanced Authentication example plus AdvancedWebserver example, unified Created Mar 16, 2017 by Ahmed El-Sharnoby. This example code is in the public domain. Reworked by dsyleixa 2019 */ #include <WiFi.h> #include <WiFiClient.h> #include <ESPmDNS.h> #include <ArduinoOTA.h> #include <WebServer.h> WebServer webserver(80); char ssid[64] = "WLAN"; char password[64] = "1865844"; char www_username[64] = "admin"; char www_password[64] = "esp32"; IPAddress this_ip(192, 168, 2, 201); // <<< static local IP of this ESP-webserver IPAddress gateway(192, 168, 2, 1); // <<< LAN Gateway IP IPAddress subnet(255, 255, 255, 0); // <<< LAN Subnet Mask // allows you to set the realm of authentication Default:"Login Required" const char* www_realm = "Custom Auth Realm"; // the Content of the HTML response in case of Unautherized Access Default:empty String authFailResponse = "Authentication Failed"; //---------------------------------------------------------- // GPIOs char LED = LED_BUILTIN; //---------------------------------------------------------- void setup() { Serial.begin(115200); pinMode(LED, OUTPUT); // set the LED pin mode WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); WiFi.config(this_ip, gateway, subnet, gateway, gateway); if (WiFi.waitForConnectResult() != WL_CONNECTED) { Serial.println("WiFi Connect Failed! Rebooting..."); delay(1000); ESP.restart(); } ArduinoOTA.begin(); webserver.on("/", []() { if (!webserver.authenticate(www_username, www_password)) //Basic Auth Method with Custom realm and Failure Response //return webserver.requestAuthentication(BASIC_AUTH, www_realm, authFailResponse); //Digest Auth Method with realm="Login Required" and empty Failure Response //return webserver.requestAuthentication(DIGEST_AUTH); //Digest Auth Method with Custom realm and empty Failure Response //return webserver.requestAuthentication(DIGEST_AUTH, www_realm); //Digest Auth Method with Custom realm and Failure Response { return webserver.requestAuthentication(DIGEST_AUTH, www_realm, authFailResponse); } handleRoot(); }); webserver.onNotFound(handleNotFound); webserver.begin(); Serial.println("HTTP webserver started"); Serial.print("Open http://"); Serial.print(WiFi.localIP()); Serial.print(":"); Serial.print(80); Serial.println("/ in your browser to see it working"); } //---------------------------------------------------------- void loop() { ArduinoOTA.handle(); webserver.handleClient(); } //---------------------------------------------------------- void handleRoot() { char temp[255]; String script; int sec = millis() / 1000; int min = sec / 60; int hr = min / 60; // time-cstr sprintf(temp,"%02d:%02d:%02d", hr, min%60, sec%60); script= (String)"" + "<html>" + "<head>" + "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"> \n" + "<meta http-equiv='refresh' content='5'; URL=\"/myweb.sytes.net:80\"/>" + "<title>ESP32 Demo</title>" + "<style>" + "body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }" + "</style>" + "</head>" + "<body>" + "<h1>Hello from ESP32°!</h1>" + "<p>Uptime:" + temp + "</p>" + "</body>" + "<br> \n " + "</html>" ; webserver.send(200, "text/html", script); } //---------------------------------------------------------- void handleNotFound() { String message = "File Not Found\n\n"; message += "URI: "; message += webserver.uri(); message += "\nMethod: "; message += (webserver.method() == HTTP_GET) ? "GET" : "POST"; message += "\nArguments: "; message += webserver.args(); message += "\n"; for (uint8_t i = 0; i < webserver.args(); i++) { message += " " + webserver.argName(i) + ": " + webserver.arg(i) + "\n"; } webserver.send(404, "text/plain", message); }
Wie bekomme ich die LED-Steuerung aus dem 1. Code zusätzlich auf die Website vom 2. Code?
Da laufen mir wifiserver und webserver nun völlig aus dem Ruder, alle meine Versuche sind gescheitert.
Wer bekommt das gemeinsam zum Laufen?







Zitieren

Lesezeichen