A sample text widget

Etiam pulvinar consectetur dolor sed malesuada. Ut convallis euismod dolor nec pretium. Nunc ut tristique massa.

Nam sodales mi vitae dolor ullamcorper et vulputate enim accumsan. Morbi orci magna, tincidunt vitae molestie nec, molestie at mi. Nulla nulla lorem, suscipit in posuere in, interdum non magna.

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

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

Bookmark this post: bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark

19 comments to Arduino sending requests to PHP script

  • atmoz

    Thanks for this very nice and relatively “simple” to understand code!! This is exactly what I was searching for. Many many thanks and greets from the Netherlands…

    With kind regards,


  • benhowdle

    Brilliant!! Exactly what i have been searching for, however when i come to compile i get this error:

    error: no matching function for call to ‘EtherShield::ES_client_browse_url(char*, char [50], char*, NULL, void (*)(uint8_t, uint16_t))

    Any ideas?


  • Administrator

    Hi Ben,

    have a look in the library directory and check that the ip_config.h file has the correct #define’s set for your application. I think you’ll need these for a client:

    #define WWW_client
    #define FROMDECODE_websrv_help
    #define URLENCODE_websrv_help

    Hope this helps


  • benhowdle

    Hi Andy,

    Thanks for your reply, i did double (and triple) check that all those were defined but still luck ๐Ÿ™

    The exact error is this:

    In function ‘void loop()’:
    error: no matching function for call to ‘EtherShield::ES_client_browse_url(char*, char [50], char*, NULL, void (*)(uint8_t, uint16_t))’pathtoarduino\arduino-0018\libraries\etherShield/etherShield.h:71: note: candidates are: void EtherShield::ES_client_browse_url(prog_char*, char*, prog_char*, void (*)(uint8_t, uint16_t))

    No idea!

  • Administrator

    Hi Ben,

    Do you have the sketch, in particular the line that uses the ES_client_browse_url function? I think what is happening is that your parameters are slightly wrong.
    The first parameter is the url and should be a PSTR( “/test.php” ). The second parameter is a run time derived string that is appended to the url and would be something line “?result1=1234&result2=3435”. The third is the virtual host, again PSTR(“” ) so that it builds the correct packet for use with virtual hosting services. The fourth is the call back function for when the response is received.

    Your error seems to have char* which would indicate you’re not using the PSTR function to extract static strings from program memory.

    Hope this helps


  • brob

    Hi Andy,

    Sorry for being a total noob, but if I can post to PHP pages I’ll be able to do a lot more with the Arduino.

    This may be a stupid question but where do I find the file #include you refer to in the sketch?


  • Hi Ben,

    Which include files (or libraries) there are 2.

    etherShield.h is my ethershield library, see post here
    SHT1x.h this is a library for the SHT1x temp/humidity sensors, cant remember where it originally came from but details are in library available here

    Hope this helps.


    The first one is

  • brob

    Great thanks for the SHT1 files.

    One more error I’m getting

    In function ‘void setup()’:
    error: ‘class EtherShield’ has no member named ‘ES_client_set_gwip’ In function ‘void loop()’:

    Checking the etherShield.h and I can see void ES_client_set_gwip(uint8_t *gwipaddr); being set

    Do you know what this could be?

    Thanks again

  • benhowdle

    Hi Andy, i have managed to get this script up and running and i’m very happy with the results, i currently have the Arduino pumping readings up to our company website via php/mysql. I have set the update time in the Arduino sketch to 30 seconds but the actual intervals it sends the data on are a bit random, some are 2 mins, some 5 mins, some 1.5 mins. Any idea why this could be, is it because i’m sending the data to a public site rather than just my localhost?

    Thanks for any feedback!

    Great script!

  • Hi,

    If you’ve not worked it out already, check the file ip_config.h in the library, there should be a line with the following:

    #define WWW_client

    if it says #undef then change it to #define and re-compile.



  • Hi Ben,
    Sounds a bit odd, not entirely sure what the problem could be. Was it ok with your local webserver? Unless you have access to the logs on your new server this one might be tricky to debug.



  • benhowdle

    I think the difference was that when it was working fine, i was manually putting the values into the statusstring to send to the PHP to test the relationship was ok, when i then wanted to grab live data from analogs and digitals it seemed to starting playing up on the timings…is it the amount of values i’m sending, i’m sending 10 digital ins and 6 analog ins in one string, would this be a problem or not?

    Thanks for your help…

  • benhowdle

    Hi Andy,

    just wanted to let know i found the problem with the timings of the updates – turns out i was trying to send too many characters in “statusstring” – just had to up the buffer size from 50 and all works fine! Thanks anyway

  • stavast

    As a contribution to the code above I would like to share the following php code which i juse to write straight to a mysql database

    as a database name i jused arduino
    as table name i jused temp
    field names are date_time, sensor1, sensor2, sensor3

    it’s not a very exciting code for the experts, but i think it’s very helpfull to some noobs ๐Ÿ™‚

  • stavast

    hmm.. php is not allowed, Andy could you add this to my previous comment? thnx

  • stavast

    Ben, try the change this line




    that did the trick for me.

    please notice that i’ve also followed up the advice Andy gave you.

    “check the file ip_config.h in the library, there should be a line with the following:

    #define WWW_client

    if it says #undef then change it to #define and re-compile.”

    now there are two #define WWW_client in ip_config.h

  • joeinbend

    Hi Andy,
    Thanks for posting this example! I’m hoping you can help me, as I’ve searched high and low and come up empty. In your example, you have the data sending to php encapsulated in a WHILE() statement, which is making it very difficult to integrate into a larger Arduino sketch. It does a great job of writing my data to my server, however because of the WHILE(), it is “stuck” in that loop, and I am unable to run the normal LOOP() procedure.

    I tried just moving the WHILE() out into the regular LOOP() part of the sketch, however that then means that the “Continue();” at the bottom of the loop is not valid (only valid in a while, do, for). I tried just removing “continue();” but it does not write data to php.

    I also tried keeping the whole WHILE() in tact, and just wrapping it in a function, and calling to that function, then adding a line to break out of the WHILE() at the end, but that fails to write anything either.

    Is there a way to simplify just the “business end” of the sketch, down to a just bare minimum to write to a PHP? So far anything I have tried just ends up with a failure to write anything to my database.

  • You have to put the command es.ES_enc28j60SpiInit (); before es.ES_enc28j60Init (mymac);

  • Hi,

    How can i achieve this using a GSM connection and send the data to my PHP Script on my web server?