Skip to content

Sensor data via MQTT processed in Node-RED

Overview

This guide will show you how to access mqtt data of sensors by using Node-RED within Docker.

Prerequisites

Set up MQTT node

Drag and drop an MQTT node from the network section of the toolbox on the left into your flow. Double-click on the mqtt node in the info panel on the right.

Create Container

This will open the configuration for the mqtt node. In this case we will subscribe to the topic balluff/cmtk/master1/iolink/devices/port1/data/fromdevice in order to receive the current sensor data of the IO-Link device connected at port1.

MQTT topic structure

For the master integrated in CMTK itself:

  • balluff/cmtk/master1/iolink/devices/port[1-4]/data/fromdevice for process data. Schema
  • balluff/cmtk/master1/iolink/devices/port[1-4]/units for process data units. Schema
  • balluff/cmtk/master1/iolink/devices/port[1-4]/events for events. Schema

In case of an additional EIP master data is delivered under the following topics:

  • balluff/cmtk/master2/iolink/devices/port[1-4]/data/fromdevice for process data. Schema
  • balluff/cmtk/master2/iolink/devices/port[1-4]/units for process data units. Schema
  • balluff/cmtk/master2/iolink/devices/port[1-4]/events for events. Schema

However, we still need to add an MQTT broker. To do this, click on the pencil icon next to the the text Add new mqtt-broker.

Create Container

The broker is available at the IP-address 172.16.0.2. Enter the address and click on add. This provides read-only access to MQTT without authentication.

Note

This only works for containers which are deployed and running directly on a CMTK device. This interface is not available for external devices.

Create Container

This will return you to the MQTT node where you just need to save the changes by clicking on Done. Now your MQTT node is configured and can be used within your flows.

Create Container

Debug Output

In order to test whether the flow is working as intended, it might be a good idea to use a debug node. Simply drag in a debug-node from the common section of the toolbox and connect the input to the output of your MQTT node. Do not forget to deploy your changes with the Deploy button on the top right.

Create Container

To check the debug output, simply click on the bug icon in the upper right toolbox. The raw messages will appear in a console window if everything is working.

Create Container

Example

The example we have just created may be imported by clicking on the menu in the top right and selecting "import" with the following JSON:

[
    {
        "id": "f6f2187d.f17ca8",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": ""
    },
    {
        "id": "8bb16eb08a113c52",
        "type": "mqtt in",
        "z": "f6f2187d.f17ca8",
        "name": "",
        "topic": "balluff/cmtk/master1/iolink/devices/port1/data/fromdevice",
        "qos": "2",
        "datatype": "auto",
        "broker": "06eea54c85469165",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 170,
        "y": 260,
        "wires": [
            [
                "f0e2a0d565502417"
            ]
        ]
    },
    {
        "id": "f0e2a0d565502417",
        "type": "debug",
        "z": "f6f2187d.f17ca8",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 440,
        "y": 260,
        "wires": []
    },
    {
        "id": "06eea54c85469165",
        "type": "mqtt-broker",
        "name": "Local",
        "broker": "172.16.0.2",
        "port": "1883",
        "clientid": "",
        "autoConnect": true,
        "usetls": false,
        "protocolVersion": "4",
        "keepalive": "60",
        "cleansession": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthPayload": "",
        "birthMsg": {},
        "closeTopic": "",
        "closeQos": "0",
        "closePayload": "",
        "closeMsg": {},
        "willTopic": "",
        "willQos": "0",
        "willPayload": "",
        "willMsg": {},
        "sessionExpiry": ""
    }
]

To debug the MQTT retained messages we created the example below:

[
    {
        "id": "f6f2187d.f17ca8",
        "type": "tab",
        "label": "Influxdb",
        "disabled": false,
        "info": ""
    },
    {
        "id": "f6f4aba3975268fd",
        "type": "mqtt in",
        "z": "f6f2187d.f17ca8",
        "name": "",
        "topic": "#",
        "qos": "0",
        "datatype": "json",
        "broker": "5fbe19bf51b90673",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 230,
        "y": 460,
        "wires": [
            [
                "c5379b21d4fcc319"
            ]
        ]
    },
    {
        "id": "8ad25111e0375486",
        "type": "debug",
        "z": "f6f2187d.f17ca8",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 750,
        "y": 500,
        "wires": []
    },
    {
        "id": "c5379b21d4fcc319",
        "type": "function",
        "z": "f6f2187d.f17ca8",
        "name": "",
        "func": "if (msg.retain==true){\nreturn msg;\n}",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 500,
        "y": 420,
        "wires": [
            [
                "8ad25111e0375486"
            ]
        ]
    },
    {
        "id": "5fbe19bf51b90673",
        "type": "mqtt-broker",
        "name": "",
        "broker": "172.16.0.2",
        "port": "1883",
        "clientid": "",
        "autoConnect": true,
        "usetls": false,
        "protocolVersion": "4",
        "keepalive": "60",
        "cleansession": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthRetain": "false",
        "birthPayload": "",
        "birthMsg": {},
        "closeTopic": "",
        "closeQos": "0",
        "closePayload": "",
        "closeMsg": {},
        "willTopic": "",
        "willQos": "0",
        "willPayload": "",
        "willMsg": {},
        "sessionExpiry": ""
    }
]

Schemes

Process Data Schema

{
  data:{
    isValid: boolean,
    items: {
      pd_name1: number | boolean | string | number[],
      // ...
      pd_nameN: number | boolean | string | number[]
    }
  },
  timestamp: string,
  type: string
}

Event Schema

{
  data:{
    deviceId: number,
    event: 'CONNECTED' | 'DISCONNECTED',
    vendorId: number
  }
  origin: string,
  timestamp: string,
  type: string
}

Unit Schema

{
  data: {
    pd_name1: string,
    // ...
    pd_nameN: string,
  },
  timestamp: string,
  type: string
}