location.href (html) causing unwanted refreshes

I have a simple webserver on my arduino that turns on a LED and reads an analogue input. To update the analogue reading displayed on the users web browser I am using a page refresh. However, without getting too bogged down with side issues, I need to set the URL back to index.htm so as two connected users do not repetitively repost their (sometimes opposite) requests to the server when their pages auto-refresh, causing on-off flashing of the LED.

To reset the url to a ‘non-command’ I use the location.href command so that there is not this repeat reposting of their original command when their page refreshes.

However I note that the line : client.println(“location.href=“http://192.168.1.109/index.htm””); causes the page to constantly refresh very frequently. As you can see in the code below, I have the refresh commented out but I still get the too frequent refreshes. By commenting out the location.href line the constant refreshes stop (but I still need the URL reset). I want the pages to refresh every ~5 seconds but not at the current rate which causes unwanted network traffic and annoying flickering of the browser page.

Does anyone know how I can get around it? It would seem that the location.href instruction is not just resetting the URL variable in the browser (so it uses the new URL next time it refreshes) but is telling the browser to repost back to the server using this new URL (which in turn executes location.href again) thereby creating a loop.

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 109 };
byte gateway[] = { 192, 168, 1, 1 };
byte subnet[] = { 255, 255, 255, 0 };
EthernetServer server(80);

boolean LEDStatus;

void setup()
{
  pinMode(2, OUTPUT);
  pinMode(19, OUTPUT);
  LEDStatus = false;
  Serial.begin(9600);
  Serial.println("NIC Reset"); 
  digitalWrite(2, LOW);
  delay(1000);
  digitalWrite(2, HIGH);
  delay(2000);
  
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  server.begin();
  Serial.println("Server Started...");
}

void loop()
{
  EthernetClient client = server.available();
  if (client)
  {
    char InBuffer[32] = {'\0'};
    uint8_t i = 0;
    while (client.connected())
    {
      if (client.available())
      {
        char c = client.read();
        if (i < 30)
        {
          InBuffer[i++] = c;
        }
        if (c == '\n')  //if HTTP request has ended
        {
          Serial.println(InBuffer);
          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          client.println("Connection: close");
          //client.println("<meta http-equiv=\"refresh\" content=\"3; url=http://192.168.1.109/\">");          
          client.println("<script language=\"javascript\">");
          //client.println("Refresh: 5");
          client.println("</script>");
          
          
          client.println();
          client.println("<!DOCTYPE HTML>");         
          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>LED Control</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");
          
          client.println("<font size=\"5\">");
          client.println("<H1>LED Control Page</H1>");          
          
          if (LEDStatus)
          {
            client.print("<p>LED is On</p>");
          }
          else
          {
            client.print("<p>LED is Off</p>");
          }
          
          client.print("Aanalogue Reading: ");
          client.print(analogRead(0));
          client.println("
");
          client.println("
");
          client.println("<input type=button value=ON style=\"font-size: 20px; width: 200px; height: 60px\" onmousedown=location.href='/?1;'>"); 
          client.println("<input type=button value=OFF style=\"font-size: 20px; width: 200px; height: 60px\" onmousedown=location.href='/?0;'>");
          
          client.println("<script language=\"javascript\">");
          //client.println("location.replace (\"\index.htm\");");
          client.println("location.href=\"http://192.168.1.109/index.htm\"");
          client.println("</script>");
          
          client.println("</font>");          
          client.println("</BODY>");
          client.println("</HTML>");
          
          delay(2);
          client.stop();
          
          InBuffer[i] = '\0';         
          if(InBuffer[6] == '1')
          {
            LEDStatus = true;
          }
          else if(InBuffer[6] == '0')
          {
            LEDStatus = false;
          }
          Serial.print("Buffer: ");
          Serial.println(InBuffer);
          digitalWrite(19, LEDStatus);
          if (LEDStatus)
          {
            Serial.println("LED On");
          }
          else
          {
            Serial.println("LED Off");
          }          
        }
      }
    }
    Serial.println("Disconnected");
  }
}

However I note that the line : client.println("location.href=\"http://192.168.1.109/index.htm\""); causes the page to constantly refresh very frequently. As you can see in the code below, I have the refresh commented out but I still get the too frequent refreshes.

The likely culprit for an unwanted refresh is that browsers seem to also request fort a favicon after a request to a url. If your server is setup to supply the web page any time it is contacted by a client, it may send the web page again when the favicon request is made.

Thanks zoomcat.
You are right about the browsers requesting the favicon.

You may recognise the webserver as a cutdown version of one that you posted a while back.

I have fixed (cludged?) it by making the location.href conditional, substituting the following into my code above:

          if(InBuffer[6] == '1' || InBuffer[6] == '0')
          {
            client.println("<script language=\"javascript\">");            
            client.println("location.href=\"http://192.168.1.109/index.htm\"");
            client.println("</script>");
          }

Thereby only implementing the location.href if the last post was a command (i.e. turning the LED on or off with a '“1” or a “0”)

I noticed the favicon issue when I was checking the request for “on” or “off” to control an LED. “on” is contained in “favicon” so the favicon request was turning the LED on. I changed the “on” to “on1” as a workaround in the below code. also sent a “status: 204” back to the client so the page would not refresh.

//zoomkat 12-8-11
//simple button GET with iframe code
//for use with IDE 1.0
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html (or use ')
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port

String readString; 

//////////////////////

void setup(){

  pinMode(5, OUTPUT); //pin selected to control
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  server.begin();

  //enable serial data print 
  Serial.begin(9600); 
  Serial.println("server LED test 1.0"); // so I can keep track of what is loaded
}

void loop(){
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string 
          readString += c; 
          //Serial.print(c);
        } 

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //print to serial monitor for debuging 

          //now output HTML data header
             if(readString.indexOf('?') >=0) { //don't send new page
               client.println("HTTP/1.1 204 Zoomkat");
               client.println();
               client.println();  
             }
             else {
          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          client.println();

          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>Arduino GET test page</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");

          client.println("<H1>Zoomkat's simple Arduino button</H1>");
          
          client.println("<a href=\"/?on1\" target=\"inlineframe\">ON</a>"); 
          client.println("<a href=\"/?off\" target=\"inlineframe\">OFF</a>"); 

          //client.println("<IFRAME name=inlineframe src=\"res://D:/WINDOWS/dnserror.htm\" width=1 height=1\">");
          client.println("<IFRAME name=inlineframe style=\"display:none\" >");          
          client.println("</IFRAME>");

          client.println("</BODY>");
          client.println("</HTML>");
             }

          delay(1);
          //stopping client
          client.stop();

          ///////////////////// control arduino pin
          if(readString.indexOf("on1") >0)//checks for on
          {
            digitalWrite(5, HIGH);    // set pin 4 high
            Serial.println("Led On");
          }
          if(readString.indexOf("off") >0)//checks for off
          {
            digitalWrite(5, LOW);    // set pin 4 low
            Serial.println("Led Off");
          }
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}