user10683: 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?

user43334: 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.

10683: <p>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 <em>needs</em> to be as reliable as possible.</p>

<p>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)</p>

<p>My main loop has some other code in it for reading buttons, values, sensors, etc, but the bulk of it is basically</p>

<pre><code>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' &amp;&amp; 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("&lt;!DOCTYPE HTML&gt;");
                    client.println("&lt;html&gt;");
                    // add a meta refresh tag, so the browser pulls again every 5 seconds:
                    client.println("&lt;meta http-equiv=\"refresh\" content=\"5\"&gt;");
                    // output the value of each analog input pin
                    client.println(getStrDivByTag("MyWebPage", "h3"));
                    client.println("&lt;br /&gt;");


                    client.println("&lt;/html&gt;");
                    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 ...

}
</code></pre>

<p>When the server is working as expected, my serial output gives me something like:</p>

<blockquote>
  <p>new client</p>
  
  <p>GET / HTTP/1.1</p>
  
  <p>Host: 192.168.1.17</p>
  
  <p>User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:73.0) 
  Gecko/20100101 Firefox/73.0</p>
  
  <p>Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,<em>/</em>;q=0.8</p>
</blockquote>

<p>But just before it stops working, I get two back to back requests:</p>

<blockquote>
  <p>GET / HTTP/1.1 Host: 192.168.1.17 User-Agent: Mozilla/5.0 (X11;</p>
  
  <p>Ubuntu; Linux x86_64; rv:73.0) Gecko/20100101 Firefox/73.0 Accept:</p>
  
  <p>text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,<em>/</em>;q=0.8</p>
  
  <p>Accept-Language: en-CA,en-US;q=0.7,en;q=0.3 Accept-Encoding: gzip,</p>
  
  <p>deflate Connection: keep-alive Upgrade-Insecure-Requests: 1</p>
  
  <p>Cache-Control: max-age=0</p>
  
  <p>client disconnected Current Draw: -0.341</p>
  
  <p>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)</p>
  
  <p>Gecko/20100101 Firefox/73.0 Accept: image/webp,<em>/</em> Accept-Language:</p>
  
  <p>en-CA,en-US;q=0.7,en;q=0.3 Accept-Encoding: gzip, deflate Connection:
  keep-alive Cache-Control: max-age=0</p>
  
  <p>client disconnected Current Draw: -0.048</p>
  
  <p>0.0.0.0</p>
  
  <p>Accept-Language: en-CA,en-US;q=0.7,en;q=0.3</p>
  
  <p>Accept-Encoding: gzip, deflate</p>
  
  <p>Connection: keep-alive</p>
  
  <p>Upgrade-Insecure-Requests: 1</p>
  
  <p>Cache-Control: max-age=0</p>
  
  <p>client disconnected</p>
</blockquote>

<p>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.</p>

<p>Is there an issue with my network configuration, the ethernet2 library Im using?</p>
 43334: <p>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"); }</p>

<p>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.</p>
