Wiki source code of Connect Devices to ThingsEye
Hide last authors
| author | version | line-number | content |
|---|---|---|---|
![]() |
1.1 | 1 | |
| 2 | |||
![]() |
2.1 | 3 | **Table of Contents:** |
![]() |
1.1 | 4 | |
![]() |
2.1 | 5 | {{toc/}} |
![]() |
1.1 | 6 | |
![]() |
2.1 | 7 | |
![]() |
3.1 | 8 | = 1. Overview = |
| 9 | |||
| |
22.1 | 10 | This instructions will show you how to connect a device that has been registered to the server to the Thingseye platform. |
![]() |
3.1 | 11 | |
| |
22.1 | 12 | |
![]() |
2.1 | 13 | = 2. UDP Procotol ~-~- Directly Connection = |
| 14 | |||
| 15 | == 2.1 UDP Interface == | ||
| 16 | |||
![]() |
1.1 | 17 | * Server Address: server1.thingseye.io |
| |
20.1 | 18 | * Port: 11560 |
![]() |
1.1 | 19 | |
| 20 | The data sent to above UDP interface will not go directly to client's database. | ||
| 21 | |||
| 22 | Please contact ThingsEye team for detail how to forward data to client. | ||
| 23 | |||
| |
21.1 | 24 | **Note:Currently udp connections require port forwarding, please contact the ThingsEye team to inform them of your device's IMEI before connecting.** |
| 25 | |||
![]() |
1.1 | 26 | |
| |
22.1 | 27 | ---- |
![]() |
1.1 | 28 | |
| |
22.1 | 29 | == 2.2 Test with Dragino NB device == |
![]() |
1.1 | 30 | |
| |
22.1 | 31 | === 2.2.1 Configure NB-IoT End Node === |
![]() |
1.1 | 32 | |
![]() |
4.1 | 33 | Device here is **[[S31-NB>>https://www.dragino.com/products/temperature-humidity-sensor/item/288-s31-nb-s31b-nb.html]]** : and have been configure below |
| 34 | |||
| |
19.1 | 35 | * Set to use ThingsEye UDP server: **AT+SERVADDR=server1.thingseye.io,11560** |
![]() |
8.1 | 36 | * Use UDP Uplink & Json protocol:** AT+PRO=2,5** |
![]() |
4.1 | 37 | * Equip with a NB-IoT SIM Card to access to NB-IoT Network. |
| 38 | |||
| 39 | The S31-NB's cellular module has the IMEI: **863663062789483** | ||
| 40 | |||
| |
22.1 | 41 | === 2.2.2 Check Uplink Data === |
![]() |
4.1 | 42 | |
| |
22.1 | 43 | Turn on debug mod |
| 44 | |||
| 45 | [[image:1758762334133-608.png||height="494" width="1003"]] | ||
| 46 | |||
| 47 | ---- | ||
| 48 | |||
![]() |
4.1 | 49 | Re-activate the S31-NB, and we can see it in the debug window: |
| 50 | |||
| |
22.1 | 51 | [[image:1758762371290-726.png||height="528" width="1005"]] |
![]() |
4.1 | 52 | |
| |
22.1 | 53 | [[image:1728378218744-800.png||_mstalt="295269" height="431" width="1003"]] |
| 54 | |||
| 55 | ---- | ||
| 56 | |||
| |
23.1 | 57 | === 2.2.4 Auto-Create Device === |
![]() |
4.1 | 58 | |
| 59 | The default **Tenant UDP Server** has already been configured to decode the Dragino -NB / -CB NB-IoT node. So once each end node sends a data to server. Tenant will auto create the device in the server. | ||
| 60 | |||
| |
22.1 | 61 | [[image:1728378968101-683.png||_mstalt="297154" height="210" width="1005"]] |
![]() |
4.1 | 62 | |
| |
22.1 | 63 | [[image:1728379050044-764.png||_mstalt="295282" height="326" width="1009"]] |
![]() |
4.1 | 64 | |
| 65 | |||
| |
23.1 | 66 | ---- |
![]() |
4.1 | 67 | |
![]() |
10.1 | 68 | = 3. Connect to The Things Stack = |
![]() |
8.1 | 69 | |
![]() |
10.1 | 70 | == 3.1 Network Structure == |
![]() |
8.1 | 71 | |
| |
18.1 | 72 | The network diagram below illustrates the connection between the devices and The Things Stack, as well as how the data can be integrated with the ThingsEye IoT platform. |
![]() |
8.1 | 73 | |
| |
22.1 | 74 | [[image:1732679444539-853.jpg||_mstalt="296933" height="388" width="1452"]] |
| |
18.1 | 75 | |
![]() |
10.1 | 76 | == 3.2 Creat Integration for The Things Stack. == |
![]() |
8.1 | 77 | |
| 78 | (% class="lead" %) | ||
| 79 | Add Integration | ||
| 80 | |||
| |
23.1 | 81 | [[image:1728535775119-971.png||_mstalt="297661" height="423" width="1007"]] |
![]() |
8.1 | 82 | |
| 83 | |||
| 84 | (% class="lead" %) | ||
| 85 | Choose Connection Type | ||
| 86 | |||
| |
23.1 | 87 | [[image:1728535857345-950.png||_mstalt="297154" height="257" width="403"]] |
![]() |
8.1 | 88 | |
| 89 | |||
| 90 | (% class="lead" %) | ||
| 91 | Input Uplink Data Converter Code | ||
| 92 | |||
| |
22.1 | 93 | [[image:1728535941851-388.png||_mstalt="297856" height="466" width="398"]] |
![]() |
8.1 | 94 | |
![]() |
11.1 | 95 | Demo JavaScript Code: [[https:~~/~~/raw.githubusercontent.com/ThingsEye-io/te-platform/refs/heads/main/Data%20Converters/The_Things_Network_MQTT_Uplink_Converter.js >>https://raw.githubusercontent.com/ThingsEye-io/te-platform/refs/heads/main/Data%20Converters/The_Things_Network_MQTT_Uplink_Converter.js]] |
![]() |
8.1 | 96 | |
| 97 | |||
| 98 | (% class="lead" %) | ||
| 99 | Input Downlink Converter | ||
| 100 | |||
| |
22.1 | 101 | [[image:1728536142721-488.png||_mstalt="296127" height="470" width="407"]] |
![]() |
8.1 | 102 | |
![]() |
11.1 | 103 | Example Javascript Code as below: [[https:~~/~~/raw.githubusercontent.com/ThingsEye-io/te-platform/refs/heads/main/Data%20Converters/The_Things_Network_MQTT_Downlink_Converter.js>>https://raw.githubusercontent.com/ThingsEye-io/te-platform/refs/heads/main/Data%20Converters/The_Things_Network_MQTT_Downlink_Converter.js]] |
![]() |
8.1 | 104 | |
| 105 | |||
| 106 | (% class="lead" %) | ||
| 107 | Set up Connection to The Things Network application | ||
| 108 | |||
| |
23.1 | 109 | [[image:1728536305503-380.png||_mstalt="293254" height="431" width="1019"]] |
![]() |
8.1 | 110 | |
| 111 | |||
| 112 | |||
| 113 | (% class="lead" %) | ||
| 114 | Test Connection & Add iIntegration | ||
| 115 | |||
| |
23.1 | 116 | [[image:1728536374214-962.png||_mstalt="295893" height="380" width="1024"]] |
![]() |
8.1 | 117 | |
| 118 | After add , we can see the integration here: | ||
| 119 | |||
| |
23.1 | 120 | [[image:1728536420275-153.png||_mstalt="293748" height="152" width="1026"]] |
![]() |
8.1 | 121 | |
| 122 | |||
![]() |
10.1 | 123 | == 3.3 Test Uplink == |
![]() |
8.1 | 124 | |
| 125 | We can use Simulate Uplink to simulate an uplink in the things stack. Then we should be able to see the message in ThingsEye | ||
| 126 | |||
| |
23.1 | 127 | [[image:1728536524638-768.png||_mstalt="298870" height="473" width="1028"]] |
![]() |
8.1 | 128 | |
| 129 | |||
| |
23.1 | 130 | [[image:1728536541040-814.png||_mstalt="293215" height="424" width="1026"]] |
![]() |
13.1 | 131 | |
| |
23.1 | 132 | |
| |
24.1 | 133 | ---- |
| |
23.1 | 134 | |
| |
15.1 | 135 | = 4.Connect chirpstack to Thingseye by way of MQTT = |
![]() |
13.1 | 136 | |
| |
15.1 | 137 | * **How to connect chirpstack to Thingseye by way of MQTT? The following tutorial will show you** |
![]() |
13.1 | 138 | |
| |
18.1 | 139 | == 4.1 Network Structure == |
![]() |
13.1 | 140 | |
| |
18.1 | 141 | The network diagram below illustrates the connection between the devices andchirpstack, as well as how the data can be integrated with the ThingsEye IoT platform. |
| 142 | |||
| |
22.1 | 143 | [[image:1732679647697-982.jpg||_mstalt="299884" height="399" width="1492"]] |
| |
18.1 | 144 | |
| 145 | == 4.2 Thingseye adds MQTT integration == | ||
| 146 | |||
| |
15.1 | 147 | Go to the Integrations page in the Integrations center section. Click the plus button to start adding a new integration. Select the type "MQTT" integration and click "Next"; |
| 148 | |||
| |
23.1 | 149 | [[image:1758762634175-353.png||height="783" width="1020"]] |
| |
15.1 | 150 | |
| |
18.1 | 151 | == 4.3 Add an uplink and downlink data converter == |
| |
15.1 | 152 | |
| |
23.1 | 153 | In the function decoder field, specify the uplink script to parse and transform the data. |
| |
15.1 | 154 | |
| |
23.1 | 155 | [[image:1758763061703-634.png||height="783" width="1020"]] |
| |
15.1 | 156 | |
| 157 | * **Uplink——JavaScript:** | ||
| 158 | |||
| 159 | var data = decodeToJson(payload); | ||
| 160 | |||
| 161 | var deviceName = data.deviceInfo.deviceName; | ||
| 162 | |||
| 163 | var deviceType = data.applicationName; | ||
| 164 | |||
| 165 | var devEui = data.deviceInfo.devEui | ||
| 166 | |||
| 167 | var label = data.deviceInfo.devEui | ||
| 168 | |||
| 169 | var model = {}; | ||
| 170 | |||
| 171 | var data2 = data.object; | ||
| 172 | |||
| 173 | var flg = data.fPort | ||
| 174 | |||
| 175 | for (var key in data2) { | ||
| 176 | |||
| 177 | ~/~/ 将属性名存入新对象中 | ||
| 178 | |||
| 179 | model[key] = data2[key]; | ||
| 180 | |||
| 181 | } | ||
| 182 | |||
| 183 | ~/~/var obj = {"devid":deviceName} | ||
| 184 | |||
| 185 | var result = { | ||
| 186 | |||
| 187 | deviceName: deviceName, | ||
| 188 | |||
| 189 | deviceType: deviceType, | ||
| 190 | |||
| 191 | telemetry: model, | ||
| 192 | |||
| 193 | groupName: "Case Study", | ||
| 194 | |||
| 195 | ~/~/label:label, | ||
| 196 | |||
| 197 | attributes:{"devEui":devEui, | ||
| 198 | |||
| 199 | ~/~/"timevalue":"test", | ||
| 200 | |||
| 201 | "inactivityTimeout":1260000 | ||
| 202 | |||
| 203 | ~/~/ "High_humidity_alarm":"not set", | ||
| 204 | |||
| 205 | ~/~/ "High_temperature_alarm":"not set", | ||
| 206 | |||
| 207 | ~/~/ "Low_humidity_alarm":"not set", | ||
| 208 | |||
| 209 | ~/~/ "Low_temperature_alarm":"not set", | ||
| 210 | |||
| 211 | ~/~/ "Low_voltage_alarm":"not set" | ||
| 212 | |||
| 213 | ~/~/"customerName": "Civionic Engineering & Consulting (2014) Inc." | ||
| 214 | |||
| 215 | } | ||
| 216 | |||
| 217 | }; | ||
| 218 | |||
| 219 | function decodeToString(payload) { | ||
| 220 | |||
| 221 | return String.fromCharCode.apply(String, payload); | ||
| 222 | |||
| 223 | } | ||
| 224 | |||
| 225 | function decodeToJson(payload) { | ||
| 226 | |||
| 227 | var str = decodeToString(payload); | ||
| 228 | |||
| 229 | var data = JSON.parse(str); | ||
| 230 | |||
| 231 | return data; | ||
| 232 | |||
| 233 | } | ||
| 234 | |||
| 235 | return result; | ||
| 236 | |||
| |
23.1 | 237 | ---- |
| |
15.1 | 238 | |
| |
23.1 | 239 | In the function decoder field, specify the downlink script to parse and transform the data. |
| 240 | |||
| 241 | [[image:1758763019466-819.png||height="787" width="1022"]] | ||
| 242 | |||
| |
15.1 | 243 | * **Dowblink——JavaScript:** |
| 244 | |||
| 245 | ~/~/ Encode downlink data from incoming Rule Engine message | ||
| 246 | |||
| 247 | ~/~/ msg - JSON message payload downlink message json | ||
| 248 | |||
| 249 | ~/~/ msgType - type of message, for ex. 'ATTRIBUTES_UPDATED', 'POST_TELEMETRY_REQUEST', etc. | ||
| 250 | |||
| 251 | ~/~/ metadata - list of key-value pairs with additional data about the message | ||
| 252 | |||
| 253 | ~/~/ integrationMetadata - list of key-value pairs with additional data defined in Integration executing this converter | ||
| 254 | |||
| 255 | ~/~/ /~*~* Encoder ~*~*/ | ||
| 256 | |||
| 257 | ~/~/var data = {"value":99}; | ||
| 258 | |||
| 259 | ~/~/ ~/~/ Process data from incoming message and metadata | ||
| 260 | |||
| 261 | ~/~/ data.tempFreq = msg.temperatureUploadFrequency; | ||
| 262 | |||
| 263 | ~/~/ data.humFreq = msg.humidityUploadFrequency; | ||
| 264 | |||
| 265 | ~/~/ data.devSerialNumber = metadata['ss_serialNumber']; | ||
| 266 | |||
| 267 | ~/~/ ~/~/ Result object with encoded downlink payload | ||
| 268 | |||
| 269 | var result = { | ||
| 270 | |||
| 271 | ~/~/ downlink data content type: JSON, TEXT or BINARY (base64 format) | ||
| 272 | |||
| 273 | contentType: "TEXT", | ||
| 274 | |||
| 275 | ~/~/ downlink data | ||
| 276 | |||
| 277 | data: msg.shared_value ,~/~/JSON.stringify(data), | ||
| 278 | |||
| 279 | ~/~/ Optional metadata object presented in key/value format | ||
| 280 | |||
| 281 | metadata: { | ||
| 282 | |||
| 283 | topic: '/test/down/'+metadata.deviceName | ||
| 284 | |||
| 285 | } | ||
| 286 | |||
| 287 | }; | ||
| 288 | |||
| 289 | return result; | ||
| 290 | |||
| |
23.1 | 291 | ---- |
| 292 | |||
| |
18.1 | 293 | == 4.4 Configure the connection == |
| |
15.1 | 294 | |
| 295 | Generate MQTT certificate integrated on chirpstack | ||
| 296 | |||
| 297 | Chirpstack generates CA certificate, TLS certificate, and TLS key respectively | ||
| 298 | |||
| 299 | They correspond to the CA certificate file, Certificate file, and Private key file on thingseye | ||
| 300 | |||
| |
23.1 | 301 | [[image:图片4.png||_mstalt="5131737" height="504" width="1016"]] |
| |
15.1 | 302 | |
| |
23.1 | 303 | [[image:图片5.png||_mstalt="5131854" height="505" width="1017"]] |
| |
15.1 | 304 | |
| 305 | * Copy the contents of the certificates and paste them into the linked file below, a total of three certificates are required | ||
| 306 | |||
| 307 | **Integrated Certificate File demo Download Address:** | ||
| 308 | |||
| 309 | [[https:~~/~~/github.com/ThingsEye-io/te-platform/tree/main/chirpstack>>https://github.com/ThingsEye-io/te-platform/tree/main/chirpstack]] | ||
| 310 | |||
| 311 | Enter the server address Host: lns1.thingseye.io on the Connection configuration | ||
| 312 | |||
| 313 | Port: 8883 | ||
| 314 | |||
| 315 | Credentials type: PEM | ||
| 316 | |||
| 317 | Upload the certificate and key file | ||
| 318 | |||
| |
24.1 | 319 | [[image:图片6.png||_mstalt="5131971" height="509" width="1026"]] |
| |
15.1 | 320 | |
| |
24.1 | 321 | [[image:图片7.png||_mstalt="5132088" height="509" width="1025"]] |
| |
15.1 | 322 | |
| |
24.1 | 323 | ---- |
| 324 | |||
| |
15.1 | 325 | The default for Topic is: |
| 326 | |||
| 327 | application/**chirpstack application id**/device/+/event/up | ||
| 328 | |||
| |
24.1 | 329 | [[image:图片8.png||_mstalt="5132205" height="508" width="1024"]] |
| |
15.1 | 330 | |
| 331 | |||
| |
16.1 | 332 | (% class="wikigeneratedid" %) |
| |
24.1 | 333 | [[image:屏幕截图 2024-11-25 095805.png||_mstalt="10831028" height="301" width="1024"]] |
| |
16.1 | 334 | |
| |
24.1 | 335 | ---- |
| 336 | |||
| |
18.1 | 337 | == 4.5 Check the connection == |
| |
15.1 | 338 | |
| |
24.1 | 339 | [[image:图片9.png||_mstalt="5132322" height="509" width="1025"]] |
| |
15.1 | 340 | |
| |
24.1 | 341 | [[image:图片10.png||_mstalt="5142449" height="509" width="1026"]] |
| |
15.1 | 342 | |
| |
24.1 | 343 | |
| 344 | ---- | ||
| 345 | |||
| |
15.1 | 346 | = 5. Check Data Uplink Log = |
| 347 | |||
![]() |
13.1 | 348 | User can check the devices log in Intergration. |
| 349 | |||
| |
22.1 | 350 | [[image:1729562069134-315.png||_mstalt="294190" height="587" width="723"]] |
