Go Down

Topic: auto refresh on browser (Read 3 times) previous topic - next topic

david1234

Hello,
I'm using sparkfun ProEthernet , and I have connect to him a sensor.
in my code the page change only when I press F5 (refresh) , and so I can see the change on my sensor
what to I need to do /change in order that it will change automatically?
let say every 1 second?
I want to see "live" as possible

this is my code

Quote

void loop()
{
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        if (c == '\n' && currentLineIsBlank) {
                    client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();

         
            client.print("Sensor Status (input 4)");
            if (digitalRead(sensor) ==HIGH)
          {
            digitalWrite(RedLED, LOW);
            digitalWrite(GreenLED,HIGH);               
            client.print("water on");
            client.println("
");
          }
          else
          {
            client.print("water off");
            client.println("
");
            digitalWrite(GreenLED,LOW);
            digitalWrite(RedLED,HIGH);
            delay(5000);
 
          }
 
 
            break;
        }
        if (c == '\n') {
           currentLineIsBlank = true;
        }
        else if (c != '\r') {
                   currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
          }
}



Thank you!

PaulS

There are Meta tags (a google search term) that control how often a client asks the server to refresh the data that it has.

zoomkat

Meta refresh server test code.

Code: [Select]
// zoomkat meta refresh server test code
// arduino IDE 1.0
// 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
// meta refresh set for 2 seconds

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

int x=0; //set refresh counter to 0
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,102); // ip in lan
EthernetServer server(84); //server is using port 84

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

void loop()
{
  // listen for incoming clients
  EthernetClient 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; //page upload counter
          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 IDE 1.0");
          client.println("<br />");
                   
          client.print("page refresh number ");
          client.println(x); //current refresh count
          client.println("<br />");
          client.println("<br />");
         
          client.print("Zoomkat's arduino analog input values:");
          client.println("<br />");
          client.println("<br />");
         
          // 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("<br />");
            }
           break;
          client.println("</BODY>");
          client.println("</HTML>");
         }
        }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
  }
}

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)

david1234

o.k. - it's working
it's make a refresh every 2 second - and show me the status of the sensor
Thank you very much!

bye the way- is there a way to make only the status change? and not all the page to do refresh? and also to add color?
I mean something like this

                                      Sensor test

the status of the sensor is ____________ (all this will stay all the time )
                                   OPEN (in RED)
                                   CLOSE (In GREEN)     this part will change according to the status of the sensor


Thank you ,(again)




PaulS

Quote
is there a way to make only the status change?

There is nothing Arduino specific in what you are asking. These are simple client/server issues, which you could find answers to if you'd bother looking.

Go Up