Hardware Reference
In-Depth Information
Start the
cURL
call, as shown in the following line of code:
$curl = curl_init();
We also have to set the options of the
cURL
call. We want the call to return the data from
our Arduino web server, and we also want the URL to be the one we defined before. This
is done using the following code:
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
));
We can now execute the command, as shown in the following line of code::
$resp = curl_exec($curl);
Close the
cURL
call, as follows:
curl_close($curl);
We get the answer in a variable that contains the data in a string. In order to open it with
PHP, we need to convert it to the JSON format first, and then extract the temperature and
humidity fields. This is done using the following piece of code:
$json = json_decode($resp, true);
$temperature = intval($json["temperature"]);
$humidity = intval($json["humidity"]);
The rest of the file is strictly identical to the code we developed in the previous chapter.
Now, we also need to make a small modification in the
plot.html
file. In the previous
chapter, the data was automatically logged inside the database as the Arduino board was
constantly sending data to the server. Here, we need to make the call on the server side.
This is done by adding the following small piece of code just before the code that reads
out the data from the database:
$.ajax({
url: "datalogger.php",