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?
-
use the Ethernet libraryJuraj– Juraj ♦2020年02月26日 08:19:29 +00:00Commented Feb 26, 2020 at 8:19
-
1Make sure you explicitly disable the SD Card slot if you don't use it, by setting pin 4 to high in your setup (at least that's the pin my Ethernet2 shield uses). I almost tore out my hair finding the reason why my (autonomous) sketch would sometimes run for 10 minutes, sometimes an hour, but always stopped responding after some time. It seems the SD Card SPI interface sometimes interferes with Ethernet SPI traffic unless you explicitly disable it.StarCat– StarCat2020年02月26日 15:54:48 +00:00Commented Feb 26, 2020 at 15:54
1 Answer 1
There are many Ethernet sketch examples that are not reliable at all. They basically handle the network setup in setup(), and the TCP connections in loop(). This is wrong. It just means that if there is anything wrong in your network connection, it won't restart correctly. You should check Ethernet.localIP(); and restart your sketch if it doesn't fit. This routine should do the trick : void reset() { asm volatile ("jmp 0"); }
Also, you should be ready for requests for a favicon.ico from usual browsers (that's the small icon that comes in your tabs and favorites) and reply to it (you can use a 404 reply if you don't want to send a file). Providing a robots.txt is also good practice.
-
I dont want to reset the entire sketch as it will bring down some relays transmitting power that the sketch is meant to keep open, but Id imagine it should be possible to just redo the network setup process every time the IP changes, right?BruceJohnJennerLawso– BruceJohnJennerLawso2020年02月26日 08:37:54 +00:00Commented Feb 26, 2020 at 8:37
-
Is there any way of smoothly restarting the Ethernet instance without hard resetting the sketch?BruceJohnJennerLawso– BruceJohnJennerLawso2020年02月29日 21:57:16 +00:00Commented Feb 29, 2020 at 21:57
-
1Yes, you have to move the Ethernet start routine from setup, into loop, but in a 'if' statement that checks whether the Ethernet is active or not. I remember having seen a few sketches that did exactly that on github, but I couldn't find them back for referencing them in the answer (maybe they used the old Ethernet ENC28J60 chip). Sorry about that.Chris– Chris2020年03月04日 07:42:34 +00:00Commented Mar 4, 2020 at 7:42
Explore related questions
See similar questions with these tags.