ich hatte die Vermutung, dass er das "12345" vielleicht nicht aus der HTML-Eingabe nimmt sondern irgendwie aus website_upwd klaut.
ich hatte die Vermutung, dass er das "12345" vielleicht nicht aus der HTML-Eingabe nimmt sondern irgendwie aus website_upwd klaut.
nein, eigentlich nicht:
readString ist der String, der von der html-Seite mit den Text-Eingabefeldern kommt, der wird in einen cstring namens strinput umgewandelt:
Dann wird in diesm strinput nach user name und pwd gefischt:Code:readString.toCharArray(strinput, MAXLEN);
Hier mal der komplette, abgespeckte Code:Code:// 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 //... if ( (strlen(strupwd)==strlen(website_upwd) ) && (strcmp(website_upwd, strupwd )==0) ) { authorized = true; //... }
Code://---------------------------------------------------------------------------- // board: ESP8266 NodeMCU 1.0 (ESP12-E module) // Arduino IDE 1.8.5 // ESP8266 core 2.4.0 //---------------------------------------------------------------------------- // Wifi + website data extern const char* ssid =""; // WIFI network name extern const char* password =""; // WIFI network password // define char website_uname[20] ="" ; // website user name log in "MyWebsiteLoginName" char website_upwd[20] =""; // website user pwd log in "MyWebsiteLoginPwd" char* website_title =""; // website caption "MySiteCaption" char* website_url =""; // website url "http:\\mysite.com" //---------------------------------------------------------------------------- // OLED SSD1306 #include <ESP_SSD1306.h> // Modification of Adafruit_SSD1306 for ESP8266 compatibility #include <Adafruit_GFX.h> // Needs a little change in original Adafruit library (See README.txt file) #include <Fonts/FreeSansBold12pt7b.h> // #include <Fonts/FreeSans9pt7b.h> // //#include <Fonts/FreeMono12pt7b.h> // // I2C Wire #include <Wire.h> #define SCL D1 // SCL #define SDA D2 // SDA #define D12 10 // GPIO intern #define OLED_RESET 10 // GPIO10=D12 Pin RESET signal ESP_SSD1306 display(OLED_RESET); //---------------------------------------------------------------------------- // IO pins //---------------------------------------------------------------------------- #define PIN_BTND3 D3 // Btn 0=D3 reset Alarm // #define PIN_IN4 D4 // N/A #define PIN_OUT1 D5 // out #define PIN_OUT2 D7 // out // #define PIN_OUT3 D8 // N/A //---------------------------------------------------------------------------- #include <ESP8266WiFi.h> #include <ESP8266WebServer.h> #define ORANGE 255,102,0 // WiFi Router #define this_iph 200 // <<< local host ip #define http_port 80 IPAddress this_ip(192, 168, 2, this_iph); // <<< Feste lokale IP dieses ESP8266-Servers IPAddress gateway(192, 168, 2, 1); // <<< LAN Gateway IP IPAddress subnet(255, 255, 255, 0); // <<< LAN Subnet Mask WiFiServer wifiserver(http_port); ESP8266WebServer lanserver(8081); bool authorized = false; //---------------------------------------------------------------------------- // 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; } //---------------------------------------------------------------------------- // OLED dashboard //---------------------------------------------------------------------------- void dashboard(int mode) { display.clearDisplay(); if (mode >= 0) { display.setCursor( 0, 0); display.print(this_ip); display.setCursor (0, 16); display.print(gateway); display.display(); } display.setFont(); } //---------------------------------------------------------------------------- // SETUP //---------------------------------------------------------------------------- void setup() { int IORes; //STR_DEGREE[0] = CHR_DEGREE; // ° symbol as ANSI C string int progress = 0; //---------------------------------------- Serial.begin(115200); delay(1000); //---------------------------------------- // i2c: init Wire.pins(SDA, SCL); // SDA, SCL Wire.begin(); delay(1); //---------------------------------------- // OLED display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x64) display.setFont(); display.setTextSize(1); display.setTextColor(WHITE); display.clearDisplay(); display.setCursor( 0, 0); display.print("OLED TEST OK"); display.display(); delay(1); Serial.println("OLED sensor init..."); //---------------------------------------- // Connect to WiFi network Serial.println(); Serial.println(); Serial.println("Connecting to Router: "); Serial.println( WiFi.gatewayIP().toString() ); WiFi.begin(ssid, password); WiFi.config(this_ip, gateway, subnet); // feste IP while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); display.clearDisplay(); display.setCursor( 0, 20); display.print("WiFi connecting..."); display.setCursor( 0, 40); display.print((String)progress + "%"); if (progress >= 98) { progress = 80; Serial.println(); } display.display(); if (progress < 10) progress += 5; else if (progress < 50) progress += 2; else if (progress < 90) progress += 1; } display.clearDisplay(); progress = 100; display.setCursor( 0, 20); display.print("WiFi connecting..."); display.setCursor( 0, 40); display.print((String)progress + "%"); display.display(); delay(300); 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 dashboard(1); Serial.println("setup done \n"); } //---------------------------------------------------------------------------- // LOOP //---------------------------------------------------------------------------- void loop() { static double ftmp; static unsigned long tsec=millis(), tms = millis(); static int8_t LEDmode=0; //--------------------------------------- // Check log-in if (!authorized) { handleNotAuthorized(); delay(100); } if (authorized) { handleWebsite(); delay(10); } lanserver.handleClient(); delay(10); //--------------------------------------- // Read local + Udp data if ( millis() - tms >= 100 ) { // refresh data rate tms = millis(); //--------------------------------------- // display on OLED if ( millis() - tsec >= 4000 ) { tsec=millis(); LEDmode++; if(LEDmode>8) LEDmode=0; } dashboard(LEDmode); delay(1); } } //---------------------------------------------------------------------------- //---------------------------------------------------------------------------- void handleNotAuthorized() { String readString=""; char strinput[MAXLEN], strupwd[TOKLEN], struname[TOKLEN], test[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); } //---------------------------------------------------------------------------- // handle root +clients //---------------------------------------------------------------------------- void handleRoot() { handleClients(); } //---------------------------------------------------------------------------- void handleClients() { double ftmp; 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); } // END OF FILE
Geändert von HaWe (18.08.2018 um 18:32 Uhr)
Manche Bibliotheken fehlen bei mir:
//#include <ESP_SSD1306.h> // Modification of Adafruit_SSD1306 for ESP8266 compatibility
//#include <Adafruit_GFX.h> // Needs a little change in original Adafruit library (See README.txt file)
//#include <Fonts/FreeSansBold12pt7b.h> //
//#include <Fonts/FreeSans9pt7b.h> //
//#include <Fonts/FreeMono12pt7b.h> //
Die habe ich deshalb rausgenommen. Dann kann er mit display nichts anfangen: 'display' was not declared in this scope
ich hatte sie drin gelassen, weil man dann auch strings aufs OLED ausgeben kann, unabh. von Serial.
wenn du kein OLED hast, macht das dann auch nichts.
Adafriut libs gehen über den library manager oder hier: https://github.com/adafruit/Adafruit-GFX-Library
<ESP_SSD1306.h> ist hier: https://github.com/ThingPulse/esp8266-oled-ssd1306
<ESP_SSD1306.h>,<Adafruit_GFX.h>, <Fonts/FreeSansBold12pt7b.h>: Fehler beim Kompilieren für das Board NodeMCU 1.0 (ESP-12E Module)
Aber was mir noch aufgefallen ist: dieses cstringarg ist noch nicht ganz durchdacht.
Da wird doch GET verwendet, um die Daten an den Server zu übertragen. Von daher müsste die Übermittlung der Daten in etwa so aussehen: http://.../xxxxx.htm?name=uschi&password=12345
Ist zwar schon eine Zeit her, als ich damit gearbeitet habe, aber so müsste es sein. Diese Daten werden vom Browser verschickt, so dass man auf diesen Übermittlungsstring nur bedingt Einfluß hat. Man muss darauf achten, dass alle Feldbezeichner (für name und password das &-Zeichen vorangestellt haben. So wird es dann wohl auch sein. Aber wenn nicht und wenn der String dann so aussieht (wie ich oben hingeschrieben habe), kommt es mit dieser cstringarg zu einer Ausnahme, weil dort nur mit dem & operiert, dass der cstringarg übergeben wird:
cstringarg verursacht demnach einen Fehler, wenn etwas nicht gefunden wird.Code:Test mit cstringarg() haystack: index.htm?upwd1=testupwd1&upwd2=testupwd2 &upwd13=testupwd13&upwd224=testupwd224 EndeTststring needle: &upwd1= *p: Exception (28):
Was mir als Bindeglied noch fehlt, ist so ein String, vom Webbrowser, so wie er an den Server übermittelt wird - das wird in readString übertragen. So weit habe ich das nachvollzogen und bis auf die Ausnahme oben ist mir bis jetzt nicht aufgefallen, dass es nicht funktioniert. Ich hätte jetzt den Gedanken, selber das so zu programmieren, dass ich die Kommunikation mit dem Browser ausprobieren kann. Aber das ist dann sicherlich abweichend von dem Code, den Du verwendest. Und wenn es dann bei mir funktionieren würde, nutzt Dir das nicht.
Geändert von Moppi (19.08.2018 um 08:31 Uhr)
danke für den Hinweis!
Ich war tatsächlich immer der Meinung, jeder Variablenname würde immer mit einem '&' beginnen und mit einem '=' enden, direkt anschließend kommt dann der Variablenwert, als arg.
Dann wäre es korrekt, dass bei xxxx nur diese Muster &xxxx= gesucht und gefunden werden, ansonsten eben "nicht gefunden", also Rückgabewert -1, ein Laufzeitfehler dürfte allerdings nicht auftreten.
Stimmt es also, dass auch '?' ein Start-delimiter sein kann? Das einzubauen wäre nicht kompliziert, allerdings beträfe es ja nur die erste Textvariable (uname) und nicht die zweite (upwd), indes nur bei der zweiten tritt der Fehler mit überlangen Eingabestrings ja auf - und der fängt ja korrekt mit '&' an und hört korrekt mit '=' auf.
Einen "Standard-GET-html-string kann ich dir nicht zeigen, denn davon verstehe ich nichts - du müsstest ihn dir selber aus dem Programm per Serial.println mal genau angucken, sowohl in meinem Programm als auch in anderen.
Ich habe hier auch noch einmal einen noch stärker verkürzten Testcode ohne OLED und mit DHCP IP (die wird per Serial am Anfang angezeigt, dann kannst du dich damit per Browser anmelden (z.B. http://192.168.2.111)
- - - Aktualisiert - - -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); }
beide codes lassen sich aber fehlerfrei compilieren bei mir, evtl versuche es unmittelbar ein 2. oder 3. Mal hinterher. Manchmal liegt es an einer temp. Datei, die momentan nicht überschrieben werden konnte.
Falls es doch nicht funzt: wie ist die exakte vollständige Fehlermeldung?
Geändert von HaWe (19.08.2018 um 10:27 Uhr)
ESP8266WebServer.h als alternative Lösung
Beispiel: http://.../xxxx.htm?uname=uschi
Wenn Du als Webseite nur "/" angibst, liefert webserver.arg(F("uname")); ebenfalls "uschi" und webserver.uri() liefert "/".Code:ESP8266WebServer webserver(80); //Webserver-Instanz für Port 80 erstellen String filename = webserver.uri(); //Liefert "xxxx.htm" String par1 = webserver.arg(F("uname")); //Liefert "uschi"
Mit ESP8266WebServer.h lassen sich Parameter sehr einfach abfragen. Du kannst jeden beliebigen Parameter abfragen, die Anzahl Parameter ermitteln etc.
Allerdings verwendest Du WiFiServer, auf Port 80.
Allerdings kannst Du auch eine Instanz von ESP8266WebServer auf Port 80 erstellen und dann so die Argumente auslesen.
Der erste Parameter wird an eine Webseite immer mit ? übergeben und jeder weitere mit &.
Geändert von Moppi (19.08.2018 um 10:44 Uhr)
Lesezeichen