OPC UA tutorials
In these tutorials, you will interact with the FairCom Edge database server using the FairCom Explorer Web Utility to pull, store, view, and use data from an OPC UA simulator.
Ensure the FairCom server is installed and running.
Confirm server access by running the FairCom API Explorer (see API Explorer to learn about the user interface).
Complete the tutorial requirements before this procedure.
Open a command prompt and find the client and server in
<FairCom Folder>/server/opc/test/open62541_v0.30/<OS version folder>/
.Run the program called server_ctt first in one command prompt window to emulate an OPC UA device and answer OPC requests.
Run the program called client in another command prompt window to connect to server_ctt and increment the defined OPC variables before exiting.
You can run it multiple times to cause the OPC variables to change.
Observe the results:
1 endpoints found
URL of endpoint 0 is opc.tcp://localhost:4840
Browsing nodes in objects folder:
…
Reading the value of node (1, "the.answer"):
the value is: 60
Writing a value of node (1, "the.answer"):
the new value is: 61
Subscription removed
Method call was successful, and 1 returned values available.
Created 'NewObject' with numeric NodeID 442
Created 'NewVariable' with numeric NodeID 443
Leave the OPC server running.
Note
The OPC simulator will answer OPC requests for the following variable:
"targetFieldName": "node1", "opcNamespace": 1, "opcNodeName": "the.answer"
Complete the tutorial requirements before this procedure.
Start the FairCom browser-based tools.
Select and log into the Data Explorer.
Click the API Explorer tab ().
This procedure creates an input named opcDS2
. The opcDS2
input takes OPC UA data and stores it in JSON format in the payload field of records in a table. The table is named opctable2
in the FairCom database.
Select Hub API from the Select API dropdown menu.
Select
"createInput"
from the JSON Actions dropdown menu.Replace the JSON in the API Request editor with the following JSON:
{ "requestId": "19", "authToken": "MyTokenHere", "api": "hub", "apiVersion": "1.0", "action": "createInput", "params": { "inputName": "opcDS2", "serviceName": "opcua", "settings": { "opcServerUrl": "opc.tcp://localhost:4840", "opcDataCollectionIntervalMilliseconds": 10000, "propertyMapList": [ { "propertyPath": "the_answer", "opcNamespace": 1, "opcNodeName": "the.answer" } ] }, "ownerName": "admin", "tableName": "opcTable2", "databaseName": "faircom", "retentionPolicy": "autoPurge", "retentionPeriod": 30, "retentionUnit": "day", "metadata": { } } }
Click Apply defaults to JSON request () to replace the
"authToken"
with a valid token from your session.Click Send request () to issue the JSON-based command.
Observe the response and ensure the action completed successfully.
Note
"errorCode"
with a value of0
indicates success."errorCode"
with a non-zero value indicates a failure, see Errors and contact FairCom with any questions.
Start the FairCom Data Explorer.
In the Server navigation window, right-click faircom to select Connect () from the dropdown menu.
Navigate to and select opctable2 in the Server navigation window through
faircom>admin>Tables>opctable2
.Click the Table Records tab ().
Note
The table records are displayed as one record per row.
Click the
"source_payload"
field for the record you wish to view.Observe the JSON contents of the field in the window that displays.
This procedure creates a transform named transform_opc2
. The transform_opc2
will transform the JSON-based data from opctable2
into fields in a new table called opc_sensor2
in the ctreeSQL database.
Select
"createTransform"
from the JSON Actions dropdown menu.Add the
"debug"
property.Replace the JSON in the API Request editor with the following JSON:
{ "requestId": "18", "authToken": "MyTokenHere", "api": "hub", "apiVersion": "1.0", "action": "createTransform", "debug": "min", "params": { "transformName": "transform_opc2", "transformActions": [ { "inputFields": [ "source_payload" ], "transformActionName": "jsonToTableFields", "transformParams": { "targetDatabaseName": "ctreeSQL", "targetTableName": "opc_sensor2", "mapOfPropertiesToFields": [ { "propertyPath": "the_answer", "name": "theAnswerField", "type": "VARCHAR", "length": 128 } ] } } ] } }
Click Send request ().
Observe the response and ensure the action completed successfully.
Note
"errorCode"
with a value of0
indicates success."errorCode"
with a non-zero value indicates a failure, see Errors and contact FairCom with any questions.
Now that the transform has been created, this procedure shows how you can alter the input to use it so that data added to the opctable2
table will also be transformed into rows in the opc_sensor2
table.
Select
"alterInput"
from the JSON Actions dropdown menu.Replace the JSON in the API Request editor with the following JSON:
{ "authToken": "", "api": "hub", "action": "alterInput", "params": { "inputName": "opcDS2", "transformName": "transform_opc2" } }
Click Send request ().
Observe the response and ensure the action completed successfully.
Note
"errorCode"
with a value of0
indicates success."errorCode"
with a non-zero value indicates a failure, see Errors and contact FairCom with any questions.
opc_sensor2
table in the ctreeSQL database:Navigate to and select ctreeSQL server in the Server navigation window, in the FairCom Data Explorer.
Select Connect from the JSON Actions dropdown menu.
Log into the server.
Select the Table Records tab.
Note
The table records are displayed as one record per row.
Perform a SQL query against the data.
Select the SQL Queries tab.
Enter and run the following SQL query in the code box.
select * from opc_sensor2 where theAnswerField > 20;
Observe the response and ensure the action completed successfully.
Now that the OPC data is flowing into a table as JSON and being transformed into discrete fields in another table, the data can be forwarded out over MQTT.
Select MQ API from the Select API dropdown menu.
Select
"configureTopic"
from the JSON Actions dropdown menu to send the OPC UA data to a topic in MQTT.Replace the JSON in the API Request editor with the following JSON:
{ "authToken": "", "api": "mq", "action": "configureTopic", "params": { "topic": "opcForward", "databaseName": "faircom", "tableName": "opcTable2" } }
Click Send request ().
Observe the response and ensure the action completed successfully.
Note
"errorCode"
with a value of0
indicates success."errorCode"
with a non-zero value indicates a failure, see Errors and contact FairCom with any questions.Subscribe to the MQTT topic to verify that your data was forwarded.
Open the FairCom MQ Explorer.
Select MQTT Management from the Control menu ().
Click Manage Subscriptions window.
() to open theSelect the topic you subscribed to from the Topic dropdown menu.
Click
().Close the Manage Subscriptions window.
Observe your message, displayed under Incoming Messages every 10 seconds.
Click
().Delete your subscription from the list.
Select DB API from the Select API dropdown menu.
Select
"getRecordsByIndex"
from the JSON Actions dropdown menu.Change the
"tableName"
property value from"athlete"
to"opc_sensor2"
.Click the Add Property button () for the
"databaseName"
property (in theAction
properties list).Set the
"databaseName"
property value to"ctreeSQL"
.Change the
"indexName"
property value from"id_pk"
to"ts_index"
.Click Send request ().
Observe the records from the table returning as JSON in the API Response.
Note
"errorCode"
with a value of0
indicates success."errorCode"
with a non-zero value indicates a failure, see Errors and contact FairCom with any questions.