Building Arduino Projects for the Internet of Things

(Steven Felgate) #1

CHAPTER 7 ■ IOT PATTERNS: ON-DEMAND CLIENTS


//Open a new connection to MySQL server
$mysqli = new mysqli($servername, $username, $password, $dbname);


//Output connection errors
if ($mysqli->connect_error)
{
die("[ERROR] Connection Failed: ". $mysqli->connect_error);
}
?>


Receive and Store Sensor Data


As shown in Figure  7-7 , you can create a new file called update.php within the smartparking
folder. This script will perform two tasks—it will first fetch information from the HTTP
request and then it will update the open parking spot count in the database.


Figure 7-7. File to receive and update stored data called update.php


Open the newly created file in a text editor and copy or type the code provided
in Listing 7-3. As mentioned in the previous step, in order to store data, a database
connection needs to be established, and you created util-dbconn.php to perform that
task, so in this file you need to include util-dbconn.php. The util-dbconn.php provides
access to the $mysqli variable, which contains the connection reference and will be used
to run the SQL queries.
The example in this book is hosted at http://bookapps.codifythings.com/
smartparking , and Arduino will be sending open parking spot data to update.php
using an HTTP GET method. As discussed in Chapter 2 , HTTP GET uses a query string
to send request data. So, the complete URL with the query string that Arduino will

Free download pdf