Communications in Node-RED
First of all, you will need to access Node-RED. It is not necessary to delete the flow generated for the first part of the project, you can create a new one that will appear as a new tab by clicking on the “+” sign indicated in the following screenshot:

Again, as in the first part of the project, the design of the Node-RED flow will be carried out progressively, fulfilling the objectives that we are setting below. The flow you will generate will have a structure like the following:

The use of debug blocks is recommended to validate each step of the process.
Inject block
Este tipo de bloque ya se introdujo en la primera parte del proyecto, por ello solo indicamos que inicialmente lo configuréis con accionamiento manual, es decir, que no genere pulsos de forma periódica.
Function block before Modbus Flex Getter block
This function block is used to configure the payload of the message received by the Modbus Flex Getter block with specific parameters so that the latter can read the data stored by the PLC with which we want to communicate.
Information regarding the parameters that can be configured and two payload examples can be found in the Modbus Flex Getter block documentation. The second example, which receives the name of Function node code example for multiple inputs, is used for cases in which different values must be read at the same time, such as the case at hand. Remember that we intend to read four statistical parameters calculated on the vibration signal of a machine: RMS, Skewness, Kurtosis and Mean.
To access the documentation of the block and the example just mentioned, click on the Modbus Flex Getter block and on the right side of the Node-RED screen, look for the Help tab, the one with the symbol of a book .
Since you already have the Modbus Flex Getter block selected (the border of a block you have selected will turn orange/red to make it easy to identify), the information that will appear at the bottom right of the Node -RED will already be the one that refers to this block. You should see something similar to what is shown in the screenshot below.

Once you copy the example that has been mentioned, you should have a function block with the following code:

Parameter names will be colored green and parameter values will be colored orange.
It is important to note that each parameter is passed as an unsigned integer. For that to be possible, two integer values are needed for each parameter. Thus, you will receive 8 values that the code below will convert to signed decimal (floating point) numbers.
Objective
- Modify the values of the parameters of the example so that the reading operation of data stored in the PLC is carried out according to the following table. Keep in mind that the initial address will be that of the first data, 0 in this case, and you want to read four parameters.
| FC o function code | Dirección o Adress | Dato |
|---|---|---|
| 3 | 0,1 | RMS |
| 3 | 2,3 | Skewness |
| 3 | 4,5 | Kurtosis |
| 3 | 6,7 | Mean |
Once the function block has been configured, it will also be necessary to configure the Modbus Flex Getter block to validate that both blocks work correctly.
Modbus Flex Getter block
The Modbus Flex Getter block, of the Modbus package, allows receiving data from devices that communicate using the Modbus TCP/IP protocol, such as the PLC that stores the parameters measured by the vibration sensor.
To configure it, double click on the block and click on the pencil icon, indicated in the following screenshot, to add a new Server.

Now, you must enter the information regarding the Type, Host and Port, with the data available regarding the PLC with which we are going to communicate, which are the following:
| Dirección IP | Puerto TCP |
|---|---|
| 147.83.83.29 | 20000 |
To exit, click the red Update or Done button.
At this point, the flow you should have generated consists of 3 blocks: Inject, function and Modbus Flex Getter. If you connect a debug block to the output of the Modbus Flex Getter block, as you can see in the following screenshot, you will be able to validate if you have reached the proposed objective.

Objective
- Run the flow and verify that you are able to receive an object with the format shown in the following screenshot:

Function block after the Modbus Flex Getter block
For the design of the code of this function block, three premises must be taken into account:
1) The received data correspond to the decimal values of the 4 parameters, but they have been encoded as unsigned integers to transmit them in binary. You will be provided with a code that allows you to transform the received data to the original decimal values. 1) When the flow is complete, you must configure (when you reach point 4 of this document) a reading period of the PLC data registers of 1 second. However, the PLC data refresh period is 5 seconds. Therefore, it is expected that sometimes the same data is read several times consecutively. Since this information does not add anything new, it will neither be processed as follows or written to the database. 1) In the event that new data has been read, you will generate a fifth parameter by way of data preprocessing, creating a fifth indicator, which you can name as you wish, which responds to the following formula:
Indicator = RMS^2
Therefore, five parameters will be written to the database at a time.
Objectives
- Copy the following code in the function block and verify in its output that the data received is similar to that of the capture.
let data1 = [msg.payload[0], msg.payload[1]];
let data2 = [msg.payload[2], msg.payload[3]];
let data3 = [msg.payload[4], msg.payload[5]];
let data4 = [msg.payload[6], msg.payload[7]];
var bytes1 = new Uint16Array(data1);
var bytes2 = new Uint16Array(data2);
var bytes3 = new Uint16Array(data3);
var bytes4 = new Uint16Array(data4);
let view1 = new DataView(bytes1.buffer);
let view2 = new DataView(bytes2.buffer);
let view3 = new DataView(bytes3.buffer);
let view4 = new DataView(bytes4.buffer);
let param1 = view1.getFloat32(0, true);
let param2 = view2.getFloat32(0, true);
let param3 = view3.getFloat32(0, true);
let param4 = view4.getFloat32(0, true);
msg.payload = [param1, param2, param3, param4];
return msg;

NOTE: The code requested in the following sections must be written after the code provided for Objective 1.
- Complete the following template with a conditional structure that allows repeated data to be discarded without actually calculating the fifth parameter.

You can verify that you have done it correctly by comparing the messages displayed by the debug blocks as in the following screenshot:

-
Add the calculation of the fifth parameter only if the data is not repeated
-
Include the data structure so that it can be written to an InfluxDB database. Create a new data series so as not to mix the data read from the PLC with the synthetic data (you can use as a base the code that you have generated in the first part of the project to write the synthetic data in the InfluxDB database).
As indicated in the following screenshot, you must include the data structure before the line where the return msg is executed:

Influx Batch block
The configuration of the Influx Batch block is the same as that indicated in the guide document of the first part of the project (page XX). At this point you should already have a complete flow like the following:

Objective
- Generate multiple messages manually using the Inject block. Confirm that the data has been written correctly in the database following the procedure indicated in the guide document of the first part of the project (page 23).
In the InfluxDB database, you should see data like the one shown in the following screenshot:
