Hardware Reference
In-Depth Information
Plotting the data locally
To end the chapter, we are going to see how to plot data measured by the Arduino board.
To do so, we will modify the Arduino sketch a little bit, and then use part of the code from
the previous chapter to plot the data right in your web browser.
First, we are going to modify the Arduino sketch so that it returns data in a more useful
format; in the present case, the JSON format. In place of the code responsible to print out
the measurements, we are going to simply print the data in the JSON format. This is done
using the following piece of code:
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: application/json");
client.println("Connection: close");
client.println();
client.print("{\"temperature\": ");
client.print(temp);
client.print(", \"humidity\": ");
client.print(hum);
client.println("}");
We can now quickly test this project. Upload the code to the Arduino board again, open
your web browser, and go to the same IP address you used before. You should see the fol-
lowing line printed on the page:
{"temperature": 24, "humidity": 36}
Now, we also have to build the server-side code to plot the data. The code is very similar to
the code of the previous chapter, so I will only detail the differences between the two pro-
jects. In the previous chapter, it was the board that was trying to reach out the server run-
ning on your computer, and the datalogger.php file was handling the requests. In this
chapter, we are going to do the reverse and call the board from the web server running on
your computer. To do so, we are going to use a module from PHP called cURL to make
GET requests to a given URL; in this case, the URL of the Arduino board.
The first thing we have to do is make changes to the datalogger.php file and add the
URL of the Arduino board, as shown in the following line of code:
$url = 'http://192.168.1.103';
Search WWH ::




Custom Search