Im working on a sketch that needs to display some internal variable values and analogue reads in a simple webpage. The only catch is that it needs to be as reliable as possible.
Im getting some odd behaviour across multiple shields where the sketch will work briefly (sometimes as long as 5-6 minutes) before cutting out and setting the IP to something weird (commonly 0.0.0.0 or something similar)
My main loop has some other code in it for reading buttons, values, sensors, etc, but the bulk of it is basically
void loop()
{
... do some stuff ...
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
// add a meta refresh tag, so the browser pulls again every 5 seconds:
client.println("<meta http-equiv=\"refresh\" content=\"5\">");
// output the value of each analog input pin
client.println(getStrDivByTag("MyWebPage", "h3"));
client.println("<br />");
client.println("</html>");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
} else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disconnected");
}
... do some more stuff ...
}
When the server is working as expected, my serial output gives me something like:
new client
GET / HTTP/1.1
Host: 192.168.1.17
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:73.0) Gecko/20100101 Firefox/73.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8
But just before it stops working, I get two back to back requests:
GET / HTTP/1.1 Host: 192.168.1.17 User-Agent: Mozilla/5.0 (X11;
Ubuntu; Linux x86_64; rv:73.0) Gecko/20100101 Firefox/73.0 Accept:
text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8
Accept-Language: en-CA,en-US;q=0.7,en;q=0.3 Accept-Encoding: gzip,
deflate Connection: keep-alive Upgrade-Insecure-Requests: 1
Cache-Control: max-age=0
client disconnected Current Draw: -0.341
192.168.1.17 new client GET /favicon.ico HTTP/1.1 Host: 192.168.1.17 User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:73.0)
Gecko/20100101 Firefox/73.0 Accept: image/webp,/ Accept-Language:
en-CA,en-US;q=0.7,en;q=0.3 Accept-Encoding: gzip, deflate Connection: keep-alive Cache-Control: max-age=0
client disconnected Current Draw: -0.048
0.0.0.0
Accept-Language: en-CA,en-US;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive
Upgrade-Insecure-Requests: 1
Cache-Control: max-age=0
client disconnected
What is that request, something about favicon.ico? After these back to back requests, the IP resets to 0.0.0.0, sometimes jumping to something odd like 0.64.1.0, 0.1.0.64, etc.
Is there an issue with my network configuration, the ethernet2 library Im using?