|
ThingsBoard PE Feature Only ThingsBoard Professional Edition supports Platform Integrations feature. See ThingsBoard PE Installation Options to install ThingsBoard PE. |
OPC UA Integration allows you to stream data from the OPC UA server to ThingsBoard and converts the device payloads to the ThingsBoard format.
See video tutorial below for step-by-step instruction how to setup OPC-UA Integration.
In this tutorial, we will configure the integration between ThingsBoard and OPC-UA to get the Airconditioners data from the OPC UA C++ Demo Server and allow the user to switch on/off any Airconditioner using the Integration downlink feature.
First, we need to create the Uplink Data converter that will be used for receiving the messages from the OPC UA server. The converter should transform the incoming payload into the required message format. The message must contain the deviceName and deviceType. These fields are used to submit the data to the correct device. If a device cannot not be found, a new device will be created. Here is how the payload from the OPC UA integration will look like:
Payload:
{
"temperature": "72.15819999999641"
}
Metadata:
{
"opcUaNode_namespaceIndex": "3",
"opcUaNode_name": "AirConditioner_1",
"integrationName": "OPC-UA Airconditioners",
"opcUaNode_identifier": "AirConditioner_1",
"opcUaNode_fqn": "Objects.BuildingAutomation.AirConditioner_1"
}
We will take the opcUaNode_name metadata value and map it to the deviceName and set the deviceType as airconditioner.
However, you can use another mapping in your specific use cases.
Also, we will retrieve the values of the temperature, humidity and powerConsumption fields and use them as device telemetries.
Go to the Data Converters and create a new uplink Converter using this function:
var data = decodeToJson(payload);
var deviceName = metadata['opcUaNode_name'];
var deviceType = 'airconditioner';
var result = {
deviceName: deviceName,
deviceType: deviceType,
telemetry: {
},
attributes: {
}
};
if (data.temperature) {
result.telemetry.temperature = Number(Number(data.temperature).toFixed(2));
}
if (data.humidity) {
result.telemetry.humidity = Number(Number(data.humidity).toFixed(2));
}
if (data.powerConsumption) {
result.telemetry.powerConsumption = Number(Number(data.powerConsumption).toFixed(2));
}
if (data.state !== undefined) {
result.attributes.state = data.state === '1' ? true : false;
}
function decodeToString(payload) {
return String.fromCharCode.apply(String, payload);
}
function decodeToJson(payload) {
var str = decodeToString(payload);
var data = JSON.parse(str);
return data;
}
return result;
For sending Downlink messages from the Thingsboard to the OPC UA node, we need to define a downlink Converter.
In general, the output from a Downlink converter should have the following structure:
[{
"contentType": "JSON",
"data": "{\"writeValues\":[],\"callMethods\":[{\"objectId\":\"ns=3;s=AirConditioner_1\",\"methodId\":\"ns=3;s=AirConditioner_1.Stop\",\"args\":[]}]}",
"metadata": {}
}]
ns=<namespaceIndex>;<identifiertype>=<identifier>
)Go to Data Converters and create a new downlink Converter using this function:
var data = {
writeValues: [],
callMethods: []
};
if (msgType === 'RPC_CALL_FROM_SERVER_TO_DEVICE') {
if (msg.method === 'setState') {
var targetMethod = msg.params === 'true' ? 'Start' : 'Stop';
var callMethod = {
objectId: 'ns=3;s=' + metadata['deviceName'],
methodId: 'ns=3;s=' +metadata['deviceName']+'.'+targetMethod,
args: []
};
data.callMethods.push(callMethod);
}
}
var result = {
contentType: "JSON",
data: JSON.stringify(data),
metadata: {}
};
return result;
This converter will process the RPC command to the device using the method setState and a boolean params value to call the ‘Start’ or ‘Stop’ method of the Airconditioner.
Destination node is detected using the deviceName field of the incoming message metadata.
Next, we will create Integration with OPC UA server inside the ThingsBoard. Open the Integrations section and add a new Integration with a type OPC-UA
Objects\.BuildingAutomation\.AirConditioner_\d+$
(regular expression used to match scanned OPC UA Node FQNs/IDs to device name.)To demonstrate OPC-UA Integration and Rule Engine capabilities, we will create a separate Rule Chain to process the uplink and downlink messages related to the OPC-UA Integration.
Let´s create the Airconditioners Rule Chain.
+
button at the bottom right corner of the Rule Chains page and select the Import rule chain.
To visualize the Airconditioners data and test RPC commands, we will create the Airconditioners dashboard.
+
button at the bottom right corner of the Dashboards page and select Import dashboard.To verify our integration,
Getting started guides - These guides provide quick overview of main ThingsBoard features. Designed to be completed in 15-30 minutes.
Installation guides - Learn how to setup ThingsBoard on various available operating systems.
Data visualization - These guides contain instructions how to configure complex ThingsBoard dashboards.
Data processing & actions - Learn how to use ThingsBoard Rule Engine.
IoT Data analytics - Learn how to use rule engine to perform basic analytics tasks.
Hardware samples - Learn how to connect various hardware platforms to ThingsBoard.
Advanced features - Learn about advanced ThingsBoard features.
Contribution and Development - Learn about contribution and development in ThingsBoard.