Hi everyone,
I would need so help. I am trying to send data to a php page using an arduino with Ethernet Shield.
I manage to send data using the GET method but it is not a safe way. So I am now trying the POST method but I have no idea why it won't work.
Here is the arduino code.
#include <SPI.h>
#include <Ethernet.h>
long randNumber;
//Affectation d'une adresse MAC
byte mac[] = { 0xB8, 0x27, 0xEB, 0xFE, 0xB2, 0x49 };
// Serveur avec Mysql
IPAddress server(192,168,0,100);
bool connected = false;
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
void setup()
{
Serial.begin(9600);
randomSeed(analogRead(0));
if (Ethernet.begin(mac) == 0)
{
Serial.println("Impossible de configurer Ethernet en utilisant DHCP");
// Attente perpétuelle
for(;;);
}
Serial.println("Configuration Ethernet DHCP : OK \n@IP : ");
for (byte thisByte = 0; thisByte < 4; thisByte++)
{
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.println(".");
}
delay(1000);
}
int val=0;
void loop()
{
int val = random(300);
String DatatoSend ="";
DatatoSend += "Data=";
DatatoSend += (val);
Serial.println("DatatoSend :" + DatatoSend );
if (client.connect(server, 80))
{
Serial.println("\nconnected...");
Serial.println("ARDUINO: forming HTTP request message");
client.println("POST /Test/PhpPost.php HTTP/1.1");
client.println("From: Arduino1 ");
client.println("User-Agent: HTTPTool/1.0");
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length:");
client.println(DatatoSend.length());
client.println("Connection: close");
client.println(DatatoSend);
client.println();
Serial.println("ARDUINO: HTTP message sent");
delay(3000);
if(client.available())
{
Serial.println("ARDUINO: HTTP message received");
Serial.println("ARDUINO: printing received headers and script response...\n");
while(client.available())
{
char c = client.read();
Serial.print(c);
}
}
else
{
Serial.println("ARDUINO: no response received / no response received in time");
}
client.stop();
}
else
{
Serial.println("connection failure");
}
delay(2000);
}
and the Php Page :
<?php
$Variable = $_POST['Data'];
echo $Variable ;
?>
Thanks in advance for your Help