Building Arduino Projects for the Internet of Things

(Steven Felgate) #1
CHAPTER 7 ■ IOT PATTERNS: ON-DEMAND CLIENTS

be using becomes http://bookapps.codifythings.com/smartparking/update.
php?parkingUpdate= OPEN. Your PHP code will need to extract parkingUpdate from the
query string using a $_GET['parkingUpdate'] statement.
Since you are only checking status of a single parking spot, the default value of
$currentParkingCount in the code is set as 1 , which is the same value with which the
database was initialized. If you were monitoring multiple parking spots, you would simply
add to or subtract from the count based on the data from the proximity sensor. For this
project, the code simply sets the value as 1 whenever Arduino sends OPEN as a value of
the parkingUpdate parameter and sets the value to 0 if Arduino sends OCCUPIED as the
parameter value.
To update this count in the database table, prepare an UPDATE SQL statement in the
$sql variable. You just need to pass the $currentParkingCount value and the TIMESTAMP
value will be auto-generated.
Finally, execute the UPDATE SQL statement using $mysqli->query($sql) and check
the $result variable for success or failure.


Listing 7-3. Code to Receive and Update Stored Data in update.php


<?php
include('util-dbconn.php');


$parkingUpdate = $_GET['parkingUpdate'];


echo "[DEBUG] Parking Update: ". $parkingUpdate. "\n";


$currentParkingCount = 1;


if($parkingUpdate == "OPEN")
{
$currentParkingCount = 1;
}
else
{
$currentParkingCount = 0;
}


$sql = "UPDATE PARKING_SPOTS_DATA SET PARKING_SPOTS_COUNT =
$currentParkingCount";


if (!$result = $mysqli->query($sql))
{
echo "[Error] ". mysqli_error(). "\n";
exit();
}


$mysqli->close();


echo "[DEBUG] Updated Parking Spots Counter Successfully\n";


?>

Free download pdf