Are you looking to create a Node-RED flow with simulated OPC-UA data? With the help of node-red-contrib-opcua and MQTT, you can create a flow that sends OPC-UA data to a broker. Follow these step-by-step instructions to build the flow and check the data in Grafana.
Instructions
- Access Node-RED through UMHLens / OpenLens and create an empty flow.
- Click on the three lines in the upper right corner and select Manage palette. Click on install and add the following node-red-contrib-opcua. Then click on install.
- Next, drag an inject node, two function nodes, an opcua-client node, and an mqtt-out node into the flow. Connect them as shown in the image below.
- Open the inject node and select string under msg.topic. Write "multiple" in the string field and leave the field at msg.payload empty. Now select inject once after 0.1 seconds then at the bottom and click done.
- Open the first function node and add the code below. Once you have done that click Done.
msg.topic = "multiple"; msg.payload = []; msg.payload.push({nodeId: "ns=2;s=Pressure"}); msg.payload.push({nodeId: "ns=2;s=ConcentrationH2"}); msg.payload.push({nodeId: "ns=2;s=ConcentrationN2"}); msg.payload.push({nodeId: "ns=2;s=ConcentrationNH3"}); msg.payload.push({nodeId: "ns=2;s=Temperature"}); return msg;
- Open the opcua-client node and edit the endpoint by clicking on the pencil to the right of it. Enter your opcua-simulator service name
opc.tcp://united-manufacturing-hub-opcuasimulator-service:46010
and click add. Select SUBSCRIBE under action and click Done.
- Open the second function node and paste the code below. Once this is done click Done.
if (msg.payload && msg.payload.value) { var key = msg.topic.replace("ns=2;s=", '') var value = msg.payload.value.value; var payload =` { "timestamp_ms":` + Date.now() + `, "` + key +`": `+ value + ` }` msg.payload = payload msg.topic = "ia/factoryinsight/testLocation/testMachine/processValue/" + key return msg; }
- Open the mqtt-out node and configure the server with the service name of hivemq as the host (located in UMHLens / OpenLens under Services → name) and the port with: 1883.
- To check if everything works, drag an mqtt-in node and a debug node into your flow and connect them. Open the mqtt-in node and select the configured server. Next, paste "ia/factoryinsight/testLocation/testMachine/processValue/#" into the topic. Click done and deploy on the top right.
- Select debug messages located at the top right and press the inject-node. If everything works correctly, you should see the simulated OPC-UA data.
- Open Grafana and create a new dashboard. When adding new panels, use the
testLocation
andtestMachine
as locations and asset names in the datasource. A final dashboard could look like this: