Need help http POSTing to server

Hey guys,
I made a web controlled thermostat out of my arduino:
the server is at varano.eu.pn, check it out if you want, and the arduino runs a GET command on the server to find out what temperature I set.
I would also like the arduino to POST data to the server, for example the room temperature it already displays on its screen.
I have no idea where to start... can someone suggest me a path? What will my php say? what does the arduino do?

Thanks!
E :slight_smile:

and the arduino runs a GET command on the server to find out what temperature I set.

So, you know how to use a GET command to trigger a php script. And, you know how to write a php script to respond to a GET command.

I would also like the arduino to POST data to the server

Why? The differences between a POST command and a GET command are rather trivial. If you already know how to GET a script to do something, why not go with that approach?

POST is usually used when trying to hide the data that the script needs from prying eyes. I fail to see how the temperature in your room is a secret bit of data, or why you care if a packet sniffer sees the temperature.

Here are GET and POST method examples:
http://playground.arduino.cc/Code/WebClient

You might have to mix and match the setup functions to use the proper network assigment for your localnet.

So PaulS,you're saying that i can just do the inverse? Get the server to run a GET on the arduino??
Any more clues? :stuck_out_tongue:

Thanks !

Thanks SurferTim, I was looking for some examples!

Last week I had done a small script to connect my arduino to Mysql with a GET method.
Here is my script:

<html>
<?php
$value = $_GET['value'];
//echo $value;
  
//Get date and time
// Return date/time info of a timestamp; then format the output
$mydate=getdate(date("U"));
$timestamp = "$mydate[year]-$mydate[mon]-$mydate[mday] $mydate[hours]:$mydate[minutes]:$mydate[seconds]";

//-----------Setup Mysql Connection
  
$user = 'energy';
$pass = 'monitor';
$hostname = 'localhost';
$databaseName = 'energymonitor';
  
$linkSQL = mysqli_connect($hostname,$user,$pass,$databaseName);
  
   
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
  
//Construct the sql query and store it in a varaible 

$sql="INSERT INTO sensores (name, value,date) VALUES('modem','$value','$timestamp')";
if (!mysqli_query($linkSQL,$sql))
{
die('Error: ' . mysqli_error($linkSQL));
}
  
echo 'Success'; 
mysqli_close($linkSQL);
  
?>
</html>

With this script you can pass data using http://localhost/myscript.php?value=99
It could give you an idea how simple a a GET method could be done.

So PaulS,you're saying that i can just do the inverse? Get the server to run a GET on the arduino??
Any more clues?

No. A GET request can have "arguments" though.

GET /script?arg1=val1&arg2=val2&arg3=val3

The script (recordTempertature.php for instance) then uses the $_GET array to get the name=value pairs, and uses the values as required.

As HugoPT illustrates.

Ok thanks a lot guys!
I think I get it...
I'll post my code if i cant get it to work :slight_smile:

Ciao!

YO!
not working...

code running on arduino:

if (client.connect(serverName, 80)) {
      //  Serial.println("connecting...");
      // send the HTTP PUT request:
      client.print("GET http://varano.eu.pn/arduino.php?t=");
      client.print(TEMPr);
      client.println(" HTTP/1.0");
      //   client.println("Host: www.arduino.cc");
      //   client.println("User-Agent: arduino-ethernet");
      //   client.println("Connection: close");
      client.println();
    }

this is my server's code

<html>
<?php
  // ARDUINO TO WEB SERVER
  $t = $_GET['t'];
  file_put_contents('arduino-temp.txt', '<'.$t.'.0'.'>');
  //return 'ok';
?>
</html>

arduino-temp.txt remains blank...
It shouldn't...

What's wrong?

Love you guys :smiley:

      client.print("GET http://varano.eu.pn/arduino.php?t=");

The Arduino only communicates using one protocol, so the http: bit is not necessary. The client.connect() call has already connected to the server, so the server name is not necessary.

Try:

      client.print("GET /arduino.php?t=");