Wiki source code of ThingsBoard
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | **Table of Contents:** | ||
2 | |||
3 | {{toc/}} | ||
4 | |||
5 | {{warning}} | ||
6 | Draft Document | ||
7 | {{/warning}} | ||
8 | |||
9 | = Introduction = | ||
10 | |||
11 | This document guides you on integrating Dragino **-NB** and **-CB** series devices data with ThingsBoard. For this guide, we use ThingsBoard Cloud, which is one of the ThingsBoard versions that allows you to try it for free. | ||
12 | |||
13 | The **NB series** devices end with the suffix **-NB**, and the **CB series** devices end with the suffix **-CB**. For example, **S31B-NB** is an **NB device**, and **S31-CB** is a **CB device**. | ||
14 | |||
15 | |||
16 | = Add New Device = | ||
17 | |||
18 | First, we will add a device to simulate data using MQTT. This device does not represent a real physical device but serves as a common virtual device for all Dragino NB/CB devices. | ||
19 | |||
20 | In the left navigation, click **Entities** and then click **Devices**. | ||
21 | |||
22 | |||
23 | [[image:device-list-empty.png]] | ||
24 | |||
25 | |||
26 | On the **Devices **page, click on the ‘**+**’ button, and then click on the **Add new device **from the dropdown menu. | ||
27 | |||
28 | |||
29 | [[image:ThingsBoard-add-new-device.png||height="279" width="500"]] | ||
30 | |||
31 | |||
32 | The **Add new device** window appears. Name the device, for example '**Virtual NB/CB device**'. | ||
33 | |||
34 | Click on the **Next: Credentials** button. | ||
35 | |||
36 | |||
37 | [[image:new-device-device-details.png||height="529" width="500"]] | ||
38 | |||
39 | |||
40 | Click the **Access token** button if it is not selected by default. | ||
41 | |||
42 | Copy the **Access token** to a text editor, as you will need it in the section **'Sending data from an MQTT client'**. | ||
43 | |||
44 | Click on the **Add** button. | ||
45 | |||
46 | [[image:new-device-access-token.png||height="528" width="500"]] | ||
47 | |||
48 | |||
49 | Click on the **MQTT** button. | ||
50 | |||
51 | Then, select the operating system you are going to use with the **Mosquitto MQTT Broker**. This example shows sending a command from a computer running **MacOS**. First, install the necessary client tools on your computer. | ||
52 | |||
53 | Copy the MQTT pub command in the '**Execute the following command**' section. | ||
54 | |||
55 | {{code language="none"}} | ||
56 | mosquitto_pub -d -q 1 -h mqtt.eu.thingsboard.cloud -p 1883 -t v1/devices/me/telemetry -u "24vk3w9h7sqdld1me5eh" -m "{temperature:25}" | ||
57 | {{/code}} | ||
58 | |||
59 | Note that the **State** is still shown as **'Inactive'** in the **Latest telemetry** section. | ||
60 | |||
61 | [[image:new-device-check-connectivity.png||height="522" width="500"]] | ||
62 | |||
63 | |||
64 | On the terminal, enter the command you copied. This command sends the MQTT payload **{temperature:25}** as telemetry data from a virtual device named **'me'**. | ||
65 | |||
66 | |||
67 | [[image:mqtt-command-1.png||height="228" width="700"]] | ||
68 | |||
69 | |||
70 | |||
71 | |||
72 | [[image:new-device-telemetry-test.png||height="520" width="500"]] | ||
73 | |||
74 | |||
75 | |||
76 | = Data Converters = | ||
77 | |||
78 | In **ThingsBoard**, **Data Converters** are components used to transform incoming or outgoing data between different formats, typically to convert raw telemetry data from devices into a structured format that ThingsBoard can understand, or vice versa. | ||
79 | |||
80 | |||
81 | == Uplink == | ||
82 | |||
83 | In the left navigation, click **Integrations center**, and then click **Data converters**. | ||
84 | |||
85 | |||
86 | [[image:data-converter-list-page.png]] | ||
87 | |||
88 | |||
89 | On the **Data converters** page, click on the ‘+’ button, and then click on the **Create new converter** from the dropdown menu. | ||
90 | |||
91 | |||
92 | [[image:ThingsBoard-new-data-converter.png||height="282" width="500"]] | ||
93 | |||
94 | |||
95 | The **Add data converter** window will appear. Name it ‘**MQTT Uplink Converter NB/CB**’ and select the Type as **Uplink**. | ||
96 | |||
97 | Click on the **JavaScript** button. Now copy and paste the following JavaScript to the **Decoder function** section. This decoder function is valid for both NB and CB series devices. | ||
98 | |||
99 | {{code language="JavaScript"}} | ||
100 | //Version: 0.1 | ||
101 | // decode payload to string | ||
102 | var payloadStr = decodeToString(payload); | ||
103 | |||
104 | // decode payload to JSON | ||
105 | var objdata = {}; | ||
106 | var obj1 = {}; | ||
107 | var data = decodeToJson(payload); | ||
108 | var deviceName = data.IMEI; | ||
109 | delete data.IMEI; | ||
110 | var modelname = "Dragino " + data.Model; | ||
111 | //var mod = data.mod | ||
112 | delete data.Model; | ||
113 | //delete data.mod | ||
114 | var timestamp = new Date().getTime(); | ||
115 | |||
116 | for (var key in data) { | ||
117 | |||
118 | if (Number(key)) { | ||
119 | obj1[key] = data[key]; | ||
120 | obj1[key][obj1[key].length - 1] = Number(new Date( | ||
121 | obj1[key][obj1[key].length - 1])); | ||
122 | |||
123 | } | ||
124 | //Alec submitted25/02/25 | ||
125 | //turn old key into new | ||
126 | else if (key === "Reading") { | ||
127 | objdata["reading"] = data[key]; | ||
128 | } else if (key === "work mode") { | ||
129 | objdata["work_mode"] = data[key]; | ||
130 | } else if (key === "hum") { | ||
131 | objdata["humidity"] = data[key]; | ||
132 | }else if (key === "hum2") { | ||
133 | objdata["humidity2"] = data[key]; | ||
134 | } else if (key === "hum3") { | ||
135 | objdata["humidity3"] = data[key]; | ||
136 | } else if (key === "tem") { | ||
137 | objdata["temperature"] = data[key]; | ||
138 | } else if (key === "tem2") { | ||
139 | objdata["temperature2"] = data[key]; | ||
140 | } else if (key === "tem3") { | ||
141 | objdata["temperature3"] = data[key]; | ||
142 | } else if (key === "DS18B20_Temp") { | ||
143 | objdata["temperature_pro"] = data[key]; | ||
144 | } else if (key === "ds18b20_temperature") { | ||
145 | objdata["temperature_pro"] = data[key]; | ||
146 | } else if (key === "DS18B20_temperature_pro") { | ||
147 | objdata["temperature_pro"] = data[key]; | ||
148 | } else if (key === "tdc send flag") { | ||
149 | objdata["tdc_send_flag"] = data[key]; | ||
150 | } else if (key === "trigger mode") { | ||
151 | objdata["trigger_mode"] = data[key]; | ||
152 | } else if (key === "soil dielectric constant") { | ||
153 | objdata["soil_dielectric_constant"] = data[key]; | ||
154 | } else if (key === "door open num") { | ||
155 | objdata["door_open_num"] = data[key]; | ||
156 | } else if (key === "door duration") { | ||
157 | objdata["door_duration"] = data[key]; | ||
158 | } else if (key === "count time") { | ||
159 | objdata["count_time"] = data[key]; | ||
160 | } else if (key === "last open time2") { | ||
161 | objdata["last_open_time2"] = data[key]; | ||
162 | } else if (key === "last open time3") { | ||
163 | objdata["last_open_time3"] = data[key]; | ||
164 | } | ||
165 | //Alec submitted25/02/25 | ||
166 | else { | ||
167 | objdata[key] = data[key] | ||
168 | } | ||
169 | } | ||
170 | var listdata = [{ | ||
171 | "ts": timestamp, | ||
172 | "values": objdata | ||
173 | }] | ||
174 | for (var key1 in obj1) { | ||
175 | if (modelname == "Dragino RS485-NB") { | ||
176 | listdata.push({ | ||
177 | "ts": obj1[key1][obj1[key1].length - 1], | ||
178 | "values": { | ||
179 | "Payload": obj1[key1][0], | ||
180 | } | ||
181 | }) | ||
182 | } else { | ||
183 | listdata.push({ | ||
184 | "ts": obj1[key1][obj1[key1].length - 1], | ||
185 | "values": { | ||
186 | "values": obj1[key1] | ||
187 | }, | ||
188 | }) | ||
189 | } | ||
190 | } | ||
191 | var result = { | ||
192 | |||
193 | deviceName: deviceName, | ||
194 | deviceType: modelname, | ||
195 | attributes: { | ||
196 | model: modelname, | ||
197 | //customerName: "NB-CB", | ||
198 | //groupName: "NB-CB", | ||
199 | //integrationName: metadata['integrationName'] | ||
200 | |||
201 | }, | ||
202 | telemetry: listdata | ||
203 | } | ||
204 | |||
205 | function decodeToString(payload) { | ||
206 | return String.fromCharCode.apply(String, payload); | ||
207 | } | ||
208 | |||
209 | function decodeToJson(payload) { | ||
210 | // covert payload to string. | ||
211 | var str = decodeToString(payload); | ||
212 | |||
213 | // parse string to JSON | ||
214 | var data = JSON.parse(str); | ||
215 | return data; | ||
216 | } | ||
217 | |||
218 | return result; | ||
219 | |||
220 | {{/code}} | ||
221 | |||
222 | Click on the **Add** button. | ||
223 | |||
224 | |||
225 | [[image:uplink-data-converter.png||height="529" width="500"]] | ||
226 | |||
227 | |||
228 | |||
229 | You should see that the newly added **uplink data converter** is listed on the **Data Converters** page. | ||
230 | |||
231 | |||
232 | [[image:data-converter-list-showing-uplink-dc.png]] | ||
233 | |||
234 | |||
235 | == Downlink == | ||
236 | |||
237 | On the **Data converters** page, click on the ‘**+**’ button, and then click on the **Create new converter** from the dropdown menu. | ||
238 | |||
239 | |||
240 | [[image:ThingsBoard-new-data-converter.png||height="282" width="500"]] | ||
241 | |||
242 | |||
243 | The **Add data converter** window will appear. Name it ‘**MQTT Downlink Converter NB/CB**’ and select the Type as **Downlink**. | ||
244 | |||
245 | Click on the **JavaScript** button. Now copy and paste the following JavaScript to the **Encoder function **section. This encoder function is valid for both NB and CB series devices. | ||
246 | |||
247 | |||
248 | {{code language="JavaScript"}} | ||
249 | function hexToBase64(hexString) { | ||
250 | // 将16进制字符串两个字符转换为一个字节 | ||
251 | var bytes = hexString.match(/.{2}/g); | ||
252 | // 对每个字节进行解析,并转换为对应的字符 | ||
253 | var binaryString = bytes.map(function(byte) { | ||
254 | return String.fromCharCode(parseInt(byte, 16)); | ||
255 | }).join(''); | ||
256 | |||
257 | // 使用btoa进行base64编码 | ||
258 | return btoa(binaryString); | ||
259 | } | ||
260 | |||
261 | // Result object with encoded downlink payload | ||
262 | var result = { | ||
263 | // downlink data content type: JSON, TEXT or BINARY (base64 format) | ||
264 | contentType: "BINARY", | ||
265 | |||
266 | // downlink data | ||
267 | data:hexToBase64(metadata.shared_value) | ||
268 | |||
269 | // Optional metadata object presented in key/value format | ||
270 | //metadata: {} | ||
271 | |||
272 | }; | ||
273 | |||
274 | return result; | ||
275 | {{/code}} | ||
276 | |||
277 | |||
278 | Click on the **Add** button. | ||
279 | |||
280 | |||
281 | [[image:downlink-data-converter.png||height="530" width="500"]] | ||
282 | |||
283 | |||
284 | |||
285 | You should see that the newly added **downlink data converter** is listed on the **Data Converters** page. | ||
286 | |||
287 | |||
288 | [[image:data-converter-list.png]] | ||
289 | |||
290 | |||
291 | = Add Integration = | ||
292 | |||
293 | In the left navigation, click **Integrations center**, and then click **Integrations**. | ||
294 | |||
295 | On the **Integrations** page, click on the '**+**' button. | ||
296 | |||
297 | |||
298 | [[image:data-converter-list-page.png]] | ||
299 | |||
300 | |||
301 | The **Add integration** window appears. | ||
302 | |||
303 | In the **Add integration** window, configure the following settings: | ||
304 | |||
305 | |||
306 | **Basic settings:** | ||
307 | |||
308 | * **Integration type**: MQTT | ||
309 | * **Name**: MQTT integration NB/CB | ||
310 | |||
311 | Click **Next** button. | ||
312 | |||
313 | |||
314 | [[image:add-integration-basic-settings.png||height="511" width="500"]] | ||
315 | |||
316 | |||
317 | |||
318 | **Uplink data converter:** | ||
319 | |||
320 | * Click on the **Select existing** button. | ||
321 | * **Uplink data converter**: Select **MQTT Uplink Converter NB/CB **from the dropdown list. | ||
322 | |||
323 | Click **Next** button. | ||
324 | |||
325 | |||
326 | [[image:add-integration-uplink-data-converter.png||width="500"]] | ||
327 | |||
328 | |||
329 | |||
330 | **Downlink data converter:** | ||
331 | |||
332 | * Click on the **Select existing** button. | ||
333 | * **Downlink data converter**: Select **MQTT Downlink Converter NB/CB **from the dropdown list. | ||
334 | |||
335 | Click **Next** button. | ||
336 | |||
337 | |||
338 | [[image:add-integration-downlink-data-converter.png||height="510" width="500"]] | ||
339 | |||
340 | |||
341 | |||
342 | **Connection:** | ||
343 | |||
344 | * **Host**: mqtt.eu.thingsboard.cloud (This is the host name you copied from the device - See section xxxxx for more information) | ||
345 | * **Port**: 1883 (This is the port number you copied from the device - See section xxxxx for more information) | ||
346 | * **Credentials**: Basic | ||
347 | * **Username**: Access token (This is the access token you copied from the device) | ||
348 | * **Password** - Leave it as blank | ||
349 | * **Topic** - v1/devices/+/telemetry (sightly modify the topic 'v1/devices/me/telemetry' you copied from the device to enable receiving data from any 'device name') | ||
350 | * **QoS** - 0-At most once | ||
351 | |||
352 | Click on the **Check connection** button to verify the MQTT connection using the provided parameters. | ||
353 | |||
354 | |||
355 | [[image:add-integration-connection.png||width="500"]] | ||
356 | |||
357 | |||
358 | If the connection is successful, you will see the **Connected** message. | ||
359 | |||
360 | Click on the **Add** button. | ||
361 | |||
362 | |||
363 | [[image:add-connection-success.png||height="511" width="500"]] | ||
364 | |||
365 | |||
366 | You should see that the newly added integration is listed on the **Integrations** page. | ||
367 | |||
368 | Since we haven't received data from a device yet, the integration **Status** is shown as **Pending.** | ||
369 | |||
370 | |||
371 | |||
372 | [[image:integrations-list-added-pending.png]] | ||
373 | |||
374 | |||
375 | = Verifying the receipt of data from the device = | ||
376 | |||
377 | Connect **S31B-NB** to transfer information. If the integration was performed without errors, after the transmission of the first telemetry, a new device with the name “xxxxx” will appear in the Devices → All. Also, you can verify the input and output data, respectively, before and after conversion in Data converters → UDP Uplink Converter NB/CB → Events. |