How to read live nodes values from OPC UA server?

What product/components do you use and which version/fix level are you on?

Comulocity IoT

Is your question related to the free trial, or to a production (customer) instance?

We are IoTEP user. This is our tenat ID: bulut-dik-2021.cumulocity.com

What are you trying to achieve? Please describe it in detail.

Dear support team, I am Yumao Liu, a master student of mechanical engineering at TU Darmstadt. I am working as the research assistant at the Institute PLCM. I need to develop a program which retrieves the nodes values of OPC UA server and gives the data to a html for visualization. I have referred to the two documentations for the development:

First: Documentation for OPC UA

https://cumulocity.com/guides/protocol-integration/opcua/#operations

Especially the part of reading value of a node:


Second: REST API Documentation

https://cumulocity.com/api/10.13.0/#operation/postOperationCollectionResource

And I applied the method described in the part of creating an operation with Node.js.


I wrote a JS program, like this.

var url = 'https://bulut-dik-2021.cumulocity.com/devicecontrol/operations/';
var user = 'shuo.wang@stud.tu-darmstadt.de';
var password = 'dik2022adp..';
var authorizationData = 'Basic ' + btoa(user + ':' + password);
var unirest = require("unirest");
var req = unirest("POST", "https://bulut-dik-2021.cumulocity.com/devicecontrol/operations/")
    .headers({
        "Authorization": authorizationData,
        "Accept": "application/json",
        "Content-Type": "application/json"
    })
    .send(JSON.stringify( {    
    "deviceId" : "84459169",
    "c8y_ua_command_ReadValue": {
     "nodes": ["2"],
     "timestampsToReturn": "Neither"
    },
    "description":"read value"
      } ))
    .end(function (res) {
        console.log(res.raw_body);
    });

And I runed this program on the Node.js terminal.

Do you get any error messages? Please provide a full error message screenshot and log file.

Have you installed all the latest fixes for the products and systems you are using?

I think yes.

I also downloaded the API and tried it with Postman. I also counl not get the right response with the value I wanted. The status is always pending.

In case the low readability of the error report. The response looks like this:

{"creationTime":"2022-10-25T09:10:48.097Z","deviceId":"84459169","deviceName":"OPCUA_Server","self":"https://bulut-dik-2021.cumulocity.com/devicecontrol/operations/463626552","id":"463626552","status":"PENDING","c8y_ua_command_ReadValue":{"nodes":["2"],"timestampsToReturn":"Neither"},"description":"read value"}

I have searched inside the forum and found a Postman Collection which is provided by Macro Stoffel. This is the link. I tried to implement this collection.

I got a different response from the modified default REST API Postman Collection downloaded from your website. The response turned into empty. One thing I want to ask is what should I write here:
image
What does the “s=TunableStatusCode” means here? It is different from nodeID? For example if I want to read the value of node: ns=2, i=2, what should I write there?

Hello Yumao Liu,

i assume the OPC UA Gateway (Agent) is running, registered and connected to Cumulocity, right? Then please check if you have connected to your OPC UA Server. If this is done an the OPC UA Server is shown in Cumulocity you can use the internal ID of the server to generate the operation for the OPC UA Server.

The internal device Id you can get from the URL.
Example:

https://opcua.eu-latest.cumulocity.com/apps/devicemanagement/index.html#/device/15518776/device-info

With this device id you can now create an read operation, like that:
In my case the node id is “ns=2;s=MySwitch”.

{
  "deviceId" : "15518776",
  "c8y_ua_command_ReadValue": {
    "nodes": ["ns=2;s=MySwitch"],
    "timestampsToReturn" : "Both"
  },
  "description":"Read Value"
} 

You have to know that the operation are processed asynchronous, the result will be written to the operation in a later stage when the OPC UA gateway has read the value of the OPC UA Server. This means you have to use following API and check the status of the operation:

{{url}}/devicecontrol/operations/{{operationId}}

The operation id you get from the response of the first http POST.

If the status is “SUCCESSFUL” you can get the result from the operation in that way:

{
    "creationTime": "2022-10-27T07:00:37.623Z",
    "deviceId": "15518776",
    "self": "https://t10979174.eu-latest.cumulocity.com/devicecontrol/operations/15766306",
    "id": "15766306",
    "status": "SUCCESSFUL",
    "c8y_ua_command_ReadValue": {
        "nodes": [
            "ns=2;s=MySwitch"
        ],
        "timestampsToReturn": "Both"
    },
    "c8y_Command": {
        "result": "{\n  \"results\": {\n    \"ns=2;s=MySwitch\": {\n      \"13\": {\n        \"value\": {\n          \"value\": false\n        },\n        \"statusCode\": 0,\n        \"sourceTimestamp\": 133113268269210000,\n        \"serverTimestamp\": 133113276394480000\n      }\n    }\n  }\n}",
        "syntax": null,
        "text": null
    },
    "description": "Read Value"
}
1 Like

Hi Alexander Pester, I sincerely thank you for helping me solve this problem! In the past, I just created the operation and got the operation ID, but I didn’t use the GET method to get the result of the operation with corresponding operation ID, so I couldn’t read the node value I wanted. Now I can read the node value I want to read, and the readed result is as follows.

{
    "creationTime": "2022-10-27T15:33:42.258Z",
    "deviceId": "129075005",
    "failureReason": "GOOD (0x00000000) \"The operation succeeded.\"",
    "self": "https://bulut-dik-2021.cumulocity.com/devicecontrol/operations/464358178",
    "id": "464358178",
    "status": "FAILED",
    "c8y_ua_command_ReadValue": {
        "nodes": [
            "ns=2;i=2"
        ],
        "timestampsToReturn": "Both"
    },
    "c8y_Command": {
        "result": "{\n  \"results\": {\n    \"ns=2;i=2\": {\n      \"13\": {\n        \"value\": {\n          \"value\": \"xy2\"\n        },\n        \"statusCode\": 0,\n        \"sourceTimestamp\": 133113583883325830\n      }\n    }\n  }\n}",
        "syntax": null,
        "text": null
    },
    "description": "Read Value"
}