Webserver

Hello,

I have written a small code to be able to see results of my temperature sensor online using Ethernet shield. Here it is:

#include <Ethernet.h>
#include <SPI.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
EthernetServer server(80);
float temperature = 0.0;
float kelvin = 0.0;
void setup() {
  SPI.begin();
  Ethernet.begin(mac);
  server.begin();
  Serial.begin(9600);
  delay(1000);

}

void loop() { 
  Serial.println("Getting reading");

  kelvin = analogRead(0) * 0.004882812 * 100;
  temperature = (kelvin - 273.15);

  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.println(" degrees C");

  EthernetClient client = server.available();
  if (client) {
    Serial.println("Got a client");
    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("Temperature: ");
          client.print(temperature);
          client.print(" degrees C");  
          break;
        }
        if (c == '\n') {
          currentLineIsBlank = true;
        } 
        else if (c != '\r') {
          currentLineIsBlank = false;
        }
      }
    }
    delay(1);
    client.stop();
  }
}

With this code I need to refresh every time to see current value.
Kindly tell me how is it possible that page is refreshed automatically.

Also, what is the easiest way to save this output in a xls or csv file.

Thanks.

Best,
Z

If not refresh then I would like to see all collected values on screen based on defined delay in code.

Thanks.

Z

Okay I solved!
Here is the code:

#include <Ethernet.h>
#include <SPI.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
EthernetServer server(80);
float temperature = 0.0;
float kelvin = 0.0;
long lastReadingTime = 0;
void setup() {
  SPI.begin();
  Ethernet.begin(mac);
  server.begin();
  Serial.begin(9600);
  delay(1000);

}

void loop() { 
  kelvin = analogRead(0) * 0.004882812 * 100;
  temperature = (kelvin - 273.15);
  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.println(" degrees C");
  checkForClient();
}
void checkForClient(){
  EthernetClient client = server.available();
  if (client) {
    Serial.println("Got a client");
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        if (c == '\n' && currentLineIsBlank) {
          client.println("HTTP/1.0 200 OK\r\n"
    "Content-Type: text/html\r\n"
    "Pragma: no-cache\r\n"
    "\r\n"
    "<meta http-equiv='refresh' content='1'/>");
          client.println();
          client.print("Temperature: ");
          client.print(temperature);
          client.print(" degrees C");  
          break;
        }
        if (c == '\n') {
          currentLineIsBlank = true;
        } 
        else if (c != '\r') {
          currentLineIsBlank = false;
        }
      }
    }
    delay(1);
    client.stop();
  }
}

I still want to know how to save the output in a xls or csv file.

Z

Someone?

I still want to know how to save the output in a xls or csv file.

You can't run Excel on an Arduino, so you can't create an xls file. You could write data in a csv format, but where? Where do you want to save this file?

zeus2kx:
Also, what is the easiest way to save this output in a xls or csv file.

On the Arduino's hard disk drive. Oh, wait ...

PaulS:
Where do you want to save this file?

On my website or any server where I can download file from.
For now I am writing a csv file on SD card and attaching in a mail to me which Arduino sends every 24 hours.
But I am looking for something similar to Pachube feeds. There were problems when I tried it. I would be thankful if you could help me write a code for Pachube. Since I am testing, I need just one data stream for temperature like in above code.

Thanks.

Z

On my website or any server where I can download file from.

You can't do that (directly). You need an application on the PC that is listening to the serial port, and that writes the data it receives to the file. There is an application called GoBetwino that does this, that can be relatively easily controlled by the Arduino, if you are using Windoze.

I don't have Arduino connected with a computer. I can't read serial port, or am I wrong?
Every unit is indeed having internet using ethernet shield.
What do you suggest in this case?

I don't have Arduino connected with a computer. I can't read serial port, or am I wrong?

No, you are right.

Every unit is indeed having internet using ethernet shield.
What do you suggest in this case?

You can use scripts on the web server, if the Arduino is a client, to store the data. If the Arduino is a server, then it is up to the client to store the data that it requests from the server.

This is strictly a client-server issue, not an Arduino issue.