Getting started on data manipulation with Node-RED

In this guide you will learn how to setup Node-RED for a basic light barrier sensor

Prerequisites

Before continuing with this guide, please make sure you have installed the United Manufacturing Hub and have acquired data to process. If you are using simulated data, none of the examples currently apply for the simulated data. You can either read through the examples and use them as guidelines or you can import the template Node-RED flow from the Guide about simulation of MQTT messages.

Introduction

In this guide we will process raw sensor data, as well as setting up supporting information, like new shifts. This is something you can theoretically do however you want, e.g. by writing your own code to connect to the message broker and processing the data. However, we recommend using Node-RED as it is easy to use and very flexible. It uses so-called nodes, which you connect and create workflows with it.

For more basic Information on basic Node-RED, please feel free to try out the First Flow Tutorial from Node-RED.

In this guide we are showing how to create 4 basic Node-RED flows:

  • Adding new planned operator shifts
  • Recording the number of produced pieces with a light barrier
  • Changing the machine state using buttons on a button bar
  • Measuring the distance of an object using an inductive sensor

If you used the recommended installation guide with a light barrier sensor, it is recommended you try out also processing the light barrier data.

We also have a video on the topic, so make sure to take a look at it.

After you went through your setup and have managed to set up, make sure to return to the Get Started Walkthrough.

Example 1: adding new planned operator shifts

In the United Manufacturing Hub a shift is defined as the planned production time and is therefore important for OEE calculations. If no shifts have been added, it is automatically assumed that the machine is not planned to run. Therefore, all stops will be automatically considered as states of “no shift” or “Operator Break” until a shift is added.

nodered_flow_addshift.png

