Difference between revisions of "JSON from arduino to PHP server"
From Wiki2
| Line 24: | Line 24: | ||
<?php | <?php | ||
$thefile=file_get_contents("php://input"); | $thefile=file_get_contents("php://input"); | ||
echo ($thefile); | |||
$thearray = json_decode($thefile); | |||
print_r($thearray); | |||
?> | ?> | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Revision as of 14:19, 12 January 2013
JSON from arduino to PHP server
Encoding pin values in JSON: <syntaxhighlight>
// last pieces of the HTTP PUT request:
client.println("Content-Type: application/json");
client.println("Connection: close");
client.println();
client.print("{");
// here's the actual content of the PUT request:
for (int analogChannel = 2; analogChannel < 6; analogChannel++) {
int sensorReading = analogRead(analogChannel);
String schan = "\"sensor" + analogChannel + "\":";
client.print(schan);
client.print(sensorReading);
if (analogChannel != 5) {
client.print(",");
}
}
client.println("}");
</syntaxhighlight> Reading arduino JSON from PHP server: <syntaxhighlight> <?php $thefile=file_get_contents("php://input"); echo ($thefile); $thearray = json_decode($thefile); print_r($thearray); ?> </syntaxhighlight> Output produced: <syntaxhighlight> {"sensor2":376,"sensor3":349,"sensor4":251,"sensor5":285} stdClass Object (
[sensor2] => 376 [sensor3] => 349 [sensor4] => 251 [sensor5] => 285
) </syntaxhighlight> <sytaxhighlight> </syntaxhighlight>