1
votes

First of all, sorry for my bad English, I'm French. :)

OK, so my goal is to send toward a web server 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:

#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 :

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

1
If you re interested in a database that may be easier to use, you might want to look at the Google App Engine data store. Google App Engine also now supports PHP. Google App Engine PHP Getting Starteduser2513811

1 Answers

1
votes

Well, you're not doing it right, but it's the whole http request concept you're not getting right so it would be quite long to explain in detail. To be short :

your arduino is doing a post http request to your server. As you're not specifying a page, it's targeting the default page, most probably index.php. What you should do there is capture the POST data and store it somewhere, most probably a DB but it could aswell be a file.

when you're displaying the test.php page, there was no posted data during that request (which is not the one initiated by your arduino controller), so there's nothing to show. What you should do there is querying your database to display the data stored in previous step

EDIT :

Here's a few step about what you should do :

  1. Create database table to store your data. It would be good to store the temperature, but also the date when it was stored, and eventually some "remark" field for the future
  2. Create a PhP script that handles POSTED data and store it in the database
  3. Make a request to that script from a very basic HTML form : 1 texbox with the name of your post variable on your arduino , 1 button for submit. This form will allow you to test your PhP script
  4. once you're ok with it, make the same request from your arduino
  5. from there, if everything goes well, you'll be able to store data sent from your arduino into the database. Using something like PhPMyadmin you should be able to see the data
  6. Write a PhP script to read and display data stored in your database (also quite a basic operation, you'll find lots of tutorials to do it)