Difference between revisions of "JSON from arduino to PHP server"
From Wiki2
Line 1: | Line 1: | ||
=====JSON from arduino to PHP server===== | =====JSON from arduino to PHP server===== | ||
Encoding pin values in JSON: | Encoding pin values in JSON: | ||
< | <syntaxhighlight> | ||
// last pieces of the HTTP PUT request: | // last pieces of the HTTP PUT request: | ||
client.println("Content-Type: application/json"); | client.println("Content-Type: application/json"); | ||
Line 21: | Line 21: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Reading arduino JSON from PHP server: | Reading arduino JSON from PHP server: | ||
< | <syntaxhighlight> | ||
<?php | <?php | ||
$thefile=file_get_contents("php://input"); | $thefile=file_get_contents("php://input"); | ||
Line 31: | Line 31: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Output produced: | Output produced: | ||
< | <syntaxhighlight> | ||
{"sensor2":376,"sensor3":349,"sensor4":251,"sensor5":285} | {"sensor2":376,"sensor3":349,"sensor4":251,"sensor5":285} | ||
stdClass Object | stdClass Object |
Revision as of 15:17, 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 "I want ".sizeOf($thefile)." of them\n\n"; 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>