Building Arduino Projects for the Internet of Things

(Steven Felgate) #1
CHAPTER 5 ■ IOT PATTERNS: REALTIME CLIENTS

Standard Functions


The code in the fifth and final section implements Arduino’s standard setup() and
loop() functions.
In the setup() function, you initialize the serial port, connect to the Internet, and
calibrate the sensor for correct readings, as shown in Listing 5-6.

Listing 5-6. Code for Standard Arduino Function—setup()

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

// Connect Arduino to internet
connectToInternet();

// Calibrate motion sensor
calibrateSensor();
}

In the loop() function, you only need to call the readSensorData() function , as
shown in Listing 5-7.

Listing 5-7. Code for Standard Arduino Function—loop()

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

Your Arduino code is now complete.

Code (Android) x


This section provides instructions for developing an Android app that will fulfill the
following two requirements:


  • Display a notification in realtime whenever motion is detected by
    the sensor

  • Create a simple screen where app users can see when last motion
    was detected

Free download pdf