Real time web feedback of sensor

I need some help. I have the arduino and ethernet shield. I upload a sketch to make the arduino a web server and it serves a small page with some buttons (on, off). I can go to the ip address, and turn the led hooked to the arduino on and off using the buttons. Perfect so far. But… How can I get realtime feedback? For example, if I turn the led on using the button, then go to a different computer and browse there and turn the led off using the button, the first computer thinks the led is still on. This is a simplified case because I really want to monitor a reed switch’s state in realtime. Here is my code… Any ideas? Thanks! I have been looking for days on how to do this with no luck. So in other words, I’m looking for a way to even serve up a web page real time that displays some text saying the state of digital pin 4 for example.

void loop(){
checkForClient();
}

void checkForClient(){
Client client = server.available();
if (client) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
boolean sentHeader = false;

while (client.connected()) {
if (client.available()) {

if(!sentHeader){

// send a standard http response header
client.println(“HTTP/1.1 200 OK”);
client.println(“Content-Type: text/html”);
client.println();
client.println("");
client.println("\t");
client.println("\t\t
“);
client.println(”\t\tLED Switch");
client.println("\t\t<form METHOD=get action="");
client.println(baseurl);
client.println("\t\t">");
client.println("\t\t");
client.println("\t\t<input type=submit value=“off”>");

client.println("\t\t<form METHOD=get action="");
client.println(baseurl);
client.println("\t\t">");
client.println("\t\t");
client.println("\t\t<input type=submit value=“on”>");

sentHeader = true;
}

char c = client.read();
Serial.print(c);
if(reading && c == ’ ') reading = false;
if(c == ‘?’) reading = true; //found the ?, begin reading the info
if(reading){
Serial.print(c);
if(c == ‘0’) {
digitalWrite(7, LOW);
break;
}
if(c == ‘1’) {
digitalWrite(7, HIGH);
break;
}
}
if (c == ‘\n’ && currentLineIsBlank) break;
if (c == ‘\n’) {
currentLineIsBlank = true;
}else if (c != ‘\r’) {
currentLineIsBlank = false;
}
}
}
// check if LED is on or off
if (digitalRead(7) == LOW)
{
client.println("\t\tLED is OFF");
}
if (digitalRead(7) == HIGH)
{
client.println("\t\tLED is ON");
}
client.println("\t");
client.println("");
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection:
}
}

If your web page includes the necessary Meta tag for refresh, the client will periodically poll the server for new information. There is no way for the Arduino to push new information.

Ok, that makes sense. So if I wanted a web page to reflect the state of a reed switch, I would have the arduino server serve up a page containing the code that refreshes in order to retrieve any new data the server may contain? Is there another way without having the page constantly refresh? If I want realtime info of the reed switch, it seems it would be refreshing constantly. Thanks for the direction so far.

John

A w5100 chip ethernet shield can run both server and client code at the same time.You could have the client mode send a get request to another server application running on your pc (or another ethernet arduino) that would act on the data being sent. It would be up to you to develop that server setup to receive the data from the origional arduino.

If your web page includes the necessary Meta tag for refresh

At…

… we read…

The Meta Refresh Tag

The meta refresh tag or meta redirect is a tool for reloading and redirecting web pages. Remember that the meta refresh tag is easy to use and easy to use poorly.
Reload the Current Page with the Meta Refresh Tag

The meta tag belongs within the of your HTML document. When used to refresh the current page, the syntax looks like this:

(There is more helpful material there, too, such as where you need to put that in your web-page. The “600” in the example would result in a refresh every ten minutes.)

Meta refresh test code.

// for W5100 ethernet shield
// the IP address will be dependent on your local network/router
// port 80 is default for HTTP, but can be changed as needed
// use IP address like http://192.168.1.102:84/ in your brouser
// or http://zoomkat.no-ip.com:84 with dynamic IP service
// use the \ slash to escape the " in the html

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

int x=0;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 102 };
Server server(84);

void setup()
{
  // start the server
  Ethernet.begin(mac, ip);
  server.begin();
}

void loop()
{
  // listen for incoming clients
  Client client = server.available();
  if (client) {
     while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // see if HTTP request has ended with blank line
        if (c == '\n') {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          
          //meta-refresh page every 2 seconds
          x=x+1;
          client.println("<HTML>");
          client.print("<HEAD>");
          client.print("<meta http-equiv=\"refresh\" content=\"2\">");
          client.print("<TITLE />Zoomkat's meta-refresh test</title>");
          client.print("</head>");
          client.println("<BODY>");
          client.print("Zoomkat's meta-refresh test");
          client.println("
");
                    
          client.print("page refresh number ");
          client.println(x);
          client.println("
");
          client.println("
");
          
          client.print("Zoomkat's arduino analog input values:");
          client.println("
");
          client.println("
");
          
          // output the value of each analog input pin
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            client.print("analog input ");
            client.print(analogChannel);
            client.print(" is ");
            client.print(analogRead(analogChannel));
            client.println("
");
            }
           break;
          client.println("</BODY>");
          client.println("</HTML>");
         }
        }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
  }
}