Building Arduino Projects for the Internet of Things

(Steven Felgate) #1
CHAPTER 4 ■ COMPLEX FLOWS: NODE-RED

The HTTP response node will simply send msg.payload back to the client in the
JSON format. Change the name of this node to Send HTTP Response.
Connect the Send HTTP Response node to the second path of the Check Threshold
switch node and also to the Set Tweet Message. Your final flow should look similar to
Figure  4-26.


Figure 4-25. H T T P r e s p o n s e n o d e


Figure 4-26. Completed Node-RED flow


Code (Arduino)


Next, you are going to write code for connecting Arduino to the Internet using WiFi,
reading light sensor data, and sending it to the Node-RED server as an HTTP request.
Start your Arduino IDE and either type the code provided here 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 ( GET )

  • Standard functions


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


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


Listing 4-3. Code for Including External Dependencies


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

Free download pdf