Building Arduino Projects for the Internet of Things

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

Listing 7-8. Code for Standard Arduino Functions


void setup()
{
// Initialize serial port
Serial.begin(9600);


// Set pin mode
pinMode(ECHOPIN, INPUT);
pinMode(TRIGPIN, OUTPUT);


// Connect Arduino to internet


connectToInternet();


// Calibrate sensor
calibrateSensor();
}


void loop()
{
// Read sensor data
readSensorData();


delay(5000);


}


Your Arduino code is now complete.

Code (iOS)


The final component of your IoT application is an iOS app that will show the number of
open parking spots to the user. The app will fetch the count of open parking spots from
the PHP service whenever the user taps on the Refresh button.


Project Setup


In this section, you are going to set up your Xcode project for the iOS app. You can
download Xcode from https://developer.apple.com/xcode/download/. Xcode can
also be downloaded directly from the Mac App Store. Developing and testing iOS apps in
Xcode is free. You can use built-in simulators to test your apps. In order to test your apps
on an iOS device or publish them to the App Store, you need a paid developer account
( https://developer.apple.com/programs/ ). This chapter uses built-in emulator for
testing, so you do not need a paid developed account to complete this chapter.
Start Xcode from Applications and, as shown in Figure  7-9 , click on Create a New
Xcode Project.

Free download pdf