Hallo, ich möchte nochmal um Hilfe für ein altes Problem nachfragen, um meinen Code zu berichtigen.
Die folgende Funktion bietet eine Website zum Anmelden (Benutzername, Passwort). Sie wird aufgerufen, wenn die Website-URL geladen ist und eine globale "autorisierte" Variable immer noch "false" ist.
Wenn der Login korrekt ist, wird die globale Variable "autorisiert" auf "true" gesetzt und die vollständige Website wird angezeigt.
Mit der nodeMCU 1.0 core Version 2.3.0 und 2.4.0 läuft der Code, wenngleich der Browser auch einen Fehler meldet ("Website funktioniert nicht korrekt"), mit den neuesten Cores 2.4.3, 2.4.4 usw. aber funktioniert es gar nicht mehr: Die Website zum Login wird überhaupt nicht mehr angezeigt, das Programm hängt sich aufCode:void handleNotAuthorized() { String readString = ""; char strinput[MAXLEN], strupwd[TOKLEN], struname[TOKLEN] ; WiFiClient client = wifiserver.available(); //--------------------------------------- // debug // authorized=true; strcpy(strinput, ""); strcpy(strupwd, ""); strcpy(struname, ""); while ( client.connected() ) { if (authorized) return; readString.toCharArray(strinput, MAXLEN); // cstringarg( char* haystack, char* vname, char* sarg ) // haystack pattern: &varname=1234abc, delimiters &, \n, \0, SPACE, EOF cstringarg(strinput, "uname", struname); // uname cstringarg(strinput, "upwd", strupwd); // upwd // debug Serial.print("strupwd >>>"); Serial.print(strupwd); Serial.println("<<<"); Serial.print("website_upwd>>>"); Serial.print(website_upwd); Serial.println("<<<"); if ( (strlen(strupwd) == strlen(website_upwd) ) && (strcmp(website_upwd, strupwd ) == 0) ) { authorized = true; //debug //Serial.print("check: authorized="); Serial.println(authorized); readString = ""; return; } if ( client.available() ) { char c = client.read(); //read char by request if (readString.length() < TOKLEN) { //store characters to string readString += c; Serial.println(c); } //if HTTP request has ended if (c == '\n') { client.flush(); //now output html data header String script = ""; script += ("HTTP/1.1 401 Log-In Required"); script += ("Content-Type: text/html \n"); script += ("\n"); // do not forget this one //???? script += ("<!DOCTYPE html> \n"); script += ("<html> \n"); script += ("<head> \n"); // utf-8 für "°" Zeichen script += "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"> \n" ; script += "<title>" ; script += website_title ; script += "</title> \n" ; script += "</head> \n" ; script += "<body> \n" ; script += "<h1><p style=\"color:rgb(255,0,191);\"> " + (String)website_url ; script += (String)": <wbr> <wbr> " + "Not authorized !</p> </h1> \n" ; script += ("<h2><p style=\"color:rgb(255,0,191);\"> log in to proceed: </p> </h2> \n"); script += ("<FORM ACTION='/' method=GET > \n"); script += ("<h2>user name: <INPUT TYPE=text NAME='uname' VALUE='' MAXLENGTH='50'> </h2> \n"); script += ("<h2>password : <INPUT TYPE=PASSWORD NAME='upwd' VALUE='' MAXLENGTH='50'> </h2> \n"); script += ("<h2><INPUT TYPE=SUBMIT></h2> \n"); script += ("</FORM> \n"); script += ("<BR> \n"); script += ("</body> \n"); script += ("</html> \n"); client.print(script); //stopping client client.stop(); delay(1); //clearing string for next read readString = ""; } } delay(1); } }
("Error: Connection interrupted
The connection to the server was reset while the page was loading.")
Zurück-switchen zu core 2.3.0 oder 2.4.0 und Neukompilieren lässt es wieder laufen.
Hier ist die heruntergekürzte MCVE code version ( dhcp mode, no I2C screen).
Ich würde mich sehr freuen, wenn es hier versierte html- und Arduino-ESP8266-API-Kenner gäbe, die den Code berichtigen können;
auch eine Änderung der Anmeldezeilen von der jetzigen html- GET- in eine PUT-Methode zum Verstecken der Eingabezeilen wäre sicher positiv!
Code://---------------------------------------------------------------------------- // board: ESP8266 NodeMCU 1.0 (ESP12-E module) // Arduino IDE 1.8.5 // ESP8266 core 2.4.0 //---------------------------------------------------------------------------- // Wifi + website data const char* ssid = "SSID"; // WIFI network name const char* password = "PSK"; // WIFI network password // define char website_uname[20] = "xx" ; // website user name log in "MyWebsiteLoginName" char website_upwd[20] = "yy"; // website user pwd log in "MyWebsiteLoginPwd" const char* website_title = "#5021"; // website caption "MySiteCaption" const char* website_url = "http://givemeluck"; // website url "http:\\mysite.com" int http_port = 80; //---------------------------------------------------------------------------- #include <ESP8266WiFi.h> #include <ESP8266WebServer.h> WiFiServer wifiserver(http_port); ESP8266WebServer lanserver(8081); bool authorized = false; //---------------------------------------------------------------------------- // handle root +clients //---------------------------------------------------------------------------- void handleRoot() { handleClients(); } //---------------------------------------------------------------------------- void handleClients() { String msgtok; //------------------------------------------ //client-Werte auch bei Url-Aufruf zurückgeben String message = "*** "; // re CLIENT 0 /* message += (String)"&c0t1=" + c0t1.sact + "&c0h1=" + c0h1.sact; message += (String)"&c0t2=" + c0t2.sact + "&c0h2=" + c0h2.sact; message += "&c0out1=" + (String)c0out1 + "&c0out2=" + (String)c0out2 + "&c0out3=" + (String)c0out3 ; */ message += " ###"; //Serial.println(message); lanserver.send(200, "text/plain", message); } //---------------------------------------------------------------------------- // Tools //---------------------------------------------------------------------------- int16_t strstrpos(char * haystack, char * needle) // find 1st occurance of substr in str { char *p = strstr(haystack, needle); if (p) return p - haystack; return -1; // Not found = -1. } //---------------------------------------------------------------------------- const int MAXLEN = 1024; const int TOKLEN = 64; char * cstringarg( char* haystack, char* vname, char* sarg ) { int i = 0, pos = -1; unsigned char ch = 0xff; char kini[3] = "&"; // start of varname: '&': char kequ[3] = "="; // end of varname, start of argument: '=' char needle[TOKLEN] = ""; // complete pattern: &varname=abc1234 //kequ[0] = '='; // customize strcpy(sarg, ""); strcpy(needle, kini); strcat(needle, vname); strcat(needle, kequ); pos = strstrpos(haystack, needle); if (pos == -1) return sarg; pos = pos + strlen(vname) + 2; // start of value = kini+vname+kequ while ( (ch != '&') && (ch != '\0') ) { ch = haystack[pos + i]; if ( (ch == '&') || (ch == ';') || (ch == ' ') || (ch == '\0') || (ch == '\n') || (i + pos >= strlen(haystack)) || (i > TOKLEN - 1) ) { sarg[i] = '\0'; return sarg; } if ( (ch != '&') ) { sarg[i] = ch; i++; } } return sarg; } //---------------------------------------------------------------------------- // SETUP //---------------------------------------------------------------------------- void setup() { //STR_DEGREE[0] = CHR_DEGREE; // ° symbol as ANSI C string //---------------------------------------- Serial.begin(115200); delay(1000); //---------------------------------------- // Connect to WiFi network Serial.println(); Serial.println(); Serial.println("Connecting to Router: "); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.print("WiFi connected: "); Serial.println(WiFi.gatewayIP()); //---------------------------------------- // Start the WiFi server (-> www) wifiserver.begin(); Serial.println("WiFi Server started"); //---------------------------------------- // Start the ESP LAN server (-> ESP client) lanserver.on("/", handleRoot) ; lanserver.on("/client/client0/", handleClients); delay(10); lanserver.on("/client/client1/", handleClients); delay(10); lanserver.on("/client/client2/", handleClients); delay(10); lanserver.on("/client/client3/", handleClients); delay(10); lanserver.begin(); Serial.println("ESP Server started"); // Print the IP address Serial.print("Use this URL to connect: "); Serial.print("http://"); Serial.print(WiFi.localIP()); Serial.print(":"); Serial.print(http_port); Serial.println("/"); Serial.print((String)website_url + ":" + http_port + "/"); delay(1); //---------------------------------------- // setup done Serial.println("setup done \n"); } //---------------------------------------------------------------------------- // LOOP //---------------------------------------------------------------------------- void loop() { //--------------------------------------- // Check log-in if (!authorized) { handleNotAuthorized(); delay(100); } if (authorized) { handleWebsite(); delay(10); } lanserver.handleClient(); delay(10); } //---------------------------------------------------------------------------- //---------------------------------------------------------------------------- void handleNotAuthorized() { String readString = ""; char strinput[MAXLEN], strupwd[TOKLEN], struname[TOKLEN] ; WiFiClient client = wifiserver.available(); //--------------------------------------- // debug // authorized=true; strcpy(strinput, ""); strcpy(strupwd, ""); strcpy(struname, ""); while ( client.connected() ) { if (authorized) return; readString.toCharArray(strinput, MAXLEN); // cstringarg( char* haystack, char* vname, char* sarg ) // haystack pattern: &varname=1234abc, delimiters &, \n, \0, SPACE, EOF cstringarg(strinput, "uname", struname); // uname cstringarg(strinput, "upwd", strupwd); // upwd // debug Serial.print("strupwd >>>"); Serial.print(strupwd); Serial.println("<<<"); Serial.print("website_upwd>>>"); Serial.print(website_upwd); Serial.println("<<<"); if ( (strlen(strupwd) == strlen(website_upwd) ) && (strcmp(website_upwd, strupwd ) == 0) ) { authorized = true; //debug //Serial.print("check: authorized="); Serial.println(authorized); readString = ""; return; } if ( client.available() ) { char c = client.read(); //read char by request if (readString.length() < TOKLEN) { //store characters to string readString += c; Serial.println(c); } //if HTTP request has ended if (c == '\n') { client.flush(); //now output html data header String script = ""; script += ("HTTP/1.1 401 Log-In Required"); script += ("Content-Type: text/html \n"); script += ("\n"); // do not forget this one //???? script += ("<!DOCTYPE html> \n"); script += ("<html> \n"); script += ("<head> \n"); // utf-8 für "°" Zeichen script += "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"> \n" ; script += "<title>" ; script += website_title ; script += "</title> \n" ; script += "</head> \n" ; script += "<body> \n" ; script += "<h1><p style=\"color:rgb(255,0,191);\"> " + (String)website_url ; script += (String)": <wbr> <wbr> " + "Not authorized !</p> </h1> \n" ; script += ("<h2><p style=\"color:rgb(255,0,191);\"> log in to proceed: </p> </h2> \n"); script += ("<FORM ACTION='/' method=GET > \n"); script += ("<h2>user name: <INPUT TYPE=text NAME='uname' VALUE='' MAXLENGTH='50'> </h2> \n"); script += ("<h2>password : <INPUT TYPE=PASSWORD NAME='upwd' VALUE='' MAXLENGTH='50'> </h2> \n"); script += ("<h2><INPUT TYPE=SUBMIT></h2> \n"); script += ("</FORM> \n"); script += ("<BR> \n"); script += ("</body> \n"); script += ("</html> \n"); client.print(script); //stopping client client.stop(); delay(1); //clearing string for next read readString = ""; } } delay(1); } } //---------------------------------------------------------------------------- void handleWebsite() { WiFiClient client = wifiserver.available(); //--------------------------------------- // Check if a client has connected // Read the first line of the request String request = client.readStringUntil('\r'); Serial.println(request); client.flush(); //--------------------------------------- // LogOut if (request.indexOf("/logout") != -1) { authorized = false; return; } delay(1); //--------------------------------------- // Return the response String script = ""; // init website script += ("HTTP/1.1 200 OK \n"); script += ("Content-Type: text/html \n"); script += ("\n"); // do not forget this one script += ("<!DOCTYPE html> \n"); script += ("<html> \n"); // head + title script += ("<head> \n"); // autom. Aktualisierung alle 20 sec. script += "<meta http-equiv=\"refresh\" content=\"20; URL="; script += (String)website_url + ":" + (String)http_port + "\"> \n" ; // utf-8 für "°" Zeichen script += "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"> \n" ; script += ("<title>"); script += (website_title); script += ("</title> \n"); script += ("</head> \n"); // body + caption script += ("<body> \n"); script += ("<h1> <p> "); script += ("<font style=\"color:rgb(255,0,204);\"> HELLO WORLD! "); script += (" <wbr> <wbr> "); script += ("<font style=\"color:rgb(0,205,102);\"> Welcome to " + (String)website_url ); script += ("! </p> </h1> "); // script+= ("! </p> </h1> \n"); delay(1); //--------------------------------------- script += "<h2> <br> \n HEIMSERVER <br> \n </h2>"; //--------------------------------------- // remote buttons Server // <input type="button" value="submit" style="height: 100px; width: 100px; left: 250; top: 250;"> // <button style=\"height:200px;width:200px\"> </button> client.print(script); script = ""; //--------------------------------------- // sensors Server // chart table //--------------------------------------- // text font Courier, color black script += ("<p> <font face=\"courier\"> "); // <<< Courier script += "<h2> "; script += "<p style=\"color:rgb(0,0,0);\" > </p> " ; script += ("<br> \n"); script += "</h2>"; client.print(script); script = ""; script += ("<br> \n"); // log out script += ("<h3>Log Out: "); script += ("<a href=\" /logout\"\"> <button style=\"height:70px;width:140px\" > Log Out </button></a> </h3> "); script += WiFi.localIP().toString() + " " + (String)ssid + " <br>" ; script += "</font> </p> \n"; script += "</body> \n"; script += "</html> \n"; client.print(script); delay(1); }







Zitieren

Lesezeichen