Wiki source code of Connect Devices to ThingsEye

Version 22.1 by Hera Guo on 2025/09/25 09:10

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 ----
57
Chxy 4.1 58 === 2.3.3 Auto-Create Device ===
59
60 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.
61
Hera Guo 22.1 62 [[image:1728378968101-683.png||_mstalt="297154" height="210" width="1005"]]
Chxy 4.1 63
Hera Guo 22.1 64 [[image:1728379050044-764.png||_mstalt="295282" height="326" width="1009"]]
Chxy 4.1 65
66
Chxy 6.1 67 **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.**
Chxy 4.1 68
Chxy 10.1 69 = 3.  Connect to The Things Stack =
Chxy 8.1 70
Chxy 10.1 71 == 3.1 Network Structure ==
Chxy 8.1 72
Hera Guo 18.1 73 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 74
Hera Guo 22.1 75 [[image:1732679444539-853.jpg||_mstalt="296933" height="388" width="1452"]]
Hera Guo 18.1 76
Chxy 10.1 77 == 3.2 Creat Integration for The Things Stack. ==
Chxy 8.1 78
79 (% class="lead" %)
80 Add Integration
81
Hera Guo 22.1 82 [[image:1728535775119-971.png||_mstalt="297661" height="456" width="1087"]]
Chxy 8.1 83
84
85 (% class="lead" %)
86 Choose Connection Type
87
Hera Guo 22.1 88 [[image:1728535857345-950.png||_mstalt="297154"]]
Chxy 8.1 89
90
91 (% class="lead" %)
92 Input Uplink Data Converter Code
93
Hera Guo 22.1 94 [[image:1728535941851-388.png||_mstalt="297856" height="466" width="398"]]
Chxy 8.1 95
Chxy 11.1 96 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 97
98
99 (% class="lead" %)
100 Input Downlink Converter
101
Hera Guo 22.1 102 [[image:1728536142721-488.png||_mstalt="296127" height="470" width="407"]]
Chxy 8.1 103
Chxy 11.1 104 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 105
106
107 (% class="lead" %)
108 Set up Connection to The Things Network application
109
Hera Guo 22.1 110 [[image:1728536305503-380.png||_mstalt="293254" height="510" width="1206"]]
Chxy 8.1 111
112
113
114 (% class="lead" %)
115 Test Connection & Add iIntegration
116
Hera Guo 22.1 117 [[image:1728536374214-962.png||_mstalt="295893"]]
Chxy 8.1 118
119 After add , we can see the integration here:
120
Hera Guo 22.1 121 [[image:1728536420275-153.png||_mstalt="293748" height="208" width="1404"]]
Chxy 8.1 122
123
Chxy 10.1 124 == 3.3 Test Uplink ==
Chxy 8.1 125
126 We can use Simulate Uplink to simulate an uplink in the things stack. Then we should be able to see the message in ThingsEye
127
Hera Guo 22.1 128 [[image:1728536524638-768.png||_mstalt="298870" height="493" width="1071"]]
Chxy 8.1 129
130
Hera Guo 22.1 131 [[image:1728536541040-814.png||_mstalt="293215"]]
Chxy 13.1 132
Hera Guo 15.1 133 = 4.Connect chirpstack to Thingseye by way of MQTT =
Chxy 13.1 134
Hera Guo 15.1 135 * **How to connect chirpstack to Thingseye by way of MQTT? The following tutorial will show you**
Chxy 13.1 136
Hera Guo 18.1 137 == 4.1 Network Structure ==
Chxy 13.1 138
Hera Guo 18.1 139 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.
140
Hera Guo 22.1 141 [[image:1732679647697-982.jpg||_mstalt="299884" height="399" width="1492"]]
Hera Guo 18.1 142
143 == 4.2 Thingseye adds MQTT integration ==
144
Hera Guo 15.1 145 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";
146
Hera Guo 22.1 147 [[image:1758762634175-353.png]]
Hera Guo 15.1 148
Hera Guo 18.1 149 == 4.3 Add an uplink and downlink data converter ==
Hera Guo 15.1 150
151 In the function decoder field, specify the script to parse and transform the data.
152
Hera Guo 22.1 153 [[image:图片2.png||_mstalt="5131503"]]
Hera Guo 15.1 154
155 * **Uplink——JavaScript:**
156
157 var data = decodeToJson(payload);
158
159 var deviceName = data.deviceInfo.deviceName;
160
161 var deviceType = data.applicationName;
162
163 var devEui = data.deviceInfo.devEui
164
165 var label = data.deviceInfo.devEui
166
167 var model = {};
168
169 var data2 = data.object;
170
171 var flg = data.fPort
172
173 for (var key in data2) {
174
175 ~/~/ 将属性名存入新对象中
176
177 model[key] = data2[key];
178
179 }
180
181 ~/~/var obj = {"devid":deviceName}
182
183 var result = {
184
185 deviceName: deviceName,
186
187 deviceType: deviceType,
188
189 telemetry: model,
190
191 groupName: "Case Study",
192
193 ~/~/label:label,
194
195 attributes:{"devEui":devEui,
196
197 ~/~/"timevalue":"test",
198
199 "inactivityTimeout":1260000
200
201 ~/~/ "High_humidity_alarm":"not set",
202
203 ~/~/ "High_temperature_alarm":"not set",
204
205 ~/~/ "Low_humidity_alarm":"not set",
206
207 ~/~/ "Low_temperature_alarm":"not set",
208
209 ~/~/ "Low_voltage_alarm":"not set"
210
211 ~/~/"customerName": "Civionic Engineering & Consulting (2014) Inc."
212
213 }
214
215 };
216
217 function decodeToString(payload) {
218
219 return String.fromCharCode.apply(String, payload);
220
221 }
222
223 function decodeToJson(payload) {
224
225 var str = decodeToString(payload);
226
227 var data = JSON.parse(str);
228
229 return data;
230
231 }
232
233 return result;
234
Hera Guo 22.1 235 [[image:图片3.png||_mstalt="5131620"]]
Hera Guo 15.1 236
237 * **Dowblink——JavaScript:**
238
239 ~/~/ Encode downlink data from incoming Rule Engine message
240
241 ~/~/ msg - JSON message payload downlink message json
242
243 ~/~/ msgType - type of message, for ex. 'ATTRIBUTES_UPDATED', 'POST_TELEMETRY_REQUEST', etc.
244
245 ~/~/ metadata - list of key-value pairs with additional data about the message
246
247 ~/~/ integrationMetadata - list of key-value pairs with additional data defined in Integration executing this converter
248
249 ~/~/ /~*~* Encoder ~*~*/
250
251 ~/~/var data = {"value":99};
252
253 ~/~/ ~/~/ Process data from incoming message and metadata
254
255 ~/~/ data.tempFreq = msg.temperatureUploadFrequency;
256
257 ~/~/ data.humFreq = msg.humidityUploadFrequency;
258
259 ~/~/ data.devSerialNumber = metadata['ss_serialNumber'];
260
261 ~/~/ ~/~/ Result object with encoded downlink payload
262
263 var result = {
264
265 ~/~/ downlink data content type: JSON, TEXT or BINARY (base64 format)
266
267 contentType: "TEXT",
268
269 ~/~/ downlink data
270
271 data: msg.shared_value ,~/~/JSON.stringify(data),
272
273 ~/~/ Optional metadata object presented in key/value format
274
275 metadata: {
276
277 topic: '/test/down/'+metadata.deviceName
278
279 }
280
281 };
282
283 return result;
284
Hera Guo 18.1 285 == 4.4 Configure the connection ==
Hera Guo 15.1 286
287 Generate MQTT certificate integrated on chirpstack
288
289 Chirpstack generates CA certificate, TLS certificate, and TLS key respectively
290
291 They correspond to the CA certificate file, Certificate file, and Private key file on thingseye
292
Hera Guo 22.1 293 [[image:图片4.png||_mstalt="5131737"]]
Hera Guo 15.1 294
Hera Guo 22.1 295 [[image:图片5.png||_mstalt="5131854" height="630" width="1269"]]
Hera Guo 15.1 296
297 * Copy the contents of the certificates and paste them into the linked file below, a total of three certificates are required
298
299 **Integrated Certificate File demo Download Address:**
300
301 [[https:~~/~~/github.com/ThingsEye-io/te-platform/tree/main/chirpstack>>https://github.com/ThingsEye-io/te-platform/tree/main/chirpstack]]
302
303 Enter the server address Host: lns1.thingseye.io on the Connection configuration
304
305 Port: 8883
306
307 Credentials type: PEM
308
309 Upload the certificate and key file
310
Hera Guo 22.1 311 [[image:图片6.png||_mstalt="5131971"]]
Hera Guo 15.1 312
Hera Guo 22.1 313 [[image:图片7.png||_mstalt="5132088"]]
Hera Guo 15.1 314
315 The default for Topic is:
316
317 application/**chirpstack application id**/device/+/event/up
318
Hera Guo 22.1 319 [[image:图片8.png||_mstalt="5132205"]]
Hera Guo 15.1 320
321
Hera Guo 16.1 322 (% class="wikigeneratedid" %)
Hera Guo 22.1 323 [[image:屏幕截图 2024-11-25 095805.png||_mstalt="10831028" height="374" width="1272"]]
Hera Guo 16.1 324
Hera Guo 18.1 325 == 4.5 Check the connection ==
Hera Guo 15.1 326
Hera Guo 22.1 327 [[image:图片9.png||_mstalt="5132322"]]
Hera Guo 15.1 328
Hera Guo 22.1 329 [[image:图片10.png||_mstalt="5142449"]]
Hera Guo 15.1 330
331 = 5. Check Data Uplink Log =
332
Chxy 13.1 333 User can check the devices log in Intergration.
334
Hera Guo 22.1 335 [[image:1729562069134-315.png||_mstalt="294190" height="587" width="723"]]