Skip to content

Commit defc9cf

Browse files
dmytro-landiakashvayka
authored andcommitted
custom integration docs
1 parent 7a4c784 commit defc9cf

File tree

6 files changed

+156
-5
lines changed

6 files changed

+156
-5
lines changed

_data/docs-home.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@ toc:
2020
- title: Device Connectivity Status
2121
path: /docs/user-guide/device-connectivity-status/
2222
- title: Claiming Devices
23-
new: true
2423
path: /docs/user-guide/claiming-devices/
2524
- title: Entity Views
26-
new: true
2725
path: /docs/user-guide/entity-views/
2826
- title: Using RPC capabilities
2927
path: /docs/user-guide/rpc/
@@ -76,6 +74,8 @@ toc:
7674
path: /docs/user-guide/integrations/tcp/
7775
- title: UDP
7876
path: /docs/user-guide/integrations/udp/
77+
- title: Custom
78+
path: /docs/user-guide/integrations/custom/
7979
- title: Entity Groups
8080
path: /docs/user-guide/groups/
8181
pe: true
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
---
2+
layout: docwithnav
3+
title: Custom Integration
4+
description: Custom integration guide
5+
6+
---
7+
8+
{% assign feature = "Platform Integrations" %}{% include templates/pe-feature-banner.md %}
9+
10+
* TOC
11+
{:toc}
12+
13+
### Introduction
14+
15+
Custom integration is **only executed remotely** from the main ThingsBoard instance. It allows to create integration with custom configuration
16+
that will use any transport protocol for communication with your devices.
17+
18+
This guide contains step-by-step instructions on how to create and launch ThingsBoard custom integration.
19+
For example, we will launch custom integration that uses TCP transport protocol to stream data from devices and pushes the converted data to
20+
[cloud.thingsboard.io](https://cloud.thingsboard.io/signup).
21+
22+
Before we start, you can find the full code of custom integration example that we will use in this guide [here](https://github.com/thingsboard/remote-integration-example).
23+
24+
### Prerequisites
25+
26+
We assume you already have a tenant administrator account on your own ThingsBoard PE v2.4.1+ instance or cloud.thingsboard.io.
27+
28+
Let’s assume that we have a sensor which is sending current temperature, humidity and battery level readings respectively in the following format: **“25,40,94”**.
29+
30+
### Uplink and Downlink Converters
31+
32+
Before setting up a custom integration, you need to create an Uplink and a Downlink converters.
33+
34+
#### Uplink Converter
35+
36+
Let's create uplink converter.
37+
38+
![image](/images/user-guide/integrations/remote/custom-converter.gif)
39+
40+
**NOTE**: Although the Debug mode is very useful for development and troubleshooting, leaving it enabled in production mode may tremendously increase the disk space, used by the database, because all the debugging data is stored there.
41+
It is highly recommended to turn the Debug mode off when done debugging.
42+
43+
See the following script that is pasted to the Decoder function section:
44+
45+
```javascript
46+
/** Decoder **/
47+
48+
// decode payload to string
49+
var decodedString = decodeToString(payload);
50+
// remove unnecessary [\"] and split by [,] to get an array
51+
var payloadArray = decodedString.replace(/\"/g, "").split(',');
52+
var result = {
53+
deviceName: "Device A",
54+
deviceType: "type",
55+
telemetry: {
56+
// get each reading from the array and convert the string value to a number
57+
temperature: Number(payloadArray[0]),
58+
humidity: Number(payloadArray[1]),
59+
batteryLevel: Number(payloadArray[2])
60+
},
61+
attributes: {}
62+
};
63+
64+
/** Helper functions **/
65+
66+
function decodeToString(payload) {
67+
return String.fromCharCode.apply(String, payload);
68+
}
69+
70+
function decodeToJson(payload) {
71+
// convert payload to string.
72+
var str = decodeToString(payload);
73+
74+
// parse string to JSON
75+
var data = JSON.parse(str);
76+
return data;
77+
}
78+
79+
return result;
80+
```
81+
82+
The purpose of the decoder function is to parse the incoming data and metadata to a format that ThingsBoard can consume.
83+
**deviceName** and **deviceType** are required, while **attributes** and **telemetry** are optional.
84+
**Attributes** and **telemetry** are flat key-value objects. Nested objects are not supported.
85+
86+
#### Downlink Converter
87+
88+
We will not use the Downlink converter in this guide so there is no need to create one.
89+
In case you have another use case, please refer to the following [instructions](/docs/user-guide/integrations/#downlink-data-converter).
90+
91+
### Custom Integration Setup
92+
93+
Let's create custom integration.
94+
95+
![image](/images/user-guide/integrations/remote/custom-integration.gif)
96+
97+
Notice that **Execute remotely** is enabled automatically when we choose **Custom** type and we enable **Debug mode**.
98+
99+
**Integration class** is used to create an instance of the integration using the Java reflective method.
100+
101+
The **Integration JSON configuration** is the custom configuration that has two fields in our case:
102+
- **port**, which will be used to bind the TCP server-client communication
103+
- **msgGenerationIntervalMs**, the interval between generating the messages
104+
105+
We will get back to this later in this guide.
106+
107+
### Custom Integration Application
108+
109+
#### Download the sample application
110+
111+
Feel free to grab the [code from the ThingsBoard repository](https://github.com/thingsboard/remote-integration-example) and build the project with maven:
112+
113+
```bash
114+
mvn clean install
115+
```
116+
117+
Go ahead and add that maven project to your favorite IDE.
118+
119+
#### Dependencies review
120+
121+
Main dependencies that are used in the project:
122+
123+
```xml
124+
<!-- Api ThingsBoard provides to create custom integration -->
125+
<dependency>
126+
<groupId>org.thingsboard.common.integration</groupId>
127+
<artifactId>remote-integration-api</artifactId>
128+
<version>${thingsboard.version}</version>
129+
</dependency>
130+
<!-- Netty for TCP client-server implementation -->
131+
<dependency>
132+
<groupId>io.netty</groupId>
133+
<artifactId>netty-all</artifactId>
134+
<version>${netty.version}</version>
135+
</dependency>
136+
<!-- Grpc transport between remote integration and ThingsBoard -->
137+
<dependency>
138+
<groupId>io.grpc</groupId>
139+
<artifactId>grpc-netty</artifactId>
140+
<version>${grpc.version}</version>
141+
</dependency>
142+
```
143+
144+
#### Source code review
145+
146+
To be continued...
147+
148+
## Next steps
149+
150+
{% assign currentGuide = "ConnectYourDevice" %}{% include templates/guides-banner.md %}

docs/user-guide/integrations/mqtt.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Please review the integration diagram to learn more.
1616

1717
![image](/images/user-guide/integrations/mqtt-integration.png)
1818

19-
ThingsBoard MQTT Integration acts as an MQTT client. It subscribes to topics and converts the data into telemetry and attribute updates. In case of downlink message, MQTT integration converts it to the device-suitable format and pushs to external MQTT broker.
19+
ThingsBoard MQTT Integration acts as an MQTT client. It subscribes to topics and converts the data into telemetry and attribute updates. In case of downlink message, MQTT integration converts it to the device-suitable format and pushes to external MQTT broker.
2020
Pay attention: MQTT broker should be either co-located with ThingsBoard instance or deployed in the cloud and have a valid DNS name or static IP address. ThingsBoard instance that is running in the cloud can’t connect to the MQTT broker deployed in local area network.
2121

2222
This video is a step-by-step tutorial on setting up of MQTT Integration.
@@ -30,7 +30,7 @@ This video is a step-by-step tutorial on setting up of MQTT Integration.
3030

3131
## MQTT Integration Configuration
3232

33-
Also you may folow this guide, which discloses MQTT Integration to provide devices connection to the Platform and ability to send RPC commands to devices.
33+
Also you may follow this guide, which discloses MQTT Integration to provide devices connection to the Platform and ability to send RPC commands to devices.
3434

3535
### Prerequisites
3636

@@ -39,7 +39,7 @@ In this tutorial, we will use:
3939
- ThingsBoard Professional Edition instance — [cloud.thingsboard.io](https://cloud.thingsboard.io);
4040
- MQTT broker, accessible by ThingsBoard PE instance — broker.hivemq.com (port 1883);
4141
- mosquitto_pub and mosquitto_sub MQTT clients to send and receive messages;
42-
- an andvanced [device simulator](/docs/user-guide/integrations/resources/mqtt-client.py) for RPC simulation example.
42+
- an advanced [device simulator](/docs/user-guide/integrations/resources/mqtt-client.py) for RPC simulation example.
4343

4444
Let's assume that we have a sensor which is sending current temperature readings.
4545
Our sensor device **SN-001** publishes it's temperature readings to **tb/mqtt-integration-tutorial/sensors/SN-001/temperature** and it is subscribed to **tb/mqtt-integration-tutorial/sensors/SN-001/rx** to receive RPC calls.

docs/user-guide/integrations/remote-integrations.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ Explore guides and video tutorials related to specific integrations:
181181
- [OPC-UA](/docs/user-guide/integrations/opc-ua/)
182182
- [TCP](/docs/user-guide/integrations/tcp/)
183183
- [UDP](/docs/user-guide/integrations/udp/)
184+
- [Custom](/docs/user-guide/integrations/custom/)
184185

185186

186187
## Remote integration troubleshooting
Loading
Loading

0 commit comments

Comments
 (0)