Building Arduino Projects for the Internet of Things

(Steven Felgate) #1

CHAPTER 7 ■ IOT PATTERNS: ON-DEMAND CLIENTS


Get the Parking Spot Count


Your app is not going to directly query the database for the parking spot count; instead
you are going to create a PHP service that returns the information over HTTP. As shown
in Figure  7-8 , you should create a new file called getcount.php in the smartparking
folder. Once the iOS app calls the http://bookapps.codifythings.com/smartparking/
getcount.php URL, the PHP service will return the open parking spots count from the
database in JSON format as part of the HTTP response.


Figure 7-8. File for interface to database is getcount.php


Listing 7-4 provides the complete code for getcount.php , so copy or write the code
in getcount.php. The code requires access to the database so include util-dbconn.php ,
create a new SELECT sql statement, and execute it using $mysqli->query($sql). Check
if any results were returned and pass all the results in JSON format as part of the HTTP
response.


Listing 7-4. Code for Interface to Database in getcount.php


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


$sql = "SELECT PARKING_SPOTS_COUNT FROM PARKING_SPOTS_DATA";
$result = $mysqli->query($sql);
$resultCount = $result->num_rows;


if ($resultCount > 0)
{
$row = $result->fetch_assoc();
print(json_encode($row));
}

Free download pdf