Making a sample flow
In this section, we will create these two sensor data output flows in the Node-RED flow editor.
You will use the sensor modules you have prepared to collect data and create a sample flow to visualize it on Node-RED. By using two different sensor modules, we can learn the basics of data handling in Node-RED.
Use case 1 – light sensor
The first is a light sensor. Let's create a flow (application) that detects light and outputs the value detected by a fixed-point observation to a log:
Connect the light sensor module to the Raspberry Pi and use the Node-RED flow editor on the Raspberry Pi to output the data obtained as a standard output.
Use case 2 – temperature/humidity sensor
The second one is a temperature/humidity sensor. Let's create an application (flow) that detects temperature and humidity and outputs the value detected by a fixed-point observation to a log:
Connect the temperature/humidity sensor module to the Raspberry Pi and use the Node-RED flow editor on the Raspberry Pi to output the data obtained as a standard output.
If you want to spot test these two use cases on your device, you need to connect a sensor that you can use to obtain sensor data.
You may have to prepare this before creating the flow.
This time, we will use Grove Base HAT, which is easy to use with Raspberry Pi, and as this setup was completed in the previous step, we are ready to access the data on Raspberry Pi. However, we have not yet prepared Node-RED. It is difficult to access this data with Node-RED as default. One way is to use a Function node and code the script from scratch, which is very difficult but not impossible.
For handling the sensing data recognized by Raspberry Pi on Node-RED, a "node" dedicated to Grove Base HAT is required.
The good news is that you can start using the node right away. This is because Seigo Tanaka, a Node-RED User Group Japan board member (https://nodered.jp/) and Node-RED contributor, has already created and released a node for Grove Base HAT. This is the node for the Grove Base HAT for Raspberry Pi:
node-red-contrib-grove-base-hat
You can read more about it here: https://www.npmjs.com/package/node-red-contrib-grove-base-hat.
If you need a refresher on how to install nodes that are published on the node library, please read the Getting several nodes from the library section in Chapter 4, Learning the Major Nodes.
The reason I refer you back to this is that the next step is to install the node for the Grove Base HAT from the library into your environment.
Let's enable the use of this Grove Base HAT node in our Node-RED flow editor:
- Click the menu at the top right and select Manage palette to open the settings panel:
- When the settings panel is opened, type the name of the node you want to use in the search window. We want to use node-red-contrib-grove-base-hat, so please type the following:
grove base
- After that, you can see the node-red-contrib-grove-base-hat node in the search window. Click the Install button:
- After clicking the Install button, you will see a message asking you to read the documentation to find out more information about this node. Read the document if necessary, and then click the Install button on the message box:
Now you are ready to use the node for Grove Base HAT. Check the palette in the flow editor. At the bottom of the palette, you can see that the Grove Base HAT node has been added:
There are many sensing modules that can be connected to Grove Base HAT. This time, only the light and temperature/humidity sensors are used, but there are other things that can be seen by looking at the types of nodes.
The procedure followed for the two use cases created here can also be applied when using other sensors. If you are interested, please try other sensors too. In the next section, we will make a flow for use case 1.
Making a flow for use case 1 – light sensor
In use case 1, Node-RED can be used to handle the illuminance obtained from the light sensor as JSON data. That data can be handled as JSON data, then be sent to the server side afterward, and various processes can be easily performed on the edge device.
The value obtained from the light sensor is received by Node-RED and the output is a debug log (standard output). We can set this using the following steps:
- Select the grove light sensor v1_2 node from the palette on the left side of the flow editor and drag and drop it into the workspace to place it:
This node allows the value of the sensor device, which is continuously acquired on the Raspberry Pi via the Grove Base HAT, to be handled as a JSON format message object on Node-RED.
- After placing the grove-light-sensor-v1_2 node, place the inject node and debug nodes and wire them so that the grove-light-sensor-v1_2 node you placed is sandwiched between them:
- Next, check the settings of the grove-light-sensor-v1_2 node. Double-click the node to open the settings panel.
- There is a selection item called Port in the settings panel. A0 is selected by default.
This Port setting is to specify which connector on the Grove Base HAT gets data from the connected module.
- Earlier, we connected the Grove light sensor to the Grove Base HAT. If the connection is made according to the procedure in this tutorial, it should be connected to port A2, so select A2 as the node setting value. If you are connecting to another port, select the port you are connecting to:
- After checking and setting Port on the settings panel, click the Done button in the upper-right corner to close the settings panel.
That's it! Don't forget to click the deploy button.
You should remember how to execute a flow from a inject node, because you learned about this in the previous chapter. Click the switch on the inject node to run the flow. The data for the timing when the switch is clicked is outputted as a log, so please try clicking it a couple of times.
Important Note
Do not forget to display the debug window to show that the value of the acquired data will be the output to the debug window. Node-RED does not automatically show the debug window even if the debug output is activated.
The resulting output in the debug window looks like the following:
You can see that the result was output to the debug window.
Congratulations! With this, we have successfully created a basic flow (application) that handles the value of our first light sensor with Node-RED.
You can also download this flow definition file here: https://github.com/PacktPublishing/-Practical-Node-RED-Programming/blob/master/Chapter05/light-sensor-flows.json.
Making a flow for use case 2 – temperature/humidity sensor
In use case 2, Node-RED can be used to handle the temperature and the humidity obtained from the temperature/humidity sensor as JSON data. The data, which can be handled as JSON data, can be sent to the server side afterward, and various processes can be easily performed on the edge device.
The value obtained from the temperature/humidity sensor is received by Node-RED and is outputted as a debug log (standard output):
- Select the grove temperature humidity sensor sht3x node from the palette on the left side of the flow editor and drag and drop it into the workspace to place it:
This node allows the value of the sensor device, which is continuously acquired on the Raspberry Pi via Grove Base HAT, to be handled as a JSON format message object on Node-RED.
- After placing the grove-temperature-humidity-sensor-sht3x node, place the inject and debug nodes, respectively, and wire them so that the grove-temperature-humidity-sensor-sht3x node you placed is sandwiched between them:
- Next, check the settings of the grove-temperature-humidity-sensor-sht3x node and double-click the node to open the settings panel.
Actually, this node has no values to set (strictly speaking, the name can be set, but the presence or absence of this setting does not affect the operation):
You can see on the settings panel that the port is designated as I2C (not changeable). If you have connected the Grove temperature and humidity sensor to the Grove Base HAT according to the procedure in this document, the module should be correctly connected to the I2C port. If it is connected to a port other than I2C, reconnect it properly.
- After checking Port on the settings panel, click the Done button in the upper-right corner to close the settings panel.
That's it! Don't forget to click the deploy button.
- Click the switch on the inject node to run the flow. The data for the timing when the switch is clicked is outputted as a log, so please try clicking it a couple of times.
Important Note
As noted in the previous section, do not forget to display the debug window to show that the value of the acquired data will be the output to the debug window. Node-RED does not automatically show the debug window even if the debug output is activated.
The resulting output in the debug window looks like the following:
You can see that the result was outputted to the debug window.
Congratulations! With this, we have successfully created a basic flow (application) that handles the value of the second sample, the temperature/humidity sensor, with Node-RED.
You can also download this flow definition file here: https://github.com/PacktPublishing/-Practical-Node-RED-Programming/blob/master/Chapter05/light-sensor-flows.json.
Well done! Now you have learned how to handle the data obtained from the illuminance sensor and temperature and humidity sensor in JSON format on Node-RED.