Building Arduino Projects for the Internet of Things

(Steven Felgate) #1

CHAPTER 4 ■ COMPLEX FLOWS: NODE-RED


Standard Functions


Finally, the code in the fifth and last section is provided in Listing 4-6. It implements
Arduino’s standard setup() and loop() functions.
The setup() function initializes the serial port and connects to the Internet. While
it’s in the loop() function, it calls doHttpGet() at an interval of 5,000 milliseconds. The
doHttpGet() function reads the sensor data and sends this sensor value to Node-RED in
an HTTP request.


Listing 4-6. Code for the Standard Arduino Functions


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


// Connect Arduino to internet
connectToInternet();
}


void loop()
{
// Make HTTP GET request
doHttpGet();


delay(5000);


}


Your Arduino code is now complete.

Final Product


To test the application, make sure your Node-RED server is up and running with the flow
deployed.
Also verify and upload the Arduino code, as discussed in Chapter 1. Once the code
has been uploaded, open the Serial Monitor window. You will start seeing log messages
similar to the ones shown in Figure  4-27.

Free download pdf