A shift can be added by sending an [addshift](https://learn.umh.app/docs/datamodel/messages/addshift/) message. In Node-RED, this needs to be handled by at least three nodes.

  • Inject node
  • Function node (called add shift in the picture)
  • MQTT Out node

You don’t need to adjust the Inject node as it can be used unmodified.

For the function node, you need to insert the following code to add message payload according to the datamodel.

msg.payload = {
  "timestamp_ms": 1639522810000,
  "timestamp_ms_end": 1639609200000
}
msg.topic = "ia/factoryinsight/dccaachen/docs/addShift"
return msg;

The MQTT node needs to be configured to connect with the MQTT broker (if nothing has been changed from the default United Manufacturing Hub Installation, it will be selected automatically).

Now you can deploy the flow and by clicking on the inject node you will generate the MQTT message. A shift will be added for the customer factoryinsight , the asset location dccaachen and the asset name docs.

The shift will start on 2021-12-14 00:00 and end 2021-12-15 00:00 standard time. If you need to translate the UNIX timestamp in millisecords to a properly readable format, we recommend Currentmillis.com

Example 2: recording the number of produced pieces

Now we will process sensor data instead of sending new data as before. With the light barrier it is possible, for example, to record the number of pieces produced. Also with a more complicated logic, machine states can be detected directly with a light barrier. For the sake of simplicity, this is not explored and applied in our example.

The first two nodes in this exaple will be the same for all other remaining examples as well.

First Node: MQTT-in

️ It can happen, that you need to configure the username for the MQTT broker first. For this, you should select a MQTT node and go into the settings of the MQTT broker. Then add the username ia_nodered without password.

mqtt_in.png

The topic structure is /ia/raw/<transmitterID>/gatewaySerialNumber>/<portNumber>/<IOLinkSensorID>.

As you can see in the image, an example for such a topic is ia/raw/development/000200410332/X02/310-372. This means that an IO-Link gateway with serial number 000200410332 has connected to the sensor 310-372 to the first port X02.

Now all messages coming in (around 20 messages per second and sensor) will start a Node-RED message flow.

Second Node: JSON

json.png

The payload of incoming messages will be in a JSON format. Therefore we need to add a JSON node to interpret the data correctly.

Third Node: function

function.png

The third node function formats the incoming data by extracting information from the JSON (timestamp and relevant data point) and arranging it in a usable way for Node-RED (parsing). The code for this node looks like this:

msg.timestamp=msg.payload.timestamp_ms
msg.payload=msg.payload.Distance;
return msg;

Fourth Node: trigger

trigger.png

The trigger allows us, in this example in the case of the light barrier, to sort out distances that are irrelevant for our evaluation, i.e. greater than 15 cm. To do this, you need to enter a 15 to the “Threshhold” field.

Fifth Node: function

function_light_barrier.png

In our example, we want to count the number of produced parts. As a trolley on a rail is responsible for the transport into a container after the production of the part and moves towards the light barrier, the number of produced parts shall be increased by one as soon as the distance between the trolley and the light barries is smaller than 15. To do this, we need a function with the following code:

msg.payload = {
  "timestamp_ms": msg.timestamp,
  "count": 1
}
msg.topic = "ia/factoryinsight/dccaachen/docs/count"
return msg;

Sixth node: MQTT-out

mqtt_out.png

To publish messages to a pre-configured topc, the MQTT-Out node is used. The complete Node-RED flow then looks like this:

nodered_flow_light_barrier.png

Example 3: changing the machine state using buttons on a button bar

️ The first two nodes are the same as in example 2

Third node: function

function.png

The third node function formats the incoming data by extracting information from the JSON (timestamp and relevant data point) and arranging it in a usable way for Node-RED (parsing). The code for this node looks like this:

msg.timestamp=msg.payload.timestamp_ms
msg.payload=msg.payload.value_string;
return msg;

Fourth node: filter

filter.png

The filter blocks the values of a sensor until the value is changed. A change of the property of the node is not necessary.

Fifth node: switch

switch.png

The switch node is used to distinguish between the different inputs for the following flow and only lets through the values that come through the previously defined input.

With the button bar, these are the individual buttons. You can see which name is assigned to a single button by the number that appears in the debug window when you press a button. For our example, these are the numbers “0108”, “0104”, “0120” and “0101”.

Sixth node: function

function_button_bar.png

The switch node is followed by a separate function for each button. In our example different states are transmitted. States can be e.g. active, unknown pause, material change, process etc. are defined via numbers. The different states can be found here.

For example, the code for the active function looks like this:

msg.payload = {
  "timestamp_ms": msg.timestamp,
  "state": 10000
}

msg.topic = "ia/factoryinsight/dccaachen/docs/state"
return msg;

To reach further machine states, only the adaption of the state number is necessary.

Seventh node: MQTT-out

mqtt_out.png

To publish messages to a pre-configured topic, the MQTT-Out node is used.

The complete Node-RED flow then looks like this:

nodered_flow_button_bar.png

Example 4: Measuring the distance of an object using an inductive sensor

️ The first two nodes are the same as in example 2

Third node: function

function.png

In the third node “function” a timestamp is generated for each capacitive value. The timestamp is stored in the form of a string. This makes it possible, for example, to read out the time at which an item was produced.

The code for this node looks like this:

msg.timestamp=msg.payload.timestamp_ms
msg.payload=msg.payload["Process value"]
return msg;

Fourth node: function

function_inductive_sensor.png

In our example, the following function formats the incoming data from MQTT-in by extracting information from the JSON (timestamp and relevant data point) and arranging it in a usable way for Node-RED (parsing). To do this, we need a function with the following code:

msg.payload = {
    "timestamp_ms": msg.timestamp, 
    "process_value": msg.payload
}
msg.topic = "ia/factoryinsight/dccaachen/docs/processValue"
return msg;

The process value name process_value can be chosen freely.

Fifth node: MQTT-Out

mqtt_out.png

To publish messages to a pre-configued topic, the MQTT-out node is used.

The complete Node-RED flow then looks like this:

nodered_flow_inductive_sensor.png

What now?

Return to the Getting Started page to make sure

Last modified February 17, 2023: update (#208) (ea731fc)