Browser synching... 2 browsers to 1 server

Hmmmm well I’m pretty sure there’s a way round this, otherwise the Internet wouldn’t work.

I have a server running with the sketch below, although this phenomenon manifests with other similar sketches. It’s basically zk’s sketch with my network details and a bit of Serial.print-ing.

Browsers open on two laptops… click LED on from LaptopA, which sends the appropriate GET from the URL line and turns led on. Click it off from LaptopB which sends the appropriate GET and it goes off.

BUT… LaptopA leaves its GET in the URL line, so if the browser refreshes for whatever reason, it resends the on-switching GET and we get the LED coming on when it should be off.

//zoomkat 4-1-12
//simple button GET for led on pin 8
//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 ' instead of " 
//address will look like http://192.168.1.102:84 when submited
//for use with W5100 based ethernet shields

//jim 7 dec with my details- ip address etc

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

byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 
  10, 0, 0, 200}; // ip in lan
byte gateway[] = { 
  10, 0, 0, 2 }; // internet access via router
byte subnet[] = { 
  255, 255, 255, 0 }; //subnet mask
EthernetServer server(8085); //server port

String readString; 
int ledPin = 8; 

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

void setup(){
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT); //pin selected to control
  digitalWrite(ledPin, LOW);  

  //start Ethernet
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  server.begin();
  Serial.print("Server established at ");
  Serial.println(Ethernet.localIP());


  //enable serial data print 

  Serial.println("server pin 8 test 1.1"); // so I can keep track of what is loaded
}

void loop(){
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    Serial.println();
    Serial.println("We have 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("Client request ended... here it is... ");
          Serial.println(readString); //print to serial monitor for debuging 
          Serial.println("That was client request");

          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 ala Jim</H1>");

          client.println("<a href=\"/?on8\">ON</a>"); 
          client.println("<a href=\"/?off\">OFF</a>"); 

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

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

          ///////////////////// control arduino pin
          if(readString.indexOf("on8") >0)//checks for on
          {
            digitalWrite(ledPin, HIGH);   
            Serial.println();
            Serial.println("Local message: Led On");
          }
          if(readString.indexOf("off") >0)//checks for off
          {
            digitalWrite(ledPin, LOW);   
            Serial.println();
            Serial.println("Local message: Led Off");
          }
          //clearing string for next read
          readString="";

        }
      }
    }
  }
}

BUT.... LaptopA leaves its GET in the URL line

You could reply with a redirect, when the request is not for the original page, to the original page. Then, the address bar will never show anything but the initially requested page.

PS. This isn't an Arduino question.

BUT.... LaptopA leaves its GET in the URL line, so if the browser refreshes for whatever reason, it resends the on-switching GET and we get the LED coming on when it should be off.

Stop the browser from refreshing. The server acts upon received commands from independent clients and does as told.

PS. This isn't an Arduino question*.

Reason I asked* it here was that I was wondering (read as: hoping) there might be a way of coding my way out of this from the server side. But it seems there isn't:

Stop the browser from refreshing.

  • Actually I just noticed I didn't ask a question- it was more in the way of an observation. So let me ask this as an Arduino question:

Seeing as I have coding control over the way the server sketch serves its pages, can I code my way out of this?

Answer will probably be "no" but at least it's an Arduino question now Paul.

W3 Schools says that GET should only be used to retrieve data. That makes sense: a browser refresh (advertant or not) will then be playing safe and fetching the latest data, as opposed to issuing a spurious command like in my case and restarting a nuclear reactor that someone had turned off from another machine.

Perhaps that's why they called it "GET". POST seems the better (read as: correct?) way to send data for processing.

EDIT: And Part 16 of this tutorial provides a solution. I haven't got that far yet- was looking ahead.

Answer will probably be "no" but at least it's an Arduino question now Paul.

The answer is yes. The response to each GET request that is not the original (no argument) GET can include redirect tags, to cause the browser to go to another page (i.e. back to the original page).