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 | |||
| |
17.1 | 10 | The instructions in this chapter will show you how to connect a device that has been registered to the server to the Thingseye platform. |
![]() |
3.1 | 11 | |
![]() |
2.1 | 12 | = 2. UDP Procotol ~-~- Directly Connection = |
| 13 | |||
| 14 | == 2.1 UDP Interface == | ||
| 15 | |||
![]() |
1.1 | 16 | * Server Address: server1.thingseye.io |
| |
20.1 | 17 | * Port: 11560 |
![]() |
1.1 | 18 | |
| 19 | The data sent to above UDP interface will not go directly to client's database. | ||
| 20 | |||
| 21 | Please contact ThingsEye team for detail how to forward data to client. | ||
| 22 | |||
| |
21.1 | 23 | **Note:Currently udp connections require port forwarding, please contact the ThingsEye team to inform them of your device's IMEI before connecting.** |
| 24 | |||
![]() |
2.1 | 25 | == 2.2 Test UDP Interface via Socket Tool == |
![]() |
1.1 | 26 | |
| 27 | Download the UDP Test Tool from: [[https:~~/~~/sourceforge.net/projects/sockettest/ >>https://sourceforge.net/projects/sockettest/]]. Run this tool, and input the server address and UDP port as below and click send. | ||
| 28 | |||
![]() |
9.1 | 29 | [[image:1728566363151-470.png]] |
![]() |
1.1 | 30 | |
| 31 | In server side, Tenant Administrator can check this in Tenant UDP Server location. | ||
| 32 | |||
| 33 | [[image:1728362834430-749.png||height="510" width="1667"]] | ||
| 34 | |||
| 35 | Click and see the debug info as below: | ||
| 36 | |||
| 37 | [[image:1728363020699-203.png]] | ||
| 38 | |||
| 39 | We can see the message arrives, but it shows ERROR because the message doesn't follow with the UDP Server format. | ||
| 40 | |||
![]() |
5.1 | 41 | == 2.3 Test with Dragino NB device == |
![]() |
1.1 | 42 | |
![]() |
4.1 | 43 | === 2.3.1 Configure NB-IoT End Node === |
| 44 | |||
| 45 | 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 | ||
| 46 | |||
| |
19.1 | 47 | * Set to use ThingsEye UDP server: **AT+SERVADDR=server1.thingseye.io,11560** |
![]() |
8.1 | 48 | * Use UDP Uplink & Json protocol:** AT+PRO=2,5** |
![]() |
4.1 | 49 | * Equip with a NB-IoT SIM Card to access to NB-IoT Network. |
| 50 | |||
| 51 | The S31-NB's cellular module has the IMEI: **863663062789483** | ||
| 52 | |||
| 53 | === 2.3.2 Check Uplink Data === | ||
| 54 | |||
| 55 | Re-activate the S31-NB, and we can see it in the debug window: | ||
| 56 | |||
| 57 | [[image:1728378218744-800.png||height="431" width="1003"]] | ||
| 58 | |||
| 59 | === 2.3.3 Auto-Create Device === | ||
| 60 | |||
| 61 | 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. | ||
| 62 | |||
| 63 | [[image:1728378968101-683.png||height="273" width="1307"]] | ||
| 64 | |||
| 65 | [[image:1728379050044-764.png||height="424" width="1312"]] | ||
| 66 | |||
| 67 | |||
![]() |
6.1 | 68 | **So we have this device in the Tenant Device List. The next step will be how to use these value to make a nice dashboard for user's application.** |
![]() |
4.1 | 69 | |
![]() |
10.1 | 70 | = 3. Connect to The Things Stack = |
![]() |
8.1 | 71 | |
![]() |
10.1 | 72 | == 3.1 Network Structure == |
![]() |
8.1 | 73 | |
| |
18.1 | 74 | 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 | 75 | |
| |
18.1 | 76 | [[image:1732679444539-853.jpg||height="388" width="1452"]] |
| 77 | |||
![]() |
10.1 | 78 | == 3.2 Creat Integration for The Things Stack. == |
![]() |
8.1 | 79 | |
| 80 | (% class="lead" %) | ||
| 81 | Add Integration | ||
| 82 | |||
| 83 | [[image:1728535775119-971.png||height="456" width="1087"]] | ||
| 84 | |||
| 85 | |||
| 86 | (% class="lead" %) | ||
| 87 | Choose Connection Type | ||
| 88 | |||
| 89 | [[image:1728535857345-950.png]] | ||
| 90 | |||
| 91 | |||
| 92 | (% class="lead" %) | ||
| 93 | Input Uplink Data Converter Code | ||
| 94 | |||
| 95 | [[image:1728535941851-388.png||height="466" width="398"]] | ||
| 96 | |||
![]() |
11.1 | 97 | 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 | 98 | |
| 99 | |||
| 100 | (% class="lead" %) | ||
| 101 | Input Downlink Converter | ||
| 102 | |||
| 103 | [[image:1728536142721-488.png||height="470" width="407"]] | ||
| 104 | |||
![]() |
11.1 | 105 | 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 | 106 | |
| 107 | |||
| 108 | (% class="lead" %) | ||
| 109 | Set up Connection to The Things Network application | ||
| 110 | |||
| 111 | [[image:1728536305503-380.png||height="510" width="1206"]] | ||
| 112 | |||
| 113 | |||
| 114 | |||
| 115 | (% class="lead" %) | ||
| 116 | Test Connection & Add iIntegration | ||
| 117 | |||
| 118 | [[image:1728536374214-962.png]] | ||
| 119 | |||
| 120 | After add , we can see the integration here: | ||
| 121 | |||
| 122 | [[image:1728536420275-153.png||height="208" width="1404"]] | ||
| 123 | |||
| 124 | |||
![]() |
10.1 | 125 | == 3.3 Test Uplink == |
![]() |
8.1 | 126 | |
| 127 | We can use Simulate Uplink to simulate an uplink in the things stack. Then we should be able to see the message in ThingsEye | ||
| 128 | |||
| 129 | [[image:1728536524638-768.png||height="493" width="1071"]] | ||
| 130 | |||
| 131 | |||
| 132 | [[image:1728536541040-814.png]] | ||
![]() |
13.1 | 133 | |
| |
15.1 | 134 | = 4.Connect chirpstack to Thingseye by way of MQTT = |
![]() |
13.1 | 135 | |
| |
15.1 | 136 | * **How to connect chirpstack to Thingseye by way of MQTT? The following tutorial will show you** |
![]() |
13.1 | 137 | |
| |
18.1 | 138 | == 4.1 Network Structure == |
![]() |
13.1 | 139 | |
| |
18.1 | 140 | 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. |
| 141 | |||
| 142 | [[image:1732679647697-982.jpg||height="399" width="1492"]] | ||
| 143 | |||
| 144 | == 4.2 Thingseye adds MQTT integration == | ||
| 145 | |||
| |
15.1 | 146 | 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"; |
| 147 | |||
| |
16.1 | 148 | [[image:图片1.png]] |
| |
15.1 | 149 | |
| |
18.1 | 150 | == 4.3 Add an uplink and downlink data converter == |
| |
15.1 | 151 | |
| 152 | In the function decoder field, specify the script to parse and transform the data. | ||
| 153 | |||
| |
16.1 | 154 | [[image:图片2.png]] |
| |
15.1 | 155 | |
| 156 | * **Uplink——JavaScript:** | ||
| 157 | |||
| 158 | var data = decodeToJson(payload); | ||
| 159 | |||
| 160 | var deviceName = data.deviceInfo.deviceName; | ||
| 161 | |||
| 162 | var deviceType = data.applicationName; | ||
| 163 | |||
| 164 | var devEui = data.deviceInfo.devEui | ||
| 165 | |||
| 166 | var label = data.deviceInfo.devEui | ||
| 167 | |||
| 168 | var model = {}; | ||
| 169 | |||
| 170 | var data2 = data.object; | ||
| 171 | |||
| 172 | var flg = data.fPort | ||
| 173 | |||
| 174 | for (var key in data2) { | ||
| 175 | |||
| 176 | ~/~/ 将属性名存入新对象中 | ||
| 177 | |||
| 178 | model[key] = data2[key]; | ||
| 179 | |||
| 180 | } | ||
| 181 | |||
| 182 | ~/~/var obj = {"devid":deviceName} | ||
| 183 | |||
| 184 | var result = { | ||
| 185 | |||
| 186 | deviceName: deviceName, | ||
| 187 | |||
| 188 | deviceType: deviceType, | ||
| 189 | |||
| 190 | telemetry: model, | ||
| 191 | |||
| 192 | groupName: "Case Study", | ||
| 193 | |||
| 194 | ~/~/label:label, | ||
| 195 | |||
| 196 | attributes:{"devEui":devEui, | ||
| 197 | |||
| 198 | ~/~/"timevalue":"test", | ||
| 199 | |||
| 200 | "inactivityTimeout":1260000 | ||
| 201 | |||
| 202 | ~/~/ "High_humidity_alarm":"not set", | ||
| 203 | |||
| 204 | ~/~/ "High_temperature_alarm":"not set", | ||
| 205 | |||
| 206 | ~/~/ "Low_humidity_alarm":"not set", | ||
| 207 | |||
| 208 | ~/~/ "Low_temperature_alarm":"not set", | ||
| 209 | |||
| 210 | ~/~/ "Low_voltage_alarm":"not set" | ||
| 211 | |||
| 212 | ~/~/"customerName": "Civionic Engineering & Consulting (2014) Inc." | ||
| 213 | |||
| 214 | } | ||
| 215 | |||
| 216 | }; | ||
| 217 | |||
| 218 | function decodeToString(payload) { | ||
| 219 | |||
| 220 | return String.fromCharCode.apply(String, payload); | ||
| 221 | |||
| 222 | } | ||
| 223 | |||
| 224 | function decodeToJson(payload) { | ||
| 225 | |||
| 226 | var str = decodeToString(payload); | ||
| 227 | |||
| 228 | var data = JSON.parse(str); | ||
| 229 | |||
| 230 | return data; | ||
| 231 | |||
| 232 | } | ||
| 233 | |||
| 234 | return result; | ||
| 235 | |||
| |
16.1 | 236 | [[image:图片3.png]] |
| |
15.1 | 237 | |
| 238 | * **Dowblink——JavaScript:** | ||
| 239 | |||
| 240 | ~/~/ Encode downlink data from incoming Rule Engine message | ||
| 241 | |||
| 242 | ~/~/ msg - JSON message payload downlink message json | ||
| 243 | |||
| 244 | ~/~/ msgType - type of message, for ex. 'ATTRIBUTES_UPDATED', 'POST_TELEMETRY_REQUEST', etc. | ||
| 245 | |||
| 246 | ~/~/ metadata - list of key-value pairs with additional data about the message | ||
| 247 | |||
| 248 | ~/~/ integrationMetadata - list of key-value pairs with additional data defined in Integration executing this converter | ||
| 249 | |||
| 250 | ~/~/ /~*~* Encoder ~*~*/ | ||
| 251 | |||
| 252 | ~/~/var data = {"value":99}; | ||
| 253 | |||
| 254 | ~/~/ ~/~/ Process data from incoming message and metadata | ||
| 255 | |||
| 256 | ~/~/ data.tempFreq = msg.temperatureUploadFrequency; | ||
| 257 | |||
| 258 | ~/~/ data.humFreq = msg.humidityUploadFrequency; | ||
| 259 | |||
| 260 | ~/~/ data.devSerialNumber = metadata['ss_serialNumber']; | ||
| 261 | |||
| 262 | ~/~/ ~/~/ Result object with encoded downlink payload | ||
| 263 | |||
| 264 | var result = { | ||
| 265 | |||
| 266 | ~/~/ downlink data content type: JSON, TEXT or BINARY (base64 format) | ||
| 267 | |||
| 268 | contentType: "TEXT", | ||
| 269 | |||
| 270 | ~/~/ downlink data | ||
| 271 | |||
| 272 | data: msg.shared_value ,~/~/JSON.stringify(data), | ||
| 273 | |||
| 274 | ~/~/ Optional metadata object presented in key/value format | ||
| 275 | |||
| 276 | metadata: { | ||
| 277 | |||
| 278 | topic: '/test/down/'+metadata.deviceName | ||
| 279 | |||
| 280 | } | ||
| 281 | |||
| 282 | }; | ||
| 283 | |||
| 284 | return result; | ||
| 285 | |||
| |
18.1 | 286 | == 4.4 Configure the connection == |
| |
15.1 | 287 | |
| 288 | Generate MQTT certificate integrated on chirpstack | ||
| 289 | |||
| 290 | Chirpstack generates CA certificate, TLS certificate, and TLS key respectively | ||
| 291 | |||
| 292 | They correspond to the CA certificate file, Certificate file, and Private key file on thingseye | ||
| 293 | |||
| |
16.1 | 294 | [[image:图片4.png]] |
| |
15.1 | 295 | |
| |
16.1 | 296 | [[image:图片5.png||height="630" width="1269"]] |
| |
15.1 | 297 | |
| 298 | * Copy the contents of the certificates and paste them into the linked file below, a total of three certificates are required | ||
| 299 | |||
| 300 | **Integrated Certificate File demo Download Address:** | ||
| 301 | |||
| 302 | [[https:~~/~~/github.com/ThingsEye-io/te-platform/tree/main/chirpstack>>https://github.com/ThingsEye-io/te-platform/tree/main/chirpstack]] | ||
| 303 | |||
| 304 | Enter the server address Host: lns1.thingseye.io on the Connection configuration | ||
| 305 | |||
| 306 | Port: 8883 | ||
| 307 | |||
| 308 | Credentials type: PEM | ||
| 309 | |||
| 310 | Upload the certificate and key file | ||
| 311 | |||
| |
16.1 | 312 | [[image:图片6.png]] |
| |
15.1 | 313 | |
| |
16.1 | 314 | [[image:图片7.png]] |
| |
15.1 | 315 | |
| 316 | The default for Topic is: | ||
| 317 | |||
| 318 | application/**chirpstack application id**/device/+/event/up | ||
| 319 | |||
| |
16.1 | 320 | [[image:图片8.png]] |
| |
15.1 | 321 | |
| 322 | |||
| |
16.1 | 323 | (% class="wikigeneratedid" %) |
| 324 | [[image:屏幕截图 2024-11-25 095805.png||height="374" width="1272"]] | ||
| 325 | |||
| |
18.1 | 326 | == 4.5 Check the connection == |
| |
15.1 | 327 | |
| |
16.1 | 328 | [[image:图片9.png]] |
| |
15.1 | 329 | |
| |
16.1 | 330 | [[image:图片10.png]] |
| |
15.1 | 331 | |
| 332 | = 5. Check Data Uplink Log = | ||
| 333 | |||
![]() |
13.1 | 334 | User can check the devices log in Intergration. |
| 335 | |||
| 336 | [[image:1729562069134-315.png||height="587" width="723"]] |
