Wiki source code of Connect Devices to ThingsEye

Version 16.1 by Hera Guo on 2024/11/27 09:09

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