Go Down

Topic: POST request on a webserveur using Arduino Ethernet shield (Read 5940 times) previous topic - next topic

guigs68

Hi everyone !

First of all, sorry for my bad english, I'm french. :)
Ok, so my goal is to send toward a webserver a data of temperature at regular time. I use a LAMP server on a Raspberry Pi computer and the temperature is measured from an Arduino board linked to an Ethernet shield. For this purpose I set a POST request on the Arduino side to send the value of the variable "temp" to the server. This part seems to work properly because the result to the client.read() function is good and match to the result of the page test.php that I host on my Raspberry Pi.

Here you can see my Arduino script :

   
Code: [Select]
#include <Ethernet.h>
    #include <SPI.h>

    byte mac[] = {  0x90, 0xA2, 0xDA, 0x0D, 0xF6, 0xFF };
    byte ip[] = {  192, 168, 0, 9};
    byte gateway[] = { x,x,x,x };
 
    EthernetClient client;

    String temp= "data=5"; 

    void setup()
    {
    Ethernet.begin(mac, ip, gateway);
    Serial.begin(9600);
    Serial.println(Ethernet.localIP());
    delay(1000);
    delay(1000);
    Serial.println("connecting...");

    if (client.connect("192.168.0.55",80))
    {                                 
    Serial.println("Sending to Server: ");                   
    client.println("POST /test.php HTTP/1.1");           
    Serial.print("POST /test.php HTTP/1.1");           
    client.println("Host: 192.168.0.55");
    client.println("Content-Type: application/x-www-form-urlencoded");
    client.println("Connection: close");
    client.println("User-Agent: Arduino/1.0");
    client.print("Content-Length: ");
    client.println(temp.length());
    client.println();
    client.print(temp);
    client.println();                                           
    }

    else
    {
    Serial.println("Cannot connect to Server");               
    }

    }

    void loop()                                           
    {       
    if (client.available()) {
    char c = client.read();
    Serial.print(c);
    }

    if (!client.connected())
    {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;;)
      ;
    }                               

    }


And this is my file test.php :

   
Code: [Select]
<?php
    
echo 'Temperature = ' htmlspecialchars($_POST["data"]) . '!';
    
?>


The result of client.read() on the Arduino serial terminal is 5, that's proved that the POST request and PHP part are working.
However, if I go on my browser at the url : http://192.168.0.55/test.php, only "Temperature = " is displayed. The value (5) is missing.
So if somebody know how can I display the value directly on my browser, it helps me a lot.

Regards
Guillaume

SurferTim

You can't directly with a URL in the address bar. You need a form with a METHOD=POST to do that.

If you want to test it, add this to your php page.
Code: [Select]
<?php
    
echo 'POST Temperature = ' htmlspecialchars($_POST["data"]) . '!<br>';
    echo 
'GET Temperature = ' htmlspecialchars($_GET["data"]) . '!';
?>

Then use this in your browser address bar
http://192.168.0.55/test.php?data=5

guigs68

Thanks for your quick answer SurferTim !
I already tested this and I know that it is working. But for my project I use a temperature sensor connected to the Arduino. So I don't know the temperature value in advance, and it's not my job to "complete a form".
Actually I would like something which send the data automatically to my server and after that (next step) I will be able to gather a database (MySql or Sqlite for instance). I hope to be understandable.


SurferTim

Quote
I already tested this and I know that it is working. But for my project I use a temperature sensor connected to the Arduino. So I don't know the temperature value in advance, and it's not my job to "complete a form".

It isn't your job to "complete a form". The Arduino/ethernet shield is doing that for you. It sounds to me like it is working, and that is good. Much better than not working.

Quote
Actually I would like something which send the data automatically to my server and after that (next step) I will be able to gather a database (MySql or Sqlite for instance).

It sounds like the Arduino is already doing that. Now you need to regulate how often you send that to your server. I use this kind of stuff with Apache/PHP/MySQL all the time.

mine_Bio

hey french bro, your code is working and for that i wana say thank's bro. But i have made some changes that are below:
1:copy the arduino code and paste it in Arduino IDE(note: no need of gateway, just enter your  IP address for arduino Ethernet and the server IP address).
2: Now server part, i have creat a PHP file which will accept the POST request and save the value(the one came from arduino ethernet) into database. Below is the PHP file
<html>
<body>

<?php
if($_SERVER["REQUEST_METHOD"] == "POST") //if request is a post request, in this case post request from //arduino
{
$id = $_POST["data"]; //As in above arduino code data=5
}
?>//post request ends here


<?php
$servername = "XXXXXXXXXXXXXXXX";//server name of database
$username = "XXXXXXXXXXXXXXXXXX";//username of database
$password = "XXXXXXXXXXXXXXXXX";//password of database
$dbname = "XXXXXXXXXXXXXXXX";//database name

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);//now connect to your database
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);//if any error during connection then cut the //connection
}

$sql = "INSERT INTO table_name (field_name)//now insert the value in a table using table name and //corresponding field name(column name)
VALUES ('$id')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

</body>
</html>
note: if you no knowledge about mySql database link is http://www.w3schools.com/php/php_mysql_intro.asp

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy