mikakaraila / node-red-contrib-opcua

A Node-RED node to communicate OPC UA. Uses node-opcua library.
Other
212 stars 196 forks source link

[OPCUA]Unable to get reflected changes in the node-red debug window for subsequent changes made at tia portal program. #572

Closed shubhamnodered closed 1 year ago

shubhamnodered commented 1 year ago

In my Node-red flow, i am taking data from Siemens PLC through OPC-UA node in node-red. I'm getting data perfectly for some-time(meaning whatever changes i make in Tia portal program is also shows in node-red debug window.[Refer to screenshot -1]).

But after certain time(2-3 minutes), whatever changes i make at Tia portal program does not reflected back in node-red debug window[Refer to screenshot -2]. I don't know why?

Also, OPCUA node status is also changing from "'Active subscribing' to 'keep-alive' to 'active subscribing'..........[Refer to screenshot -3]"

I am using

Those who need my-node-red flow, i have pasted it here.

[
    {
        "id": "b5d2b5d70adc9eec",
        "type": "tab",
        "label": "Flow 4",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "06942aed7746321a",
        "type": "function",
        "z": "b5d2b5d70adc9eec",
        "name": "NodeId Array (Static variables)",
        "func": "// Tested with Prosys static variables\nmsg.topic = \"multiple\";\nmsg.payload = [];\nmsg.payload.push({ nodeId: 'ns=3;s=\\\"SQL\".\"Excel_log_bit\"'});\nmsg.payload.push({ nodeId: 'ns=3;s=\\\"SQL\".\"H_CoExtRunTime\"'});\nmsg.payload.push({ nodeId: 'ns=3;s=\\\"SQL\".\"H_TotDefDrum\"' });\nmsg.payload.push({ nodeId: 'ns=3;s=\\\"SQL\".\"H_TotProdDrum\"'});\nmsg.payload.push({ nodeId: 'ns=3;s=\\\"SQL\".\"H_Act_Length\"'});\nmsg.payload.push({ nodeId: 'ns=3;s=\\\"SQL\".\"AvgSpeed\"' });\nmsg.payload.push({ nodeId: 'ns=3;s=\\\"SQL\".\"H_AWGrm/Mtr\"'});\nmsg.payload.push({ nodeId: 'ns=3;s=\\\"SQL\".\"H_CoExtScrapAct\"'});\nmsg.payload.push({ nodeId: 'ns=3;s=\\\"SQL\".\"H_Defects\"'});\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 490,
        "y": 280,
        "wires": [
            [
                "70152a7ad43e3a4c"
            ]
        ]
    },
    {
        "id": "4842bce434c17963",
        "type": "inject",
        "z": "b5d2b5d70adc9eec",
        "name": "Subscribe multiple",
        "props": [
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "1",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "multiple",
        "x": 210,
        "y": 280,
        "wires": [
            [
                "06942aed7746321a"
            ]
        ]
    },
    {
        "id": "67c900801f738564",
        "type": "function",
        "z": "b5d2b5d70adc9eec",
        "name": "Just value",
        "func": "if (msg.payload && msg.payload.value) {\n  var value = msg.payload.value.value;\n  msg.payload = value;\n  return msg;\n}\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 920,
        "y": 280,
        "wires": [
            [
                "f06aa782df1ad63e"
            ]
        ]
    },
    {
        "id": "a20ea5df904b241e",
        "type": "debug",
        "z": "b5d2b5d70adc9eec",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1510,
        "y": 260,
        "wires": []
    },
    {
        "id": "f06aa782df1ad63e",
        "type": "join",
        "z": "b5d2b5d70adc9eec",
        "name": "",
        "mode": "custom",
        "build": "object",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "accumulate": false,
        "timeout": "",
        "count": "9",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "",
        "reduceFixup": "",
        "x": 1050,
        "y": 280,
        "wires": [
            [
                "cabe61a302bbe43b"
            ]
        ]
    },
    {
        "id": "95dd18ccee2f5d60",
        "type": "function",
        "z": "b5d2b5d70adc9eec",
        "name": "function 20",
        "func": "msg.payload = {\n    \"Excel_log_bit\": msg.payload['ns=3;s=\\\"SQL\".\"Excel_log_bit\"'] == true? 1 : 0,\n    \"H_CoExtRunTime\": msg.payload['ns=3;s=\\\"SQL\".\"H_CoExtRunTime\"'],\n    \"H_TotDefDrum\": msg.payload['ns=3;s=\\\"SQL\".\"H_TotDefDrum\"'],\n    \"H_TotProdDrum\": msg.payload['ns=3;s=\\\"SQL\".\"H_TotProdDrum\"'],\n    \"H_Act_Length\": msg.payload['ns=3;s=\\\"SQL\".\"H_Act_Length\"'],\n    \"AvgSpeed\": msg.payload['ns=3;s=\\\"SQL\".\"AvgSpeed\"'],\n    \"H_AWGrm/Mtr\": msg.payload['ns=3;s=\\\"SQL\".\"H_AWGrm/Mtr\"'],\n    \"H_CoExtScrapAct\": msg.payload['ns=3;s=\\\"SQL\".\"H_CoExtScrapAct\"'],\n    \"H_Defects\": msg.payload['ns=3;s=\\\"SQL\".\"H_Defects\"']\n}\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1330,
        "y": 280,
        "wires": [
            [
                "a20ea5df904b241e"
            ]
        ]
    },
    {
        "id": "c44ead5f3ce4f08e",
        "type": "comment",
        "z": "b5d2b5d70adc9eec",
        "name": "Try-5",
        "info": "",
        "x": 170,
        "y": 240,
        "wires": []
    },
    {
        "id": "70152a7ad43e3a4c",
        "type": "OpcUa-Client",
        "z": "b5d2b5d70adc9eec",
        "endpoint": "3ec188ec73ca536f",
        "action": "subscribe",
        "deadbandtype": "a",
        "deadbandvalue": 1,
        "time": "100",
        "timeUnit": "ms",
        "certificate": "n",
        "localfile": "",
        "localkeyfile": "",
        "securitymode": "None",
        "securitypolicy": "None",
        "folderName4PKI": "",
        "maxChunkCount": "",
        "maxMessageSize": "",
        "receiveBufferSize": "",
        "sendBufferSize": "",
        "name": "",
        "x": 740,
        "y": 280,
        "wires": [
            [
                "67c900801f738564"
            ]
        ]
    },
    {
        "id": "8b05d8891b583116",
        "type": "comment",
        "z": "b5d2b5d70adc9eec",
        "name": "chat-gpt corrected program",
        "info": "what i wrote:\nmsg.payload = {\n    \"Excel_log_bit\": if(msg.payload['ns=3;s=\\\"SQL\".\"Excel_log_bit\"'] == true){\n        return 1;\n        else{\n        return 0;\n    },\n\n    \"H_CoExtRunTime\": msg.payload['ns=3;s=\\\"SQL\".\"H_CoExtRunTime\"'],\n    \"H_TotDefDrum\": msg.payload['ns=3;s=\\\"SQL\".\"H_TotDefDrum\"'],\n    \"H_TotProdDrum\": msg.payload['ns=3;s=\\\"SQL\".\"H_TotProdDrum\"'],\n    \"H_Act_Length\": msg.payload['ns=3;s=\\\"SQL\".\"H_Act_Length\"'],\n    \"AvgSpeed\": msg.payload['ns=3;s=\\\"SQL\".\"AvgSpeed\"'],\n    \"H_AWGrm/Mtr\": msg.payload['ns=3;s=\\\"SQL\".\"H_AWGrm/Mtr\"'],\n    \"H_CoExtScrapAct\": msg.payload['ns=3;s=\\\"SQL\".\"H_CoExtScrapAct\"'],\n    \"H_Defects\": msg.payload['ns=3;s=\\\"SQL\".\"H_Defects\"']\n}\nreturn msg;\n\n\ni was getting error, so i pasted the incorrect program as it is into microsoft bing chat gpt, to which it gave me :\nmsg.payload = {\n    \"Excel_log_bit\": msg.payload['ns=3;s=\\\"SQL\".\"Excel_log_bit\"'] == true? 1 : 0,\n    \"H_CoExtRunTime\": msg.payload['ns=3;s=\\\"SQL\".\"H_CoExtRunTime\"'],\n    \"H_TotDefDrum\": msg.payload['ns=3;s=\\\"SQL\".\"H_TotDefDrum\"'],\n    \"H_TotProdDrum\": msg.payload['ns=3;s=\\\"SQL\".\"H_TotProdDrum\"'],\n    \"H_Act_Length\": msg.payload['ns=3;s=\\\"SQL\".\"H_Act_Length\"'],\n    \"AvgSpeed\": msg.payload['ns=3;s=\\\"SQL\".\"AvgSpeed\"'],\n    \"H_AWGrm/Mtr\": msg.payload['ns=3;s=\\\"SQL\".\"H_AWGrm/Mtr\"'],\n    \"H_CoExtScrapAct\": msg.payload['ns=3;s=\\\"SQL\".\"H_CoExtScrapAct\"'],\n    \"H_Defects\": msg.payload['ns=3;s=\\\"SQL\".\"H_Defects\"']\n}\nreturn msg;\n\nRESULT = It worked\n\n\n\n\n\n\nThen i asked him => correct this program and use if/else statement if required:\nmsg.payload = {\n    \"Excel_log_bit\": if(msg.payload['ns=3;s=\\\"SQL\".\"Excel_log_bit\"'] == true){\n        return 1;\n        else{\n        return 0;\n    },\n\n    \"H_CoExtRunTime\": msg.payload['ns=3;s=\\\"SQL\".\"H_CoExtRunTime\"'],\n    \"H_TotDefDrum\": msg.payload['ns=3;s=\\\"SQL\".\"H_TotDefDrum\"'],\n    \"H_TotProdDrum\": msg.payload['ns=3;s=\\\"SQL\".\"H_TotProdDrum\"'],\n    \"H_Act_Length\": msg.payload['ns=3;s=\\\"SQL\".\"H_Act_Length\"'],\n    \"AvgSpeed\": msg.payload['ns=3;s=\\\"SQL\".\"AvgSpeed\"'],\n    \"H_AWGrm/Mtr\": msg.payload['ns=3;s=\\\"SQL\".\"H_AWGrm/Mtr\"'],\n    \"H_CoExtScrapAct\": msg.payload['ns=3;s=\\\"SQL\".\"H_CoExtScrapAct\"'],\n    \"H_Defects\": msg.payload['ns=3;s=\\\"SQL\".\"H_Defects\"']\n}\n\n\n\nto which it replied:\n\nvar Excel_log_bit_value;\nif (msg.payload['ns=3;s=\\\"SQL\".\"Excel_log_bit\"'] == true) {\n    Excel_log_bit_value = 1;\n} else {\n    Excel_log_bit_value = 0;\n}\n\nmsg.payload = {\n    \"Excel_log_bit\": Excel_log_bit_value,\n    \"H_CoExtRunTime\": msg.payload['ns=3;s=\\\"SQL\".\"H_CoExtRunTime\"'],\n    \"H_TotDefDrum\": msg.payload['ns=3;s=\\\"SQL\".\"H_TotDefDrum\"'],\n    \"H_TotProdDrum\": msg.payload['ns=3;s=\\\"SQL\".\"H_TotProdDrum\"'],\n    \"H_Act_Length\": msg.payload['ns=3;s=\\\"SQL\".\"H_Act_Length\"'],\n    \"AvgSpeed\": msg.payload['ns=3;s=\\\"SQL\".\"AvgSpeed\"'],\n    \"H_AWGrm/Mtr\": msg.payload['ns=3;s=\\\"SQL\".\"H_AWGrm/Mtr\"'],\n    \"H_CoExtScrapAct\": msg.payload['ns=3;s=\\\"SQL\".\"H_CoExtScrapAct\"'],\n    \"H_Defects\": msg.payload['ns=3;s=\\\"SQL\".\"H_Defects\"']\n}\n\n\nResult - It also worked",
        "x": 1310,
        "y": 240,
        "wires": []
    },
    {
        "id": "cabe61a302bbe43b",
        "type": "rbe",
        "z": "b5d2b5d70adc9eec",
        "name": "",
        "func": "rbe",
        "gap": "",
        "start": "",
        "inout": "out",
        "septopics": false,
        "property": "payload",
        "topi": "topic",
        "x": 1190,
        "y": 280,
        "wires": [
            [
                "95dd18ccee2f5d60"
            ]
        ]
    },
    {
        "id": "4d2cdd99499bac90",
        "type": "comment",
        "z": "b5d2b5d70adc9eec",
        "name": "",
        "info": "If you ever get problem with changes not being reflected in debug node for subsequent changing in tia portal, then you should refer to this link.\n\n\ntype in gooogle -> opcua node interval in node red\n\n\nor directlly - https://support.industry.siemens.com/forum/in/en/posts/using-opc-ua-sampling-intervals-with-flow-creator-node-red/263016\n",
        "x": 720,
        "y": 240,
        "wires": []
    },
    {
        "id": "3ec188ec73ca536f",
        "type": "OpcUa-Endpoint",
        "endpoint": "opc.tcp://192.168.0.1:4840",
        "secpol": "None",
        "secmode": "None",
        "none": true,
        "login": false,
        "usercert": false,
        "usercertificate": "",
        "userprivatekey": ""
    }
]

[![Screenshot 1][1]][1] Screenshot (659)

[![Screenshot 2][2]][2] Screenshot (660)

[![Screenshot 3][3]][3] Screenshot (661)

mikakaraila commented 1 year ago

Have you checked timeout parameters and console output? Keepalive should keep subscription alive even there are no changes.

shubhamnodered commented 1 year ago

How and from where to check for timeout parameters and console output.

I don't know @mikakaraila but changes i make in my tia portal program is clearly reflected on "UA Expert client" but in case of node-red changes stops reflecting after 2-3 minutes. So i think its more to do with node-red.

Also i make changes in tia portal program in like interval of 0.5 seconds.

mikakaraila commented 1 year ago

If you have debug on (verbose log) then you should see all paramaters on console:

image

shubhamnodered commented 1 year ago

I have searched in google, in how to do verbose logging debugging in node red but haven't found any. Can you tell me how to do it? Does it have to do with"node-red-debugger" module.If so, there was no option to see debug errors in console.

mikakaraila commented 1 year ago

Look settings.js and logging & level:

// Configure the logging output logging: { // Only console logging is currently supported console: { // Level of logging to be recorded. Options are: // fatal - only those errors which make the application unusable should be recorded // error - record errors which are deemed fatal for a particular request + fatal errors // warn - record problems which are non fatal + errors + fatal errors // info - record information about the general running of the application + warn + error + fatal errors // debug - record information which is more verbose than info + info + warn + error + fatal errors // trace - record very detailed logging + debug + info + warn + error + fatal errors // off - turn off all logging (doesn't affect metrics or audit) level: "debug", // Whether or not to include metric events in the log output metrics: false, // Whether or not to include audit events in the log output audit: false } },

shubhamnodered commented 1 year ago

@mikakaraila First of all, Thanks for being beginner friendly and I've made the changes you've suggested. Screenshot 2023-07-03 182204

Now , i have attested the images here. What i can infer from the console if i am not wrong that earlier OPCUA client node was subscribing to the value as you can see in the screenshot-2. [As changes made in OPCUA server =111 , is also subscribed by OPCUA client node in node-red] Screenshot 2023-07-03 182324

But after certain time, OPCUA client node stops subscribing to the value[refer screenshot-3 where [As changes made in OPCUA server =333 , is failed to subscribed by OPCUA client node in node-red[I've added ? mark]]

Although client status says active subscribing[refer screenshot-3] but changes are not visible in node-red also i pointed with '?' in screenshot.

Screenshot 2023-07-03 182533

mikakaraila commented 1 year ago

Cannot say anything... is your flow making subscription only once at start?

Do not inject it again, once is enough. It oy have multiple subscriptions for the same items it could cause strange things.

mikakaraila commented 1 year ago

Problem is in your flow, you cannot subscribe items again and again. It is enough to inject & create subscription once!

image

mikakaraila commented 1 year ago

Error in the flow! Closed.

shubhamnodered commented 1 year ago

@mikakaraila , I don't understand by your statement --> "Problem is in your flow, you cannot subscribe items again and again. It is enough to inject & create subscription once!"

Without injecting, how can i get values at output?

Changes made at OPCUA server must be injected in node-red [OPCUA client] and for injection to be automatic in production environment, i have to provide some interval in inject node. Am i wrong?

mikakaraila commented 1 year ago

You cannot inject subscription with 5s interval, you can inject only once as flow starts. Siemens TIA will run out of resources as you inject new subscription every 5 seconds.

shubhamnodered commented 1 year ago

Are you saying like this?

image

mikakaraila commented 1 year ago

YES

shubhamnodered commented 1 year ago

But if i do this, then changes will be injected only when the flow is initiated.

But if the value of tags changed in OPCUA server(TIA portal), then it won't be reported here as i have used inject once when the flow start.

Am i wrong?

mikakaraila commented 1 year ago

Subscription works in a way that when monitored items are added then it will automatically got value update from the server as value changes.

Perhaps you should read OPC UA basics first! https://documentation.unified-automation.com/uasdkhp/1.4.1/html/_l2_ua_subscription.html

shubhamnodered commented 1 year ago

I am aware of it sir! when i deploy the flow, first subscription is working good as shown in screenshot-1.

But the problem is when i change the value at OPCUA server(TIA portal), things are not subscribing in in node-red. why?[screenshot-2]

Note: - However, with prosys OPCUA server(your example "OPCUA_Multi_SUB.json" , OPCUA client node is perfectly subscribing in nodered for subsequent changes in OPCUA server). But same subsequent changes is not subscribed from TIA portal program. why?

Screenshot (775)

Screenshot (776)

mikakaraila commented 1 year ago

One error can be that you should inject one by one nodeIds into the subscription. Test first with one variable.

shubhamnodered commented 1 year ago

@mikakaraila, So whats the deal here. With one-variable, things are working with changes. But if i use two variable then unless i make changes in both the variable's value in OPCUA server, i will not be subscribed in node-red. So, i think subscription feature only respond to changes. Right?

Does it mean if i want to create array of all the variable of PLC and read value of PLC using OPCUA subscribe feature, i will not get array at debug window(to send into SQL database) unless i make changes to all variable[as subscription feature only responds to changes]?

If i have 100 variables in PLC and i made changes to 20 variable only at OPC server[PLC], then how can i create array of 100 variable[80 unchanged variable + 20 changed variable] in node-red using subscription feature?

mikakaraila commented 1 year ago

Use function node to loop and send items into subscription.

On Thu, Jul 6, 2023, 12:09 shubhamnodered @.***> wrote:

@mikakaraila https://github.com/mikakaraila, So whats the deal here. With one-variable, things are working with changes. But if i use two variable then unless i make changes in both the variable's value in OPCUA server, i will not be subscribed in node-red. So, i think subscription feature only respond to changes. Right?

Does it mean if i want to create array of all the variable of PLC and read value of PLC using OPCUA subscribe feature, i will not get array at debug window(to send into SQL database) unless i make changes to all variable[as subscription feature only responds to changes]?

If i have 100 variables in PLC and i made changes to 20 variable only at OPC server[PLC], then how can i create array of 100 variable[80 unchanged variable + 20 changed variable] in node-red using subscription feature?

— Reply to this email directly, view it on GitHub https://github.com/mikakaraila/node-red-contrib-opcua/issues/572#issuecomment-1623275279, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABHRYDD5IQGYJOPDLNOBZY3XOZ6EBANCNFSM6AAAAAAZ3MFRHE . You are receiving this because you were mentioned.Message ID: @.***>

shubhamnodered commented 1 year ago

Based on your suggestion, i tried but it didn''t worked


msg.topic = "multiple";
msg.payload = [];

var nodes = ['ns=3;s="SQL"."H_TotDefDrum"', 'ns=3;s="SQL"."H_TotProdDrum"'];
var payload = [];
for (var i = 0; i < nodes.length; i++) {
    payload.push({ "node": nodes[i], "value": msg.payload[i] });
}
msg.payload = payload;
return msg;
mikakaraila commented 1 year ago

Node.send(msg);

Inside loop...

On Thu, Jul 6, 2023, 12:54 shubhamnodered @.***> wrote:

Based on your suggestion, i tried but it didn''t worked

`msg.topic = "multiple"; msg.payload = [];

var nodes = ['ns=3;s="SQL"."H_TotDefDrum"', 'ns=3;s="SQL"."H_TotProdDrum"']; var payload = []; for (var i = 0; i < nodes.length; i++) { payload.push({ "node": nodes[i], "value": msg.payload[i] }); } msg.payload = payload; return msg;`

— Reply to this email directly, view it on GitHub https://github.com/mikakaraila/node-red-contrib-opcua/issues/572#issuecomment-1623379912, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABHRYDB4U5VLKRCUYDDLMJDXO2DMNANCNFSM6AAAAAAZ3MFRHE . You are receiving this because you were mentioned.Message ID: @.***>

mikakaraila commented 1 year ago

Something wrong with msg... I am on vacation and with mobile phone.

Try to learn how to use flow & messages. Look examples OPCUA-TEST-NODES.json

On Thu, Jul 6, 2023, 13:25 shubhamnodered @.***> wrote:

I can't seem to connect to node-red. Are you aware of this?

[image: image] https://user-images.githubusercontent.com/138366751/251422284-ed09c31c-2a28-45cf-9c0b-7e8e68c13c5f.png

— Reply to this email directly, view it on GitHub https://github.com/mikakaraila/node-red-contrib-opcua/issues/572#issuecomment-1623423538, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABHRYDAGU6DURORLKB53AI3XO2HDPANCNFSM6AAAAAAZ3MFRHE . You are receiving this because you were mentioned.Message ID: @.***>

shubhamnodered commented 1 year ago

OK! Enjoy :)

mikakaraila commented 1 year ago

Here example on second tab.

Function node: var items = []; items[0] = "ns=3;i=1000"; items[1] = "ns=3;i=1001"; items[2] = "ns=3;i=1002"; items[3] = "ns=3;i=1003"; var newmsg = {}; for (var i=0; i<items.length; i++) { newmsg.topic = items[i]; node.send(newmsg); }

OPCUA-ISSUE-572.txt