Skip to content

Node-RED programming

The next step will be to program a communication flow on Node-RED capable of generating artificial data and dump it into the InfluxDB database that you have created.

Remember that to access the Node-RED container through the browser you can use the following address:
http://”IPpublicVM”:1880

Progressively and fulfilling the objectives that we are setting for you, we propose to generate a flow with the following structure:

Debug Block

Although debug blocks are not included in the structure of the flow that you have to program, it is recommended that you use the debug blocks to verify that the modifications you make to the Flow produce the expected effect.

The debug blocks can be connected to the output of any block and will show the information they have received on the right side of the screen, specifically in the debug tab.

Therefore, it is not unreasonable to use a debug block at the output of each block that generates or modifies data. Doing so can allow you to quickly spot which block is not performing the desired operation. Once it has been validated that the operation is as expected, deleting the debug blocks will not have any effect.

Below are several sections dedicated to the different types of blocks you will need. It is intended that the objectives that we set for you at the end of each section serve to verify that you have understood how to work with each type of block. To complete these objectives you will need to combine the blocks that have already been presented, cumulatively, with which you will end up generating the complete flow.

Inject Block

The inject blocks allow the generation of messages in the flow manually, by means of a button (blue square, on the left side of the block) that can be clicked, or periodically. These messages are objects that contain information and store it in a predefined structure. Normally the messages will have an element called payload (which can be accessed through msg.payload) where the data contained in the message is usually stored.

In the configuration of your inject block you can decide what you want to be included in the payload of the emitted object. By default, a timestamp is sent, that is, the time stamp (in Unix Time format) of the moment in which that inject block has been executed.

In this first part of the project, this block replaces the set of blocks in charge of taking data from a sensor, database, weather station or PLC.

Goal

  1. You must verify that you are able to program a Flow in which an inject block generates messages manually and also periodically.

Function Block

Function blocks allow messages to be manipulated and are programmed in the JavaScript language. You can consult the following tutorial to know what the JavaScript syntax is for each case.

https://www.w3schools.com/js/default.asp

The code that you write in the function block, inside the blank space that you can see in the following screenshot, will be executed every time this block receives a message.

You will find information on the operational capabilities offered by this type of block in the following link:

https://nodered.org/docs/user-guide/writing-functions

Goals

  1. Program a constant numeric value to be assigned to the message payload. Verify that this is the case using a debug block.
  2. Program variable data to be generated over time (Some examples may be periodic mathematical functions, counters, or simply random values; the “JS Math” section of the tutorial indicated above may be useful). Verify that it works correctly using a debug block.

The next goal, specified on the next page, is to program the function block to output data in the format accepted by InfluxDB databases.

The following screenshot is an example of a function to prepare the message to be dumped to the database through an “Influx Batch” block. As you can see, for each measurement that you want to write in the data series, which is indicated as measurement, from the database we can include values that correspond to the data that you want to store, and tags, which are indicators that allow you to filter the data afterwards.

For example, in a measurement where data from several temperature sensors is stored, there would be a single temperature column and a column indicating which sensor it is. The data would be organized as in the following table, where the temperature column, which would be the data to be stored, would be a field type column, and the name of the sensor to which that measurement corresponds would be a tag type column.

Temperature Sensor
23 sensor1
21 sensor1
29 sensor2
  1. Program the signal you want to write to the database and see in Grafana (this means that you should no longer modify the function block). Adapt the structure and data of the message so that the Influx block can dump them into the database.

You must provide a screenshot or screenshot of the code that you have programmed in the function block to generate the signal that you will later write to the database. Later on, you will be asked for a screenshot of the data written to the database where you can see that the data written by the Node-RED flow is the same as what you see in the InfluxDB database. That is why it is referred to that the function block is not modified during the rest of this first part of the project.

InfluxDB Batch Block

The InfluxDB block allows you to connect the Node-RED flow to an InfluxDB database. The steps to configure the block are as follows:

1) Click on the pencil to edit the information regarding the database to which the block is going to connect.

2) In the node editing options, you must fill in the host and port fields with the capture information. Note that this IP has been manually assigned to the InfluxDB container in the docker-compose file. Although it would be totally equivalent to use the IP of the virtual machine indicating port 8086, remember that the IP of the virtual machine changes every time you start it (which would imply modifying this parameter in the Flow of Node-RED each time), while the IP of the container will always be the same.

3) Finally, you must fill in the database field with the name that you have given to your database, the one that you have previously created in the InfluxDB container.

Click Update or Add when you're done to save your changes.

Objetive

  1. Generate multiple messages manually using the Inject block. Confirm that the data has been correctly written to the database by following the procedure below:

Repeat the process that was carried out to create the database in the Influx container (click here to return to the instructions for that process). Once inside the Influx container, enter the following commands:

> use <name_of_the_database>

This command is used to indicate that from now on the commands entered will refer to a specific database.

> select * from <name_of_series_of_measures>

This is how we tell influx that we want to display all the columns (*) of (from) a particular series of measurements. As an example we show you a response similar to the one you should receive in the terminal.

Keep in mind that if you have kept the Node-RED flow writing periodically, the list of measures that you will see can be very long.

  1. Generate a signal for a while (2 minutes is enough if you are writing a measure every second, since 120 measures would come out). This signal will be the one you should visualize in Grafana; how to do it is explained in the next section.

You must provide a screenshot of the data written to the InfluxDB database. Remember that at this point you are demonstrating that the synthetic data generated by how you have programmed in the function block (which has been reflected in the last capture that we have asked you for) arrive correctly at the database. As an example, think that if you have generated a sinusoidal signal with an amplitude between -1 and 1, it is very easy to see that it is the same signal in the database.