In this tutorial, I’m going to show how to develop a small website that gets data from Arduino (using your ethernet shield), stores it and displays it on a chart.
Your website will be made by two pages:
- the first, saveTemp.php, gets data from Arduino and stores it in text file;
- the second, displayTemp.php, reads the text file and displays the data.
In this example, Arduino gets room’s temperature using the SD18B20 sensor; I’ve already blogged about this sensor in this post.
For programming the web pages, I chose the PHP language, widely used on the web.
I use the GET method of the HTTP protocol to send data to the webserver. That method sends the parameters in the clear within the URL, using the form parameter=value and dividing the different parameters with the & symbol:
To prevent other users from sending data, in addition to the temp parameter you must also send to the webpage a password using the pwd parameter.
My sketch reads the temperature from the sensor, converts it in a string value and “print” it in a Stash object:
sensors.requestTemperatures(); float float_temp = sensors.getTempCByIndex(0); char string_temp; dtostrf(float_temp, 4, 2, string_temp); Stash stash; byte sd = stash.create(); stash.print(string_temp); stash.save();
Then it prepares the request in a different Stash object and sends it with the tcpSend() method:
Stash::prepare(PSTR("GET /demo/saveTemp.php?temp=$H&pwd=$F HTTP/1.0" "\r\n" "Host: $F" "\r\n" "\r\n"), sd, password, website); session_id = ether.tcpSend();
Thanks to the session identifier (session_id) you can get the response from the server and check if it contains an error message (“KO”):
const char* reply = ether.tcpReply(session_id); [...] if(strstr(reply, "KO - ") != 0) Serial.println(strstr(reply, "KO - "));
In PHP you can get the data using the associative array $_GET:
$temp = $_GET["temp"]; $pwd = $_GET["pwd"];
After having verified the password, the temperature value is stored in the file together with a timestamp:
define("LOG_FILE", "./temperatures.csv"); [...] $file_handler = fopen(LOG_FILE, "a+"); fwrite($file_handler, time() . "," . $temp . "\n");
In the next page, you’ll learn how to display the data on a chart…
Pages: 1 2