Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Practical Node-RED Programming

You're reading from   Practical Node-RED Programming Learn powerful visual programming techniques and best practices for the web and IoT

Arrow left icon
Product type Paperback
Published in Mar 2021
Publisher Packt
ISBN-13 9781800201590
Length 326 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Taiji Hagino Taiji Hagino
Author Profile Icon Taiji Hagino
Taiji Hagino
Arrow right icon
View More author details
Toc

Table of Contents (19) Chapters Close

Preface 1. Section 1: Node-RED Basics
2. Chapter 1: Introducing Node-RED and Flow-Based Programming FREE CHAPTER 3. Chapter 2: Setting Up the Development Environment 4. Chapter 3: Understanding Node-RED Characteristics by Creating Basic Flows 5. Chapter 4: Learning the Major Nodes 6. Section 2: Mastering Node-RED
7. Chapter 5: Implementing Node-RED Locally 8. Chapter 6: Implementing Node-RED in the Cloud 9. Chapter 7: Calling a Web API from Node-RED 10. Chapter 8: Using the Project Feature with Git 11. Section 3: Practical Matters
12. Chapter 9: Creating a ToDo Application with Node-RED 13. Chapter 10: Handling Sensor Data on the Raspberry Pi 14. Chapter 11: Visualize Data by Creating a Server-Side Application in the IBM Cloud 15. Chapter 12: Developing a Chatbot Application Using Slack and IBM Watson 16. Chapter 13: Creating and Publishing Your Own Node on the Node-RED Library 17. Other Books You May Enjoy Appendix: Node-RED User Community

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:

Figure 5.13 – Use case 1 – getting light sensor data

Figure 5.13 – Use case 1 – getting light sensor data

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:

Figure 5.14 – Use case 2 – getting temperature/humidity data

Figure 5.14 – Use case 2 – getting temperature/humidity data

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:

  1. Click the menu at the top right and select Manage palette to open the settings panel:
    Figure 5.15 – Selecting Manage palette

    Figure 5.15 – Selecting Manage palette

  2. 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
  3. After that, you can see the node-red-contrib-grove-base-hat node in the search window. Click the Install button:
    Figure 5.16 – Installing the node-red-contrib-grove-base-hat node

    Figure 5.16 – Installing the node-red-contrib-grove-base-hat node

  4. 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:
Figure 5.17 – A message window to read the node documentation

Figure 5.17 – A message window to read the node documentation

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:

Figure 5.18 – Grove Base HAT nodes on your dashboard

Figure 5.18 – Grove Base HAT nodes on your dashboard

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:

  1. 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:
    Figure 5.19 – grove light sensor v1_2

    Figure 5.19 – grove light sensor v1_2

    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.

  2. 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:
    Figure 5.20 – Placing nodes and wiring them for the light sensor

    Figure 5.20 – Placing nodes and wiring them for the light sensor

  3. Next, check the settings of the grove-light-sensor-v1_2 node. Double-click the node to open the settings panel.
  4. 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.

  5. 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:
    Figure 5.21 – Select A2 as the port if you connected the sensor to A2 of Base HAT

    Figure 5.21 – Select A2 as the port if you connected the sensor to A2 of Base HAT

  6. 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:

Figure 5.22 – Result of the light sensor flow

Figure 5.22 – Result of the light sensor flow

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):

  1. 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:
    Figure 5.23 – grove temperature humidity sensor sht3x

    Figure 5.23 – grove temperature humidity sensor sht3x

    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.

  2. 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:
    Figure 5.24 – Placing the nodes and wiring them for the temperature and humidity sensor

    Figure 5.24 – Placing the nodes and wiring them for the temperature and humidity sensor

  3. 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):

    Figure 5.25 – Already set to the I2C port

    Figure 5.25 – Already set to the I2C port

    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.

  4. 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.

  5. 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:

Figure 5.26 – Result of the temperature/humidity sensor flow

Figure 5.26 – Result of the temperature/humidity sensor flow

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.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image