|
| 1 | +--- |
| 2 | +layout: docwithnav |
| 3 | +title: TheThingsIndustries Integration |
| 4 | +description: TheThingsIndustries Integration Guide |
| 5 | + |
| 6 | +--- |
| 7 | + |
| 8 | +{% assign feature = "Platform Integrations" %}{% include templates/pe-feature-banner.md %} |
| 9 | + |
| 10 | +* TOC |
| 11 | +{:toc} |
| 12 | + |
| 13 | + |
| 14 | +## Overview |
| 15 | +TheThingsIndustries is LoRaWAN network designed for connecting your devices using LoRaWAN stack. |
| 16 | +After integrating TheThingsIndustries with the ThingsBoard, you can connect, communicate, process and visualize data from devices in the ThingsBoard IoT platform. |
| 17 | + |
| 18 | + |
| 19 | +## TheThingsIndustries setup |
| 20 | + |
| 21 | +##### Register Application |
| 22 | +The first step is to create an **application** in TheThingsIndustries console. Go to console by , open |
| 23 | +**Applications** section, press **add application** button and fill required fields. |
| 24 | + |
| 25 | +- **Application ID** - thingsboard-integration |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +##### Payload Decoder |
| 31 | +Our device submits data in binary format. We have 2 options where to decode this data: |
| 32 | + |
| 33 | +- **TheThingsIndustries decoder** - data will be decoded before entering the ThingsBoard |
| 34 | +- **ThingsBoard converters** - uplink/downlink converters will be used to decode data from binary format into JSON |
| 35 | + |
| 36 | +In this tutorial, we will make an initial transformation into JSON with TTI decoder and then use ThingsBoard converters for correct data processing. |
| 37 | +In real life scenario, it is up to you where to decode/encode data, because it is possible to do this on any side. |
| 38 | + |
| 39 | +After application registered in TTI, go to **Payload formatters**, **Uplink** select decoder function. We will take the first byte as a temperature value from a device |
| 40 | +and transform it into JSON. |
| 41 | + |
| 42 | +Decode Function {% highlight javascript %} |
| 43 | +function Decoder(bytes, port) { |
| 44 | + var decoded = {temperature: bytes[0]}; |
| 45 | + return decoded; |
| 46 | +} |
| 47 | +{% endhighlight %} |
| 48 | + |
| 49 | +Output json: |
| 50 | +{% highlight json %} |
| 51 | +{ |
| 52 | + "temperature": 2 |
| 53 | +} |
| 54 | +{% endhighlight %} |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | +Press **Save payload function** |
| 59 | + |
| 60 | +##### Device Registration in TheThingsIndustries |
| 61 | + |
| 62 | +Next step is a Device creation in the TTI. Open **End devices** page and press **Add end device** |
| 63 | + |
| 64 | +- Device ID - thermostat1. |
| 65 | +- DevEUI - unique device identifier. |
| 66 | + |
| 67 | +- Press **Network layer settings** button. |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | +- Select configuration for your device. |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | +- Press **Application layer settings** button. |
| 76 | + |
| 77 | +Fill the **AppSKey** by generation button. |
| 78 | + |
| 79 | +Press **Add end device** button. |
| 80 | + |
| 81 | + |
| 82 | +## Integration with the ThingsBoard |
| 83 | + |
| 84 | +We need to create Integration on The Things Industries, to do this open **Integrations** - **MQTT** and press **Generate new API key**. |
| 85 | +Copy username and password we will need it later. |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | +Now we can start configuring the ThingsBoard. |
| 90 | + |
| 91 | +##### ThingsBoard Uplink Data Converter |
| 92 | + |
| 93 | +First, we need to create Uplink Data converter that will be used for receiving messaged from the TTI. The converter should transform incoming payload into the required message format. |
| 94 | +Message must contains **deviceName** and **deviceType**. Those fields are used for submitting data to the correct device. If a device was not found then new device will be created. |
| 95 | +Here is how payload from TheThingsIndustries will look like: |
| 96 | +{% highlight json %} |
| 97 | +{ |
| 98 | + "end_device_ids": { |
| 99 | + "device_id": "thermostat1", |
| 100 | + "application_ids": { |
| 101 | + "application_id": "thingsboard-integration" |
| 102 | + }, |
| 103 | + "dev_eui": "ABABABABABABABAA", |
| 104 | + "join_eui": "0000000000000000", |
| 105 | + "dev_addr": "270000BC" |
| 106 | + }, |
| 107 | + "correlation_ids": [ |
| 108 | + "as:up:01EFEBYDTA1X51TDGPKC1EYK6N", |
| 109 | + "gs:conn:01ED482WRPY2BABY4TYZV57RJG", |
| 110 | + "gs:uplink:01ED9B93M49J8P3FQXCGGCYTGX", |
| 111 | + "ns:uplink:01ED9B93MH00CTY41A6KF674E4", |
| 112 | + "rpc:/ttn.lorawan.v3.AppAs/SimulateUplink:01EFEBYDS9BGD5A9VVZ6GSNBAV", |
| 113 | + "rpc:/ttn.lorawan.v3.GsNs/HandleUplink:01ED9B93MH293RE2TR3F1WMHEG" |
| 114 | + ], |
| 115 | + "received_at": "2020-08-11T08:59:45.869225403Z", |
| 116 | + "uplink_message": { |
| 117 | + "session_key_id": "BXGsg614fdmYH7efd+fRvA==", |
| 118 | + "f_port": 2, |
| 119 | + "f_cnt": 23787, |
| 120 | + "frm_payload": "AhJF8HTI3khf", |
| 121 | + "decoded_payload": { |
| 122 | + "temperature": 2 |
| 123 | + }, |
| 124 | + "settings": { |
| 125 | + "data_rate": {} |
| 126 | + }, |
| 127 | + "received_at": "2020-08-11T10:08:31.505981496Z" |
| 128 | + } |
| 129 | +} |
| 130 | +{% endhighlight %} |
| 131 | + |
| 132 | +We will take **device_id** and map it to the **deviceName** and **application_id** map to the **deviceType**. But you can use another mapping in your specific use cases. |
| 133 | +Also, we will take the value of the **temperature** field and use it as a device telemetry. |
| 134 | + |
| 135 | +Go to **Data Converters** and create new **uplink** Converter with this function: {% highlight javascript %} |
| 136 | +var data = decodeToJson(payload); |
| 137 | +var deviceName = data.end_device_ids.device_id; |
| 138 | +var deviceType = data.end_device_ids.application_ids.application_id; |
| 139 | + |
| 140 | +var result = { |
| 141 | + deviceName: deviceName, |
| 142 | + deviceType: deviceType, |
| 143 | + telemetry: { |
| 144 | + temperature: data.uplink_message.decoded_payload.temperature |
| 145 | + } |
| 146 | +}; |
| 147 | + |
| 148 | +function decodeToString(payload) { |
| 149 | + return String.fromCharCode.apply(String, payload); |
| 150 | +} |
| 151 | + |
| 152 | +function decodeToJson(payload) { |
| 153 | + var str = decodeToString(payload); |
| 154 | + var data = JSON.parse(str); |
| 155 | + return data; |
| 156 | +} |
| 157 | + |
| 158 | +return result; |
| 159 | +{% endhighlight %} |
| 160 | + |
| 161 | + |
| 162 | + |
| 163 | + |
| 164 | +##### ThingsBoard Downlink Data Converter |
| 165 | +For sending Downlink messages from the ThingsBoard to the device inside TTI, we need to define downlink Converter. |
| 166 | +In general, output from Downlink converter should have the following structure: |
| 167 | +{% highlight json %} |
| 168 | +{ |
| 169 | + "downlinks": [{ |
| 170 | + "f_port": 2, |
| 171 | + "frm_payload": "vu8=", |
| 172 | + "priority": "NORMAL" |
| 173 | + }] |
| 174 | +} |
| 175 | + |
| 176 | +{% endhighlight %} |
| 177 | + |
| 178 | +- **contentType** - defines how data will be encoded {TEXT \| JSON \| BINARY} |
| 179 | +- **data** - actual data that will be sent to the device in TTI. More details about API can be found in this [TTI API](https://enterprise.thethingsstack.io/integrations/mqtt/){:target="_blank"} |
| 180 | +- **metadata** - in this object you should place correct **devId** (device id) value that will be used to identify target device in TTI |
| 181 | + |
| 182 | +Go to **Data Converters** and create new **downlink** Converter with this function: {% highlight javascript %} |
| 183 | +var data = { |
| 184 | + downlinks: [{ |
| 185 | + f_port: 2, |
| 186 | + confirmed: false, |
| 187 | + frm_payload: btoa(msg.version), |
| 188 | + priority: "NORMAL" |
| 189 | + }] |
| 190 | + }; |
| 191 | + |
| 192 | +var result = { |
| 193 | + contentType: "JSON", |
| 194 | + data: JSON.stringify(data), |
| 195 | + metadata: { |
| 196 | + devId: 'thermostat1' |
| 197 | + } |
| 198 | + |
| 199 | +}; |
| 200 | +return result; |
| 201 | +{% endhighlight %} |
| 202 | + |
| 203 | +This converter will take **version** field from the incoming message and add it is a payload field in the outbound message. Destination device is a **thermostat1** device. |
| 204 | + |
| 205 | + |
| 206 | + |
| 207 | +##### TTI Integration |
| 208 | + |
| 209 | +Next we will create Integration with TheThingsIndustries inside the ThingsBoard. Open **Integrations** section and add new Integration with type |
| 210 | +**TheThingsIndustries** |
| 211 | + |
| 212 | +- **Name**: *TTI Integration* |
| 213 | +- **Type**: *TheThingsIndustries* |
| 214 | +- **Uplink** data converter: *TTI Uplink* |
| 215 | +- **Downlink** data converter: *TTI Downlink* |
| 216 | +- **Region**: *eu1* (region where your application was registered inside TTI) |
| 217 | +- **Username**: *thingsboard-integration@thingsboard* (use ***Username*** from TTI integration) |
| 218 | +- **Password**: use ***Password*** from TTI integration |
| 219 | + |
| 220 | + |
| 221 | + |
| 222 | + |
| 223 | + |
| 224 | +## Validation |
| 225 | + |
| 226 | +##### Validate Uplink Messages |
| 227 | +Lets verify our integration. |
| 228 | + |
| 229 | +When device sends data, we can check it in the ThingsBoard, to do this: |
| 230 | + |
| 231 | +Go to **Device Group** -> **All** -> **thermostat1** - you can see that |
| 232 | + |
| 233 | +- new device was registered in the Thingsboard with name "thermostat1" |
| 234 | +- In the **Latest Telemetry** section you will see that last submitted temperature = 2. |
| 235 | + |
| 236 | + |
| 237 | + |
| 238 | +##### Validate Downlink Messages |
| 239 | +For testing Downlink Messages, we will update our Root Rule Chain to send downlink message when device attribute is changed. |
| 240 | +Open and edit **Root Rule Chain**. Add **Integration Downlink** Action node and connect it with the **Message Type Switch** Node using relation |
| 241 | +**Attributes Updated** |
| 242 | + |
| 243 | + |
| 244 | + |
| 245 | + |
| 246 | + |
| 247 | +Save Changes. |
| 248 | + |
| 249 | +Go to **Device Group** -> **All** -> **thermostat1** -> attributes section. We will add **Shared attribute** with name **version** and |
| 250 | +value **v.0.11** |
| 251 | + |
| 252 | + |
| 253 | + |
| 254 | +By making this step, we triggered downlink message to the device **thermostat1** and this message should contains version field value. |
| 255 | +Open TTI Console, navigate to **thingsboard-integration** application, to the section **Data**. |
| 256 | +And we see that Downlink message was received (It is displayed as bytes **76 2E 30 2E 31 31**). |
| 257 | + |
| 258 | + |
| 259 | + |
| 260 | +## See also |
| 261 | +With this integration you can also configure Downlink converters and trigger required actions using Rule Engine nodes. |
| 262 | + |
| 263 | +- [Integration Overview](/docs/user-guide/integrations/) |
| 264 | +- [Uplink Converters](/docs/user-guide/integrations/#uplink-data-converter) |
| 265 | +- [Downlink Converters](/docs/user-guide/integrations/#downlink-data-converter) |
| 266 | +- [Rule Engine](/docs/user-guide/rule-engine-2-0/re-getting-started/) |
| 267 | + |
| 268 | + |
| 269 | +## Next steps |
| 270 | + |
| 271 | +{% assign currentGuide = "ConnectYourDevice" %}{% include templates/guides-banner.md %} |
0 commit comments