Skip to content

Node-RED programming

Industrial Shields Raspberry Pi-based devices can also be programmed using Node-RED, a flow-based programming environment.

This is achieved through the node-red-librpiplc library, a set of Node-RED nodes wrapping the C library librpiplc, which provides access to the inputs/outputs of the device from the Node-RED flows.

Node-RED

You can learn more about Node-RED in its documentation page.

Library Install

Default OS image

The C library is already preinstalled in the default OS images provided by Industrial Shields, but Node-RED is not. The images include an install-node-red.sh script in the home directory of the user, which can be used to install it.

Requirements

Before installing the library, ensure that:

  • You are using a Raspberry Pi–based Industrial Shields device.
  • The device is running Raspberry Pi OS.
  • You have internet access on the PLC.

The Node-RED library is a wrapper of the C one, so both of them must be installed.

  1. Install Node-RED on the device. On the default OS images provided by Industrial Shields, the install-node-red.sh included script can be used:

    ~/install-node-red.sh
    
  2. Install the C library, if it is not already installed, following the instructions of the C/C++ programming section:

    sudo apt install librpiplc librpiplc-dev
    
  3. Install the Node-RED library from the Node-RED editor, using the menu ( > Manage palette > Install) and searching for @industrial-shields/node-red-librpiplc.

Installing the library from the terminal

The library can also be installed with npm, from the Node-RED user directory (typically ~/.node-red):

npm install @industrial-shields/node-red-librpiplc

After the installation, Node-RED must be restarted.

Usage

Configuration

The nodes of the library cannot access the inputs/outputs until the characteristics of the target device are known. They are set in the rpiplc-config configuration node, which is shared by all the nodes of the flows.

When using one of the library nodes, use the PLC Option on its configuration menu to create and select a configuration node for the device model being used. This configuration node can then be used for all the other nodes of the library.

The available options are:

  • Version:
    • RPIPLC_V3 (deprecated)
    • RPIPLC_V4
    • RPIPLC_V6
    • UPSAFEPI_V6
    • GATEBERRY_V9
    • TOUCHBERRY_PI_V1
  • Model:
    • RPIPLC_CPU
    • RPIPLC_19R
    • RPIPLC_21
    • RPIPLC_38AR
    • RPIPLC_38R
    • RPIPLC_42
    • RPIPLC_50RRA
    • RPIPLC_53ARR
    • RPIPLC_54ARA
    • RPIPLC_57AAR
    • RPIPLC_57R
    • RPIPLC_58
    • UPSAFEPI
    • GATEBERRY
    • TOUCHBERRY_PI

Example

  1. Double click on a node from the library to open its menu configuration. In this case, we are using a digital write node.

    Digital write node configuration menu

  2. Click on the button and select the device version and model.

    PLC configuration node
menu

  3. On the top right corner, click on the Update and Done to save the changes.

The version and the model can also be selected while the flows are running. To do so, check the Select through "plc set config" node option of the configuration node, and send a message with the msg.version and msg.model attributes to a PLC set config node.

Only one configuration node

There must be only one rpiplc-config node in the whole Node-RED instance, since several of them interfere with each other when configuring the library.

Basic Examples

Besides the configuration ones, the library provides four nodes to access the inputs/outputs of the device:

  • digital read: gets the value of a digital input.
  • digital write: sets the value of a digital output or a relay.
  • analog read: gets the value of an analog input.
  • analog write: sets the value of an analog output.

Each pin is identified by the name printed on the device (I0.0, Q0.0, A0.5, ...), which is selected in the node itself or passed in the msg.pin attribute. All these nodes return the result of the operation in the msg.rc attribute.

The following examples can be imported into Node-RED using the menu ( > Import > Clipboard). After being imported, the changes can be deployed by pressing the Deploy button located on the top right corner.

Writing a digital output

Flow that sets the digital output Q0.0 to HIGH or LOW when the corresponding inject node is clicked:

inject (HIGH) / inject (LOW) → Digital Write (Q0.0) → debug

Digital write flow example

digital-write.json
[
    {
        "id": "inject-high",
        "type": "inject",
        "z": "dacb7168b9819136",
        "name": "Q0.0 HIGH",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": "",
        "topic": "",
        "payload": "HIGH",
        "payloadType": "str",
        "x": 180,
        "y": 120,
        "wires": [
            [
                "digital-write"
            ]
        ]
    },
    {
        "id": "inject-low",
        "type": "inject",
        "z": "dacb7168b9819136",
        "name": "Q0.0 LOW",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": "",
        "topic": "",
        "payload": "LOW",
        "payloadType": "str",
        "x": 180,
        "y": 180,
        "wires": [
            [
                "digital-write"
            ]
        ]
    },
    {
        "id": "digital-write",
        "type": "rpiplc-digital-write",
        "z": "dacb7168b9819136",
        "rpiplc": "plc-config",
        "pin": "Q0.0",
        "value": "",
        "name": "",
        "x": 420,
        "y": 150,
        "wires": [
            [
                "result"
            ]
        ]
    },
    {
        "id": "result",
        "type": "debug",
        "z": "dacb7168b9819136",
        "name": "digital write result",
        "complete": "true",
        "x": 660,
        "y": 150,
        "wires": []
    },
    {
        "id": "plc-config",
        "type": "rpiplc-config",
        "isCustom": false,
        "version": "RPIPLC_V6",
        "model": "RPIPLC_38AR",
        "name": ""
    }
]

Reading an analog input and writing an analog output

Flow that reads the analog input I0.7 every second and mirrors the value on the analog output A0.5:

inject (every second) → Analog Read (I0.7) → Analog Write (A0.5)

Analog read/write flow example

analog-read-write.json
[
    {
        "id": "every-second",
        "type": "inject",
        "z": "dacb7168b9819136",
        "name": "every second",
        "repeat": "1",
        "once": true,
        "x": 200,
        "y": 340,
        "wires": [
            [
                "analog-read"
            ]
        ]
    },
    {
        "id": "analog-read",
        "type": "rpiplc-analog-read",
        "z": "dacb7168b9819136",
        "rpiplc": "plc-config",
        "pin": "I0.7",
        "name": "",
        "x": 410,
        "y": 340,
        "wires": [
            [
                "analog-write",
                "value"
            ]
        ]
    },
    {
        "id": "analog-write",
        "type": "rpiplc-analog-write",
        "z": "dacb7168b9819136",
        "rpiplc": "plc-config",
        "pin": "A0.5",
        "value": "",
        "name": "",
        "x": 620,
        "y": 340,
        "wires": [
            []
        ]
    },
    {
        "id": "value",
        "type": "debug",
        "z": "dacb7168b9819136",
        "name": "I0.7 value",
        "complete": "payload",
        "x": 620,
        "y": 410,
        "wires": []
    },
    {
        "id": "plc-config",
        "type": "rpiplc-config",
        "isCustom": false,
        "version": "RPIPLC_V6",
        "model": "RPIPLC_38AR",
        "name": ""
    }
]

Notes and Best Practices

  • Ensure the configuration node is set with the correct PLC version and model.
  • Use only one rpiplc-config node, since several of them interfere with each other.
  • You can check the msg.rc attribute returned by the nodes to detect I/O errors.
  • Run only one program controlling the I/O of the device at a time, so the flows do not interfere with other programs. For example, Node-RED and a C program both running at the same time and trying to interact with the IO.

References