Building Arduino Projects for the Internet of Things

(Steven Felgate) #1
CHAPTER 9 ■ IOT PATTERNS: LOCATION AWARE

$password = "DB_PASSWORD";


//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  9-7 , create a new file called update.php in the gpstracker folder. This
script will perform two tasks—first it will fetch information from an HTTP request and
then it will update this information in the database table.
Open the newly created file in a text editor and copy or type the code provided


in Listing 9-3. As mentioned in the previous step, in order to store data, a database
connection needs to be established. You created util-dbconn.php to perform that task,
so in this file you need to include util-dbconn.php. The util-dbconn.php file provides
access to the $mysqli variable, which contains connection references and will be used to
run the SQL queries.
The example in this book is hosted at http://bookapps.codifythings.com/
gpstracker/ , and Arduino will be sending GPS coordinates 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 be using becomes
http://bookapps.codifythings.com/gpstracker/update.php?clientID=Sheep1&lat


Figure 9-7. File to receive and add/update data in update.php

Free download pdf