Skip to main content

Reading and writing values with Node-RED and MQTT

For this tutorial we use Node-RED and MQTT to write values to logiccloud and read processed values.

Requirements

  • Node-RED
  • logiccloud project with CaaS runtime

Create logiccloud Runtime

First of all, we should have an existing logiccloud project with runtime. If not, we need to create a new project, write the program code and publish it as a runtime. After that we can parameterize the MQTT connection.

We use a small project for this, where we read the timestamp, display it in the HMI, duplicate it in the program code, and write it back to Node-RED.

For the POU MyPou we declare the following variables and program code:

Variablen
VAR_INPUT
In1 : REAL;
END_VAR

VAR_OUTPUT
Out1 : REAL;
END_VAR

VAR_IN_OUT
END_VAR

VAR_EXTERNAL
END_VAR

VAR
END_VAR

VAR_TEMP
END_VAR
POU Code
Out1 := In1 * 2;

We also declare the two variables as Access variables so that we can use them in the HMI.

We create a simple HMI with two output elements that we link to the variables.

After that we can build the project and create a runtime.

Finally, we need to create a new MQTT connection and parameterize it.

Parameterize Node-RED

In Node-RED, we create a new flow with the inject and mqtt out and mqtt in and debug nodes.

We set the inject node to repeat, interval every 1 second and enable inject once after 0.1 seconds.

For the MQTT nodes we need to create a new connection. To do this, we select one of the two and add a new MQTT broker with the settings from the logiccloud MQTT connection:

  • Server: Host name
  • Port: Port
  • Connect automatically: True
  • TLS: True
    • TLS configuration: Here you include the certificates that are displayed in the logiccloud connection settings.
  • Client ID: Can be left empty or you assign your own value, for example the Device ID

Under Security you can enter user name and password from the logiccloud settings.

As topic, select the topic from the payload settings in logiccloud for the two variables In and Out.

After you have made all the settings, you can deploy the flow.

Gesamter Node-RED flow
[
{
"id": "28d78d934035030c",
"type": "tab",
"label": "Flow 5",
"disabled": false,
"info": "",
"env": []
},
{
"id": "710cd5bf5bda2ec9",
"type": "inject",
"z": "28d78d934035030c",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "1",
"crontab": "",
"once": true,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 190,
"y": 100,
"wires": [
[
"219d19439e1997ec"
]
]
},
{
"id": "045415b18bbb3690",
"type": "mqtt out",
"z": "28d78d934035030c",
"name": "Topic \"in\"",
"topic": " 6384b486f6beade45b0946f0-MQTT-Node-RED/in",
"qos": "",
"retain": "",
"respTopic": "",
"contentType": "",
"userProps": "",
"correl": "",
"expiry": "",
"broker": "4b234810b153a4b3",
"x": 600,
"y": 120,
"wires": []
},
{
"id": "9263edd981836417",
"type": "mqtt in",
"z": "28d78d934035030c",
"name": "Topic \"out\"",
"topic": " 6384b486f6beade45b0946f0-MQTT-Node-RED/out",
"qos": "2",
"datatype": "auto",
"broker": "4b234810b153a4b3",
"nl": false,
"rap": true,
"rh": 0,
"inputs": 0,
"x": 160,
"y": 180,
"wires": [
[
"d6b36f380580f041"
]
]
},
{
"id": "d6b36f380580f041",
"type": "debug",
"z": "28d78d934035030c",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 450,
"y": 180,
"wires": []
},
{
"id": "871457c106c9554a",
"type": "debug",
"z": "28d78d934035030c",
"name": "",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 750,
"y": 60,
"wires": []
},
{
"id": "219d19439e1997ec",
"type": "template",
"z": "28d78d934035030c",
"name": "",
"field": "payload",
"fieldType": "msg",
"format": "handlebars",
"syntax": "mustache",
"template":"{"value": {{payload}}}",
"output": "str",
"x": 400,
"y": 120,
"wires": [
[
"e9ef763d1b303b51"
]
]
},
{
"id": "e9ef763d1b303b51",
"type": "json",
"z": "28d78d934035030c",
"name": "",
"property": "payload",
"action": "",
"pretty": false,
"x": 570,
"y": 60,
"wires": [
[
"871457c106c9554a",
"045415b18bbb3690"
]
]
},
{
"id": "4b234810b153a4b3",
"type": "mqtt-broker",
"name": "logiccloud Staging",
"broker": "staging.logiccloud.io",
"port": "9883",
"tls": "e7c04b3f6d1a0243",
"clientid": "test-node-red",
"autoConnect": true,
"usetls": true,
"protocolVersion": "4",
"keepalive": "60",
"cleansession": true,
"birthTopic": "",
"birthQos": "0",
"birthRetain": "false",
"birthPayload": "",
"birthMsg": {},
"closeTopic": "",
"closeQos": "0",
"closeRetain": "false",
"closePayload": "",
"closeMsg": {},
"willTopic": "",
"willQos": "0",
"willRetain": "false",
"willPayload": "",
"willMsg": {},
"sessionExpiry": ""
},
{
"id": "e7c04b3f6d1a0243",
"type": "tls-config",
"name": "",
"cert": "",
"key": "",
"ca": "",
"certname": "client.crt",
"keyname": "client.key",
"caname": "ca.crt",
"servername": "",
"verifyservercert": false,
"alpnprotocol": ""
}
]