Building Arduino Projects for the Internet of Things

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

else
{
echo "0 results";
}
$mysqli->close();
?>


Code (Arduino)


The second component of this project is the Arduino code. This code connects Arduino
to the Internet using WiFi, checks if parking spot is open or not, and publishes this
information to a server.
Start your Arduino IDE and either type the code provided or download it from book’s
site and open it. All the code goes into a single source file ( *.ino ), but in order to make it
easy to understand and reuse, it has been divided into five sections.



  • External libraries

  • Internet connectivity (WiFi)

  • Read sensor data

  • HTTP (publish)

  • Standard functions


External Libraries..................................................................................................


The first section of code, as provided in Listing 7-5 , includes all external libraries required
to run the code. Since you are connecting to the Internet wirelessly, the main dependency
of the code is on <WiFi.h>.


Listing 7-5. Code for Including External Dependencies


#include <SPI.h>
#include <WiFi.h>


Internet Connectivity (Wireless)


The second section of the code defines variables, constants, and functions that are going
to be used for connecting to the Internet. Use the code from Listings 2-7, 2-8, and 2-9
(in Chapter 2 ) here.

Free download pdf