Skip to main content

OPC UA tutorials

FairCom Edge tutorials detailing ways to interact with an OPC UA simulator

Abstract

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

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>/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 for more information about an error.

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

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

  6. Observe the JSON contents of the field in the Edit Record window by clicking "open Edit" on the "source_payload" field.

  • The table records are displayed as one record per row.

  • If you don't see the column "source_payload" in your visualization, click on the right of any of the presented columns (create_ts or error) and select "Columns". Then, select "source_payload" in the "hidden columns" and move to the "Visible Columns" using the right arrow. Then click "Save" to return to data visualization.

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",
        "transformActions": [
          {
            "inputFields": [
              "source_payload"
            ],
            "transformActionName": "jsonToTableFields",
            "transformParams": {
              "targetDatabaseName": "faircom",
              "targetTableName": "opc_sensor2",
              "mapOfPropertiesToFields": [
                {
                  "propertyPath": "the_answer",
                  "name": "theAnswerField",
                  "type": "VARCHAR",
                  "length": 128
                }
              ]
            },
    
            "outputFields": [
              "theAnswerField"
              ]
          }
        ]
      }
    }
    
  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 for more information about an error.

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 for more information about an error.

View the transformed data that is stored in the opc_table2 table:
  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. Select the Table Records tab.

  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_table2 where theAnswerField > 20;
    3. Observe the response and ensure the action completed successfully.

The table records are displayed as one record per row.

Staying connected to the Data Explorer throughout the entire tutorial could result in error 17799 (file block cleared) if your table was open when you added your transform. Click refresh to visualize your data.

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 for more information about an error.

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

    1. Open the FairCom MQ Explorer (https://localhost:8443//MQExplorer/index.html).

    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_table2".

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

  5. Click Send request (Run Icon).

  6. 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 for more information about an error.