Guides

Explore ThingsBoard tutorials and guides.
ThingsBoard Professional Edition with White-labeling and Platform Integrations Learn More

Send RPC Request to the Related Device

ThingsBoard allows you to send remote procedure calls RPC from server-side applications to devices and vice versa.
This Tutorial is to show you how to send a remote request call to a Related Device using Rule Engine.

Use case

Let’s assume the following use case:

Note:

Turning the Rotating System to the left or to the right is based on which way is better and faster so that the difference between the direction of the wind and the wind turbine has to be not more than 5 degrees.

Prerequisites

We assume you have completed the following guides and reviewed the articles listed below:

Model definition

The Wind Turbine has two devices installed: Wind Direction Sensor and Rotating System.

Message flow

In this section, we explain the purpose of each node in this tutorial:



Configuring the Rule Chain

The following screenshot shows how the Tutorial of RPC Call Request Rule Chain should look like:

image

Also, you can create the new Rule Chain from scratch. The following section shows you how to create it.

Creating a new Rule Chain (Tutorial of RPC Call Request)

image image

Adding the required nodes

In this tutorial, you will create 8 nodes as it will be explained in the following sections:

Node A: Message Type Switch

image

Node B: Save TimeSeries

image

FieldInput Data
Name Fetch Wind Sensor Telemetry
Direction From
Max relationship level 1
Relationship type Uses
Entity type Device
Latest telemetry true
Source telemetry windDirection
Target telemetry windDirection

image

Node D: Change Orignator
FieldInput Data
Name Create New Telemetry
Originator source Related
Direction To
Max relationship level 1
Relationship type Contains
Entity type Asset

image

Node E: Save TimeSeries

image

Node F: Transform Script

image

 var newMsg = {};
 var value = Math.abs(msg.turbineDirection - metadata.windDirection);
 if ((value < 180 && msg.turbineDirection < metadata.windDirection)||
     (value > 180 && msg.turbineDirection > metadata.windDirection)) {
     newMsg.method = 'spinLeft';
 }
 if ((value <= 180 && msg.turbineDirection > metadata.windDirection)||
     (value >= 180 && msg.turbineDirection < metadata.windDirection)) {
     newMsg.method = 'spinRight';
 }
 if(newMsg.method == 'spinLeft' || 'spinRight'){
     msgType = 'RPC message';
 }
 newMsg.params = Math.round(value * 100) / 100;
 return {msg: newMsg, metadata: metadata, msgType: msgType}; 
Node G: Filter Script
: return msgType == 'RPC message'; 

image

Node H: RPC call request

image


This Rule chain is now ready and you need to save it.



How to verify the Rule Chain

To run the scripts, you need to do the following steps:

image image



Configuring Dashboards

The following screenshot shows how the Wind Turbine Dashboard Rule Chain should look like:

image

Download the attached json file for the dashboard indicated above and import it.

The next Step is to configure the aliases used by the imported dashboard.

image

Click the Edit alias button and enter the input data shown in the following table:

Alias Field Input Data
Wind Turbine Filter type Single entity
Type Asset
Asset Wind Turbine
Wind Direction Sensor Filter type Single entity
Type Device
Device Wind Direction Sensor
Rotating System Filter type Single entity
Type Device
Device Rotating System

The configuration of the dashboard is now completed and you can verify that it works as expected.

Also, you can see:

Please refer to the second link under the See Also section to see how to do this.



See Also

Next steps