-
Notifications
You must be signed in to change notification settings - Fork 13.3k
website doesn't show up with core 2.4.2, neither 2.4.1 ... - but correctly with 2.3.0 and 2.4.0 though #5021
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
update: |
The sketch is incomplete: The requirement is for a Minimal Complete Verifiable Example MCVE sketch. On a personal note: I don't understand why so many users insist on implementing their own webservers with WiFiServer. In this case, there is even an #include <ESP8266WebServer.h>, but then the webserver is not used. |
I actually don't see how to do it all without both #include <ESP8266WiFi.h> and #include <ESP8266WebServer.h>. WiFiServer wifiserver(http_port);
ESP8266WebServer lanserver(8081);
/* SNIP */
void setup() {
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...");
drawHorizontalBargraph( 0, 30, (int16_t) display.width(), 9, 1, progress);
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...");
drawHorizontalBargraph( 0, 30, (int16_t) display.width(), 9, 1, progress);
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 + "/");
//----------------------------------------
// Start UDP
Serial.println("Starting UdpTime");
UdpTime.begin(localTimePort);
Serial.print("Local Time port: ");
Serial.println(UdpTime.localPort());
Serial.println("waiting for sync");
delay(250);
setSyncProvider(getNtpTime);
delay(100);
handleNotAuthorized() is called in loop(): if (!authorized) {
handleNotAuthorized();
delay(100);
}
if (authorized) {
handleWebsite();
delay(10);
}
lanserver.handleClient(); so here we go.... //----------------------------------------------------------------------------
// 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 ) { // <~~~~~~~ updated version
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
strcpy(sarg,"");
strcpy(needle, kini);
strcat(needle, vname);
strcat(needle, kequ);
pos = strstrpos(haystack, needle);
if(pos==-1) {
needle[0]='?';
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);
}
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Moppi's Fix
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;
if ( client.available() ) {
char c = client.read();
//read char by request
readString = "";
while ( (readString.length() < TOKLEN) && (c != '\n') ) {
readString += c;
c = client.read();
}
readString.toCharArray(strinput, MAXLEN);
// cstringarg( char* haystack, char* vname, char* sarg )
// haystack pattern: &varname=1234abc, delimiters &, \n, \0, SPACE
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("<<<");
Serial.print("readString>>>");Serial.println(readString);
if ( (strlen(strupwd) == strlen(website_upwd)) && (strcmp(website_upwd, strupwd) == 0) )
{
authorized = true;
//debug
//Serial.print("check: authorized="); Serial.println(authorized);
readString = "";
return;
}
//if HTTP request has ended
if (c == '\n') {
client.flush(); // <-- useless here, this empties the output buffer, not the input
//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);
}
}
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
|
@dsyleixa your first sketch did not comply with the "C" in MCVE. The last one does not comply with the "M". |
I reported that issue just to let you know about a bug in those cores/libs. |
@dsyleixa there are currently 3 active developers, and there are 455 open issues and 134 pending PRs at the time of this writing. Expecting a developer to handle your code is not realistic. I am not convinced that the problem you see is really in the core, which is one of the reasons behind the requirement of an MCVE sketch. It is very common that a user thinks it's the core, then while reducing the sketch the issue can no longer be reproduced, which then leads to finding a bug in the user's own code or a 3rd party lib. |
ok, I didn't suppose it to be so complicated for a developer to see through this issue already at the 1st glance. I'll see if I can strip it down and report then, until then I'll try to stay with the 2.4.0 core. (edit, it's already a whole lot smaller now) |
Not complicated, just very very time consuming. A reduction and reproduction of a reported bug can easily take upwards of an hour. Multiply times a whole lot of issues, and there aren't enough hours in a day, let alone our free time each day, to look at all the reported issues. The issue count is now up from 455 in my previous comment to 463 now, and that's after I closed some. I still see the original sketch as incomplete, and the second sketch as huge. As a guideline, the size of the code in your original post is manageable. It's just not complete, as in I can't copy-paste it into an IDE, flash it, and see the bug. |
Hello, i've had the same behaviour. With Server example from https://arduino-esp8266.readthedocs.io/en/2.4.2/esp8266wifi/server-examples.html it is reproducible:
This example procudes: Failed to load resource: net::ERR_CONNECTION_RESET in Chrome If i remove the last client.stop(), the example work as expected. |
@ Scippi : for core 2.4.2 I removed the last client.stop() in void handleNotAuthorized() but still the same error message Fehler: Verbindung unterbrochen still everything works like a charm with 2.4.0. though, just like before. |
I can confirm that client.stop(); seems to break things in 2.4.2. |
again code stripped down a lot - now it's almost the absolute minimum code IMO. |
Tnhkas for the effrots put in the MVCE ! |
a pleasure! 8-) |
Bug reproduced. Working with 2.4.0, not with 2.4.2. simplified MCVE (dhcp mode, no i2c screen) Tnhkas, now we can wrok :)
|
re: "Tnhkas, now we can wrok :)" |
new cstringarg version, accepting also
|
Here's my thoughts:
To (quickl&dirty) fix the above sketch:
Using |
My quick&dirty fixes above are not sufficient. HTTP header ends with 13/10/13/10 (\r\n\r\n)
|
I meanwhile changed handleNotAuthorized() checking also TOKLEN, additionally fixing a different issue, still working fine with 2.4.0, but still not working with 2.4.2. (changes courtesy of "Moppi")
|
You should restart from adapting the WebServer example. FWIW, here's your modified working sketch. Apart from removing external stuff, only
|
thanks for your reply! OTOH, I know the example https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WebServer/examples/HelloServer/HelloServer.ino but that didn't help me to implement a log-in Website accurately though (as shown). I have to admit that I don't have the skills to understand how to change the code and fix all those issues in a reasonable way, so I'm afraid I'll have to stay with my own last version (feat. Moppi's fix) and the 2.4.0 core, at least it's working there as expected. |
This why arduino libraries are here, they do part of the job for you. |
thanks for your advice, I did so! |
Web site doesn't show up if i use VPN with core 2.4.0 till 2.4.2. |
@wongnam please open a new issue with your details. |
2.4.2 still does not work as shown in #5021 (comment) . |
@wongnam |
This bug seems to have meandered around the last 6 months and ended up morphing into a much simpler one asking for a specific example, which is still open. Don't see anything to do here, closing. |
So please kindly reopen this topic as it is still unresolved! |
Howdy, @dsyleixa, A suggestion: There really are multiple things going on in this issue and it's unclear to me. It's got quite a few sketches inline, too. The example issue is still open, sure, and being tracked in another spot, which makes it more likely to get attention as it's focused on one thing. The code in the first entry here is not an MCVE, it needs to be able to be compiled and run as-is and be as short as possible, so that the very few maintainers don't need to scrape together a new application/etc. to see what's going on. You'd be best served to post a new issue with a single MCVE that fails under 2.5.0-rc3 (2.4.2 is quite old now), a brief summary of where you think it got with this issue, and a link to #5021 at the end of the report. |
the code is cut-down here: this is probably your MCVE, bug reproduced by d-a-v |
Cool. I'm sure it would never be found otherwise. I'll reopen this one, but can you please edit the initial post here to point to that comment and the title to be more direct? |
@dsyleixa There's also this #5021 (comment) just after the one you reference. From advices given to you, you opened a new issue, in which you received help from others, and you clearly state you don't have enough skills to achieve your goals, in #5067 (comment). We, maintainers, spend a lot of our spare time to fix bugs and improve performances. I'm sorry to tell you we just cannot write sketches for you. There are support forum for this, and 99% of the time you can find a suitable example somebody kindly shared, by just googling. We will be delighted when you come back with a meaning and working example that you will propose in a pull-request to add in our example repository. |
my request of the "other" topic was about a working code example in html for the nodeMCU core examples, to be used out of the Arduino IDE. Currently the provided examples don't help very much for common Arduino users who are no computer professionals but pupils, artists, craftsmen, and hobbyists. |
Uh oh!
There was an error while loading. Please reload this page.
Basic Infos
Platform
Settings in IDE
Problem Description
the following function provides a website to login (username, password). It is called if the website url is loaded and a global variable "authorized" is still "false".
If login is correct, the global variable "authorized" is set to true, and the full website is displayed,
Admittedly surely not perfect, it actually works fine with core 2.3.0,
but now as I have changed to core 2.4.2 this website does not appear any more,
instead an error html website appears claiming the server is unreachable
("Error: Connection interrupted
The connection to the server was reset while the page was loading.")
Switching back to core 2.3.0 and recompiling, it's fine again.
What do I miss about the new core or the required new coding?
MCVE Sketch
Debug Messages
The text was updated successfully, but these errors were encountered: