2011:arduino_to_php

This is an old revision of the document!


Arduino sending requests to PHP script

I’ve been asked by a number of people to provide an example of using the EtherShield library to pass data to a PHP script, to for example store sensor data in a database.

The example here is based on existing code that reads the SHT1x temperature and humidity sensor. The results are passed as parameters to a php script running on a local linux server. The php script then saves the results in a simple text file.

The url format for this example is

http://10.0.0.2/storedata.php?p1=temp&p2=humidity&p3=dewpoint

The example Arduino sketch can be downloaded here

The PHP script used to test is

 < ?php
 $p1 = $_GET['p1'];
 $p2 = $_GET['p2'];
 $p3 = $_GET['p3'];
 $timestamp = mktime();
 //echo "Timestamp is ".date("d/m/Y H:i:s", $timestamp);
 $your_data = date("d/m/Y H:i:s", $timestamp) . "," . $p1 . "," . $p2 . "," . $p3
 . "\n";
 echo "OK";
 // Open the file for appending
 $fp = fopen("/var/www/data.txt", "a");
 // Write the data to the file
  fwrite($fp, $your_data);
 // Close the file
 fclose($fp);
 ?>

This creates a file like this:

07/01/2010 20:58:04,18.95,41.16,5.51 07/01/2010 20:58:34,18.91,40.94,5.37 07/01/2010 20:59:04,18.62,41.69,5.41 07/01/2010 20:59:34,18.48,42.03,5.40 07/01/2010 21:00:04,18.50,41.86,5.36 07/01/2010 21:00:34,18.44,42.01,5.39

2011/arduino_to_php.1313155106.txt.gz · Last modified: 2011/08/12 09:18 by tomgee