Wiki source code of Connect Devices to ThingsEye

Version 23.1 by Hera Guo on 2025/09/25 09:18

Hide last authors
Chxy 1.1 1
2
Chxy 2.1 3 **Table of Contents:**
Chxy 1.1 4
Chxy 2.1 5 {{toc/}}
Chxy 1.1 6
Chxy 2.1 7
Chxy 3.1 8 = 1. Overview =
9
Hera Guo 22.1 10 This instructions will show you how to connect a device that has been registered to the server to the Thingseye platform.
Chxy 3.1 11
Hera Guo 22.1 12
Chxy 2.1 13 = 2. UDP Procotol  ~-~- Directly Connection =
14
15 == 2.1 UDP Interface ==
16
Chxy 1.1 17 * Server Address: server1.thingseye.io
Hera Guo 20.1 18 * Port: 11560
Chxy 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
Hera Guo 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
Chxy 1.1 26
Hera Guo 22.1 27 ----
Chxy 1.1 28
Hera Guo 22.1 29 == 2.2 Test with Dragino NB device ==
Chxy 1.1 30
Hera Guo 22.1 31 === 2.2.1 Configure NB-IoT End Node ===
Chxy 1.1 32
Chxy 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
Hera Guo 19.1 35 * Set to use ThingsEye UDP server: **AT+SERVADDR=server1.thingseye.io,11560**
Chxy 8.1 36 * Use UDP Uplink & Json protocol:** AT+PRO=2,5**
Chxy 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
Hera Guo 22.1 41 === 2.2.2 Check Uplink Data ===
Chxy 4.1 42
Hera Guo 22.1 43 Turn on debug mod
44
45 [[image:1758762334133-608.png||height="494" width="1003"]]
46
47 ----
48
Chxy 4.1 49 Re-activate the S31-NB, and we can see it in the debug window:
50
Hera Guo 22.1 51 [[image:1758762371290-726.png||height="528" width="1005"]]
Chxy 4.1 52
Hera Guo 22.1 53 [[image:1728378218744-800.png||_mstalt="295269" height="431" width="1003"]]
54
55 ----
56
Hera Guo 23.1 57 === 2.2.4 Auto-Create Device ===
Chxy 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
Hera Guo 22.1 61 [[image:1728378968101-683.png||_mstalt="297154" height="210" width="1005"]]
Chxy 4.1 62
Hera Guo 22.1 63 [[image:1728379050044-764.png||_mstalt="295282" height="326" width="1009"]]
Chxy 4.1 64
65
Hera Guo 23.1 66 ----
Chxy 4.1 67
Chxy 10.1 68 = 3.  Connect to The Things Stack =
Chxy 8.1 69
Chxy 10.1 70 == 3.1 Network Structure ==
Chxy 8.1 71
Hera Guo 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.
Chxy 8.1 73
Hera Guo 22.1 74 [[image:1732679444539-853.jpg||_mstalt="296933" height="388" width="1452"]]
Hera Guo 18.1 75
Chxy 10.1 76 == 3.2 Creat Integration for The Things Stack. ==
Chxy 8.1 77
78 (% class="lead" %)
79 Add Integration
80
Hera Guo 23.1 81 [[image:1728535775119-971.png||_mstalt="297661" height="423" width="1007"]]
Chxy 8.1 82
83
84 (% class="lead" %)
85 Choose Connection Type
86
Hera Guo 23.1 87 [[image:1728535857345-950.png||_mstalt="297154" height="257" width="403"]]
Chxy 8.1 88
89
90 (% class="lead" %)
91 Input Uplink Data Converter Code
92
Hera Guo 22.1 93 [[image:1728535941851-388.png||_mstalt="297856" height="466" width="398"]]
Chxy 8.1 94
Chxy 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]]
Chxy 8.1 96
97
98 (% class="lead" %)
99 Input Downlink Converter
100
Hera Guo 22.1 101 [[image:1728536142721-488.png||_mstalt="296127" height="470" width="407"]]
Chxy 8.1 102
Chxy 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]]
Chxy 8.1 104
105
106 (% class="lead" %)
107 Set up Connection to The Things Network application
108
Hera Guo 23.1 109 [[image:1728536305503-380.png||_mstalt="293254" height="431" width="1019"]]
Chxy 8.1 110
111
112
113 (% class="lead" %)
114 Test Connection & Add iIntegration
115
Hera Guo 23.1 116 [[image:1728536374214-962.png||_mstalt="295893" height="380" width="1024"]]
Chxy 8.1 117
118 After add , we can see the integration here:
119
Hera Guo 23.1 120 [[image:1728536420275-153.png||_mstalt="293748" height="152" width="1026"]]
Chxy 8.1 121
122
Chxy 10.1 123 == 3.3 Test Uplink ==
Chxy 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
Hera Guo 23.1 127 [[image:1728536524638-768.png||_mstalt="298870" height="473" width="1028"]]
Chxy 8.1 128
129
Hera Guo 23.1 130 [[image:1728536541040-814.png||_mstalt="293215" height="424" width="1026"]]
Chxy 13.1 131
Hera Guo 23.1 132
133
Hera Guo 15.1 134 = 4.Connect chirpstack to Thingseye by way of MQTT =
Chxy 13.1 135
Hera Guo 15.1 136 * **How to connect chirpstack to Thingseye by way of MQTT? The following tutorial will show you**
Chxy 13.1 137
Hera Guo 18.1 138 == 4.1 Network Structure ==
Chxy 13.1 139
Hera Guo 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
Hera Guo 22.1 142 [[image:1732679647697-982.jpg||_mstalt="299884" height="399" width="1492"]]
Hera Guo 18.1 143
144 == 4.2 Thingseye adds MQTT integration ==
145
Hera Guo 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
Hera Guo 23.1 148 [[image:1758762634175-353.png||height="783" width="1020"]]
Hera Guo 15.1 149
Hera Guo 18.1 150 == 4.3 Add an uplink and downlink data converter ==
Hera Guo 15.1 151
Hera Guo 23.1 152 In the function decoder field, specify the uplink script to parse and transform the data.
Hera Guo 15.1 153
Hera Guo 23.1 154 [[image:1758763061703-634.png||height="783" width="1020"]]
Hera Guo 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
Hera Guo 23.1 236 ----
Hera Guo 15.1 237
Hera Guo 23.1 238 In the function decoder field, specify the downlink script to parse and transform the data.
239
240 [[image:1758763019466-819.png||height="787" width="1022"]]
241
Hera Guo 15.1 242 * **Dowblink——JavaScript:**
243
244 ~/~/ Encode downlink data from incoming Rule Engine message
245
246 ~/~/ msg - JSON message payload downlink message json
247
248 ~/~/ msgType - type of message, for ex. 'ATTRIBUTES_UPDATED', 'POST_TELEMETRY_REQUEST', etc.
249
250 ~/~/ metadata - list of key-value pairs with additional data about the message
251
252 ~/~/ integrationMetadata - list of key-value pairs with additional data defined in Integration executing this converter
253
254 ~/~/ /~*~* Encoder ~*~*/
255
256 ~/~/var data = {"value":99};
257
258 ~/~/ ~/~/ Process data from incoming message and metadata
259
260 ~/~/ data.tempFreq = msg.temperatureUploadFrequency;
261
262 ~/~/ data.humFreq = msg.humidityUploadFrequency;
263
264 ~/~/ data.devSerialNumber = metadata['ss_serialNumber'];
265
266 ~/~/ ~/~/ Result object with encoded downlink payload
267
268 var result = {
269
270 ~/~/ downlink data content type: JSON, TEXT or BINARY (base64 format)
271
272 contentType: "TEXT",
273
274 ~/~/ downlink data
275
276 data: msg.shared_value ,~/~/JSON.stringify(data),
277
278 ~/~/ Optional metadata object presented in key/value format
279
280 metadata: {
281
282 topic: '/test/down/'+metadata.deviceName
283
284 }
285
286 };
287
288 return result;
289
Hera Guo 23.1 290 ----
291
Hera Guo 18.1 292 == 4.4 Configure the connection ==
Hera Guo 15.1 293
294 Generate MQTT certificate integrated on chirpstack
295
296 Chirpstack generates CA certificate, TLS certificate, and TLS key respectively
297
298 They correspond to the CA certificate file, Certificate file, and Private key file on thingseye
299
Hera Guo 23.1 300 [[image:图片4.png||_mstalt="5131737" height="504" width="1016"]]
Hera Guo 15.1 301
Hera Guo 23.1 302 [[image:图片5.png||_mstalt="5131854" height="505" width="1017"]]
Hera Guo 15.1 303
304 * Copy the contents of the certificates and paste them into the linked file below, a total of three certificates are required
305
306 **Integrated Certificate File demo Download Address:**
307
308 [[https:~~/~~/github.com/ThingsEye-io/te-platform/tree/main/chirpstack>>https://github.com/ThingsEye-io/te-platform/tree/main/chirpstack]]
309
310 Enter the server address Host: lns1.thingseye.io on the Connection configuration
311
312 Port: 8883
313
314 Credentials type: PEM
315
316 Upload the certificate and key file
317
Hera Guo 22.1 318 [[image:图片6.png||_mstalt="5131971"]]
Hera Guo 15.1 319
Hera Guo 22.1 320 [[image:图片7.png||_mstalt="5132088"]]
Hera Guo 15.1 321
322 The default for Topic is:
323
324 application/**chirpstack application id**/device/+/event/up
325
Hera Guo 22.1 326 [[image:图片8.png||_mstalt="5132205"]]
Hera Guo 15.1 327
328
Hera Guo 16.1 329 (% class="wikigeneratedid" %)
Hera Guo 22.1 330 [[image:屏幕截图 2024-11-25 095805.png||_mstalt="10831028" height="374" width="1272"]]
Hera Guo 16.1 331
Hera Guo 18.1 332 == 4.5 Check the connection ==
Hera Guo 15.1 333
Hera Guo 22.1 334 [[image:图片9.png||_mstalt="5132322"]]
Hera Guo 15.1 335
Hera Guo 22.1 336 [[image:图片10.png||_mstalt="5142449"]]
Hera Guo 15.1 337
338 = 5. Check Data Uplink Log =
339
Chxy 13.1 340 User can check the devices log in Intergration.
341
Hera Guo 22.1 342 [[image:1729562069134-315.png||_mstalt="294190" height="587" width="723"]]