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