Pages: [1]   Go Down
Author Topic: POST request on a webserveur using Arduino Ethernet shield  (Read 3335 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi everyone !

First of all, sorry for my bad english, I'm french. smiley
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:
#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:
<?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
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 115
Posts: 5367
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
<?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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 115
Posts: 5367
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: