Skip to content

Node-RED Flow Examples

How to read a digital input?

If we want to read a digital input we can start by placing the eMOD node with digital inputs that you are going to use in the middle of the Node-RED dashboard. Currently, the available eMOD nodes with digital inputs are: 10DI and 5DI+2PR. In this example we are going to use the 10DI node.

Then, it is necessary to add an inject node to be able to manually trigger a flow.

Finally, a debug node is added to see the payloads obtained from the messages. The flow obtained is the one shown in the image below.

Exampleflow1

For more information regarding this module's functions, please check here.

Click here if you want to be able to get the flow code.

How to activate and deactivate a relay?

If we want to work with a relay we can start by placing the eMOD node with relays that you are going to use in the middle of the Node-RED dashboard. Currently, the available eMOD nodes with relays are: 8SR, 5DI+2PR and 7AI+2PR. In this example we are going to use the 8SR node.

First, an inject node must be added.

Finally, a debug node is added to see the payloads obtained from the messages. The flow obtained is the one shown in the image below.

Exampleflow2

For more information regarding this module's functions, please check here.

Click here if you want to be able to get the flow code.

How to configure Modbus to read one or multiple variables?

To work with the Modbus protocol in this example, we are going to use the SEM Three device as an input device.

First of all, the setup must be prepared properly and the connections between the eManager and the SEM Three device must be verified. To do so:

  • Check the power supply of the SEM Three device and verify that the ON led is in green.
  • Check the connections between the eManager and the SEM Three. The A+, B- and GND outputs of the SEM Three must be connected to the modbus input of our eManager.

Example 1: Reading a single variable

To read a single variable we are going to use:

  • An inject node with the timestamp
  • A function node with the following code:

    msg.payload = {
        'unitid': 72,
        'fc': 4,
        'address': 2,
        'quantity': 2
    };
    return msg;
    

    In this case we are communicating with the SEM Three, which has 72 as a default unitid, using function number 4 and reading the register with address 2, which has 2 bytes length. If you want to read other parameters or configuration modes of the SEM Three, please check the Modbus RTU Memory Map on the device datasheet.

  • The modbus flex getter module.

  • The debug node with the message payload.

The flow obtained for this example is shown in the image below.

Flow3

Example 2: Reading multiple variables using a .csv file

To read multiple variables through Modbus protocol we are going to use .csv files to store the coonfigurations (unitid, fc, address and quantity) needed for the different variables. We are going to use the following nodes:

  • An inject node with the timestamp.
  • A file node. In this case we need to store a .csv file named modbus_sem3_map.csv in /home/root/ with the following content:

    unitid,fc,address,quantity
    72,3,1,1
    72,3,7,1
    72,3,8,1
    72,3,9,2
    72,3,12,1
    72,4,2,2
    72,4,4,2
    72,4,6,2
    72,4,8,2
    72,4,10,2
    72,4,12,2
    72,4,38,2
    72,4,40,2
    72,4,60,2
    

  • Two csv nodes where columns and separators are indicated.

  • The modbus flex getter module.
  • The debug node with the message payload.
  • Another file node. We will store the results in a .csv file named modbus_sem3_result.csv in the path /home/root/.

The flow obtained for this example is shown in the image below.

Flow4

Click here if you want to be able to get the flow code for both cases.

How to write a variable using Modbus protocol?

To work with this Modbus example, we are going to use the ACTIO 24 device. We are going to write variables using Modbus to that device.

We will see in the flow created that this example is able to modify the communication baudrate (9600 or 19200) and to control the digital outputs by opening and closing them. To do so, we are going to use:

  • 4 inject nodes: two nodes with different baudrates (9600 and 19200) and the other two for activating and deactivating the digital inputs 1 & 2.
  • 1 function node to write the baudrate into an specific register, using the following code:
    msg.payload = {
        'unitid': 97,
        'fc': 16,
        'address': 0x3001,
        'quantity': 1,
        'value': msg.payload
    };
    return msg;
    
  • 1 function node to activate and deactivate the digital inputs, using the following code:

    msg.payload = {
        'unitid': 97,
        'fc': 16,
        'address': 0x1000,
        'quantity': 1,
        'value': msg.payload
    };
    return msg;
    

  • The modbus flex-write node.

  • The debug node to see the message payload.

The flow obtained for this example is shown in the image below.

Flow6

Click here if you want to be able to get the flow code.

How to read an analog input?

If we want to read an analog input we can start by placing the eMOD node with analog inputs that you are going to use in the middle of the Node-RED dashboard. Currently, the available eMOD nodes with analog inputs are: 12AI and 7AI+2PR. In this example we are going to use the 12AI node.

To do so, we need to use the following modules:

  • The 12 AI module.
  • A switch node to be able to see all the inputs at once.
  • A function node with the following code:
    msg.payload = msg.payload.voltage.value;
    return msg;
    
  • A slider node.

Note

To be able to use the slider node it is necessary to have installed the dashboard package.

Note

In order to read the input's current, change the switch node configuration Property field for payload.current.index

The flow obtained for this example is shown in the image below.

Flow5

For more information regarding this module's functions, please check here.

Click here in order to get the flow code.

How to write files with atomicity?

If we want to write a file with the certainty that the information will be written correctly, we have to do it with atomicity.

For more information on how to write to files in an eManager, read the following article.

In this example we are going to write some data in a temporary file, and once it is written, we are going to write a definitive file using the atomic command mv and make a sync to ensure the file has been written properly.

To do so, we need to use the following modules:

  • An inject node.
  • A function node that produces the data to be stored in file.
  • A write file node
  • An exec node, which allows you to take any existing system command and run it from Node-RED and incorporate the results in your flow. In this example, this node uses the atomic command mv to copy the content of the temporary file to the final file and performs a sync to avoid any data loss.

The flow obtained for this example is shown in the image below.

Flow7

Click here in order to get the flow code.


How to configure an eManager as a Modbus server?

To configure the eManager as a modbus slave, it is needed to configure it as a modbus server.

Important

The modbus flex server node only can be configured using TCP communication. If it is needed to use the eManager as a modbus server using RTU-485 communication, please check here.

To do so, we need to use the following modules:

  • An inject node with the timestamp.
  • The modbus flex server node to store the desired registers. The binding IP and the connection port must be configured.
  • A function node to set the desired values for each registers. This example initializes the holding register address number 100 to value 56. The code used in this node is the following:
    msg.payload = { 
        'value': 56, 
        'register': 'holding', 
        'address': 100 ,
        'disableMsgOutput' : 1
    } ;
    
    return msg;
    

The flow obtained for this example is shown in the image below.

Flow8

Click here in order to get the flow code.