Hello i'm trying to make a script that sends data to a php page and then receives a response.
The idea is that the data send from the arduino is checked by the php page and the page then sends back either a 0 or a 1.
However I have trouble with reading and using the 0 or 1 that is send back from the php page.
For now I wan't to use the 0 or 1 that is send back in an if statement but the response is much more then just the 0 or 1.
Can anyone give me a solution or advice on how I can do this?
Arduino code
#include <Ethernet.h>
#include <SPI.h>
byte mac[] = { 0x90,0xA2,0xDA,0x00,0x55,0x8D};
EthernetClient client;
void setup()
{
Serial.begin(9600);
Ethernet.begin(mac);
delay(1000);
Serial.println("connecting...");
pinMode(7, OUTPUT);
String data;
data+="";
data+="data=1234";
data+="&submit=Submit"; // Submitting data
if (client.connect("192.168.107.222",80))
{
Serial.println("connected");
client.println("POST /pin/abovePage.php HTTP/1.1");
client.println("Host: 192.168.107.222");
client.println("Content-Type: application/x-www-form-urlencoded");
client.println("Connection: close");
client.print("Content-Length: ");
client.println(data.length());
client.println();
client.print(data);
client.println();
//Prints your post request out for debugging
Serial.println("POST /pin/abovePage.php HTTP/1.1");
Serial.println("Host: 192.168.107.222");
Serial.println("Content-Type: application/x-www-form-urlencoded");
Serial.println("Connection: close");
Serial.print("Content-Length: ");
Serial.println(data.length());
Serial.println();
Serial.print(data);
Serial.println();
}
delay(2000);
Serial.println("Response: ");
}
void loop()
{
if (client.available())
{
char c = client.read();
Serial.println(c);
}
}
This is the output when the code is executed:
HTTP/1.1 200 OK DATE: Thu, 27 Feb 2014 09:24:23 GMT Server: Apache/2.2.22 (Win32) PHP/5.4.3 X-Powerd-By: PHP/5.4.3 Cache-Control: no-cache, must-revalidate Expires: Sat, 26 Jul 1997 05:00:00 GMT Content-Length: 1 Connection: close Content-Type: text/html 1
PHP code, I plan on using a database connection to check the data later but this is the basic idea.
<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Content-type: text/html");
$data = $_POST["data"];
switch ($data)
{
case '1234':
echo('1');
break;
default:
echo('0');
break;
}
?>