Skip to main content

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.

Requirements:

Complete the tutorial requirements before this procedure.

  1. Open a command prompt and find the client and server in <FairCom Folder>/server/opc/test/open62541_v0.30/<OS version folder>/.

  2. Run the program called server_ctt first in one command prompt window to emulate an OPC UA device and answer OPC requests.

  3. 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.

  4. 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

  5. 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.

  1. Start the FairCom browser-based tools.

  2. Select and log into the Data Explorer.

  3. Click the API Explorer tab (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.

  1. Select Hub API from the Select API dropdown menu.

  2. Select "createInput" from the JSON Actions dropdown menu.

  3. 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": {
        }
      }
    }
  4. Click Apply defaults to JSON request (Apply) to replace the "authToken" with a valid token from your session.

  5. Click Send request (Run Icon) to issue the JSON-based command.

  6. Observe the response and ensure the action completed successfully.

    Note

    "errorCode" with a value of 0 indicates success. "errorCode" with a non-zero value indicates a failure, see Errors and contact FairCom with any questions.

  1. Start the FairCom Data Explorer.

  2. In the Server navigation window, right-click faircom to select Connect (Connect) from the dropdown menu.

  3. Navigate to and select opctable2 in the Server navigation window through faircom>admin>Tables>opctable2.

  4. Click the Table Records tab (Table Records Tab).

    Note

    The table records are displayed as one record per row.

  5. Click the "source_payload" field for the record you wish to view.

  6. 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.

  1. Select "createTransform" from the JSON Actions dropdown menu.

  2. Add the "debug" property.

  3. 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",
        "transformJSON Actions": [
          {
            "inputFields": [
              "source_payload"
            ],
            "transformActionName": "jsonToTableFields",
            "transformParams": {
              "targetDatabaseName": "ctreeSQL",
              "targetTableName": "opc_sensor2",
              "mapOfPropertiesToFields": [
                {
                  "propertyPath": "the_answer",
                  "name": "theAnswerField",
                  "type": "VARCHAR",
                  "length": 128
                }
              ]
            }
          }
        ]
      }
    }
    
  4. Click Send request (Run Icon).

  5. Observe the response and ensure the action completed successfully.

    Note

    "errorCode" with a value of 0 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.

  1. Select "alterInput" from the JSON Actions dropdown menu.

  2. Replace the JSON in the API Request editor with the following JSON:

    {
       "authToken": "",
       "api": "hub",
       "action": "alterInput",
       "params": {
           "inputName": "opcDS2",
           "transformName": "transform_opc2"
       }
    }
  3. Click Send request (Run Icon).

  4. Observe the response and ensure the action completed successfully.

    Note

    "errorCode" with a value of 0 indicates success. "errorCode" with a non-zero value indicates a failure, see Errors and contact FairCom with any questions.

View the transformed data that is stored in the opc_sensor2 table in the ctreeSQL database:
  1. Navigate to and select ctreeSQL server in the Server navigation window, in the FairCom Data Explorer.

  2. Select Connect from the JSON Actions dropdown menu.

  3. Log into the server.

  4. Select the Table Records tab.

    Note

    The table records are displayed as one record per row.

  5. Perform a SQL query against the data.

    1. Select the SQL Queries tab.

    2. Enter and run the following SQL query in the code box.

      select * from opc_sensor2 where theAnswerField > 20;
    3. 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.

  1. Select MQ API from the Select API dropdown menu.

  2. Select "configureTopic" from the JSON Actions dropdown menu to send the OPC UA data to a topic in MQTT.

  3. Replace the JSON in the API Request editor with the following JSON:

    {
      "authToken": "",
      "api": "mq",
      "action": "configureTopic",
      "params": {
        "topic": "opcForward",
        "databaseName": "faircom",
        "tableName": "opcTable2"
      }
    }
  4. Click Send request (Run Icon).

  5. Observe the response and ensure the action completed successfully.

    Note

    "errorCode" with a value of 0 indicates success. "errorCode" with a non-zero value indicates a failure, see Errors and contact FairCom with any questions.

  6. Subscribe to the MQTT topic to verify that your data was forwarded.

    1. Open the FairCom MQ Explorer.

    2. Select MQTT Management from the Control menu (Control menu).

    3. Click Subscribe (Subscribe) to open the Manage Subscriptions window.

    4. Select the topic you subscribed to from the Topic dropdown menu.

    5. Click + Subscribe (+Subscribe).

    6. Close the Manage Subscriptions window.

    7. Observe your message, displayed under Incoming Messages every 10 seconds.

    8. Click Subscribe (Subscribe).

    9. Delete your subscription from the list.

  1. Select DB API from the Select API dropdown menu.

  2. Select "getRecordsByIndex" from the JSON Actions dropdown menu.

  3. Change the "tableName" property value from "athlete" to "opc_sensor2".

  4. Click the Add Property button (Insert) for the "databaseName" property (in the Action properties list).

  5. Set the "databaseName" property value to "ctreeSQL".

  6. Change the "indexName" property value from "id_pk" to "ts_index".

  7. Click Send request (Run Icon).

  8. Observe the records from the table returning as JSON in the API Response.

    Note

    "errorCode" with a value of 0 indicates success. "errorCode" with a non-zero value indicates a failure, see Errors and contact FairCom with any questions.