title
Best-practices & guides for Node-RED OPC-UA Simulator

How to Create a Node-RED Flow with Simulated OPC-UA Data

Learn how to create a Node-RED flow with simulated OPC-UA data using node-red-contrib-opcua and MQTT. Follow these step-by-step instructions to build the flow and check the data in Grafana.

How to Create a Node-RED Flow with Simulated OPC-UA Data
ℹ️

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

  1. Access Node-RED through UMHLens / OpenLens and create an empty flow.
  2. 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.
    image-12-1
    image-13-1
  3. 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.
    image-14-1
  4. 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.
    image-15-1
  5. 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;
    
  6. 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.
    image-16-1
  7. 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;
    }
    
  8. 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.
  9. 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.
  10. 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.
  11. Open Grafana and create a new dashboard. When adding new panels, use the testLocation and testMachine as locations and asset names in the datasource. A final dashboard could look like this:
    image-18-1

Stay up-to-date

Subscribe to the UMH Learning Hub Newsletter to receive the latest updates and gain early access to our blog posts.

Subscribe