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