Wiki source code of MQTT Forward Instruction

Version 37.11 by Xiaoling on 2022/06/01 10:45

Show last authors
1 **~ Contents:**
2
3 {{toc/}}
4
5
6 = 1. Introduction =
7
8 Dragino LoRa/LoRaWAN gateway support MQTT forwarding. It can forward the sensor data from LoRa network to MQTT server , and vice verse.
9
10
11 == 1.1 Support Devices ==
12
13 This MQTT forward instruction is for below devices:
14
15 * Firmware Version > LG02_LG08-5.3.1580178039 [[Firmware Download>>url:http://www.dragino.com/downloads/index.php?dir=LoRa_Gateway/LPS8/Firmware/Release/]]
16 * LG01N, OLG01N ((% style="color:red" %)**Warning**(%%): LG01-P LG01-S use another instruction: [[MQTT for LG01-P/LG01S>>doc:Through MQTT to upload data.WebHome]])
17 * LG02, OLG02
18 * LG308, DLOS8
19 * LPS8
20 * LIG16
21 * MS14 series if installed with the same firmware. (in this case, the MQTT forward will work , but no LoRa support)
22
23
24
25 = 2. Firmware Change Log for MQTT feature =
26
27 (((
28 This instruction is wrote start from LG02_LG08-5.3.1580178039. Below is related change log since this version of firmware.
29 )))
30
31 * LG02_LG08-5.3.1580178039
32 ** Initiate version
33
34
35
36 = 3. MQTT forward operating principle =
37
38 == 3.1 Network Structure ==
39
40 Below shows the network structure for MQTT forwarding.
41
42 * For Uplink: The sensor sends data to LoRa Gateway via LoRa wireless, The gateway will process these data and forward to remote MQTT Broker via Internet.
43 * For Downlink: The gateway subscribe a topic in the MQTT broker, when there is update on the topic, the gateway will know and broadcast the data to Local LoRa network,
44
45 [[image:image-20220527133547-1.png]]
46
47 General MQTT structure
48
49
50 == 3.2 How sensor data is forwarded ==
51
52 In this MQTT forward feature, the key point is how the gateway process the sensor data.
53
54
55 === 3.2.1 Upstream ===
56
57 Assume there are two sensor nodes, their ID are Node1 ID: 6734 , Node2 ID: 7456. In the remote MQTT broker there are two topics: Topic1: /channel/765800, Topic2: /channel/367860. We can set up in the gateway to map Node1 to Topic1 and Node2 to Topic2. So when there is a sensor data from Node1, the gateway will forward the data to Topic1, when there is sensor data from Node2, the gateway will forward to Topic2.
58
59 (((
60 The data flow works as below diagram.
61 )))
62
63 [[image:image-20220527134000-2.png]]
64
65 (((
66 Upstream path
67 )))
68
69 (((
70 Note: The sensor data can base or LoRa or other method, as long as there are data on the file /var/iot/channels. /span>
71 )))
72
73 (((
74
75 )))
76
77 === 3.2.2 Downstream ===
78
79 The gateway subscribes to a topic of the remote MQTT broker topic. When there is some one publish a value on this topic. The gateway will get it and broadcast to local LoRa Network.
80
81 (((
82 Below are the data flow for downstream.
83 )))
84
85 [[image:image-20220527134038-3.png]]
86
87 Downstream path
88
89
90 == 3.3 Macro Definition ==
91
92 The MQTT publish command use Macro settings to generate flexible upstream payload for MQTT publish.
93
94 (((
95 Currently the (% style="color:#4f81bd" %)**-t (topic)**(%%) and (% style="color:#4f81bd" %)**-m (message)**(%%) support Macros.
96
97
98 )))
99
100 === 3.3.1 -t topic macro ===
101
102 * CHANNEL: Remote Channel ID
103 * CLIENTID: Client ID , Same as -i
104 * WRITE_API: Remote Channel Write API
105 * USERNAME: User ID (-u)
106 * HOSTNAME: Device Hostname
107
108
109
110 === 3.3.2 -m message macro ===
111
112 * HOSTNAME: Device Hostname
113 * CHANNEL: Remote Channel ID
114 * DATA: Sensor Data without time stamp and rssi
115 * META: Completely sensor data with time stamp and rssi
116 * JSON: Convert META to json format.
117
118
119
120 === 3.3.3 Example for Macro ===
121
122 [[image:image-20220527134251-4.png]]
123
124 MQTT Publish configure
125
126
127 Above screen shots shows below format:
128
129 * -t: CLIENTID/CHANNEL/data
130 * -m: DATA
131
132 When there is a LoRa sensor arrive. it will be store at the /var/iot/channels as below:
133
134 [[image:image-20220527134332-5.png]]
135
136 Sensor Data
137
138 (((
139 According to above macro. Gateway will publish (% style="color:#4f81bd" %)**field1=22.0&field2=49.0**(%%) to topic: (% style="color:#4f81bd" %)**dragino-1b7060/78901/data**(%%), where 78901 is the remote channel for this node ID.
140 )))
141
142
143 == 3.4 Modify the MQTT to support more options ==
144
145 The MQTT Client Utility used in Dragino is **mosquitto_pub** and **mosquitto_sub**. User can add more options to the mqtt commands. User can check the valid options by command mosquitto_pub ~-~-help. as below:
146
147 (% class="box" %)
148 (((
149 root@dragino-1ec39c:~~# mosquitto_pub ~-~-help
150 mosquitto_pub is a simple mqtt client that will publish a message on a single topic and exit.
151 mosquitto_pub version 1.6.4 running on libmosquitto 1.6.4.
152 \\Usage: mosquitto_pub {[-h host] [-p port] [-u username] [-P password] -t topic | -L URL}
153 {-f file | -l | -n | -m message}
154 [-c] [-k keepalive] [-q qos] [-r] [~-~-repeat N] [~-~-repeat-delay time]
155 [-A bind_address]
156 [-i id] [-I id_prefix]
157 [-d] [~-~-quiet]
158 [-M max_inflight]
159 [-u username [-P password]]
160 [~-~-will-topic [~-~-will-payload payload] [~-~-will-qos qos] [~-~-will-retain]]
161 [{~-~-cafile file | ~-~-capath dir} [~-~-cert file] [~-~-key file]
162 [~-~-ciphers ciphers] [~-~-insecure]
163 [~-~-tls-alpn protocol]
164 [~-~-tls-engine engine] [~-~-keyform keyform] [~-~-tls-engine-kpass-sha1]]
165 [~-~-psk hex-key ~-~-psk-identity identity [~-~-ciphers ciphers]]
166 [~-~-proxy socks-url]
167 [~-~-property command identifier value]
168 [-D command identifier value]
169 mosquitto_pub ~-~-help
170 \\-A : bind the outgoing socket to this host/ip address. Use to control which interface
171 the client communicates over.
172 -d : enable debug messages.
173 -D : Define MQTT v5 properties. See the documentation for more details.
174 -f : send the contents of a file as the message.
175 -h : mqtt host to connect to. Defaults to localhost.
176 -i : id to use for this client. Defaults to mosquitto_pub_ appended with the process id.
177 -I : define the client id as id_prefix appended with the process id. Useful for when the
178 broker is using the clientid_prefixes option.
179 -k : keep alive in seconds for this client. Defaults to 60.
180 -L : specify user, password, hostname, port and topic as a URL in the form:
181 mqtt(s):~/~/[username[:password]@]host[:port]/topic
182 -l : read messages from stdin, sending a separate message for each line.
183 -m : message payload to send.
184 -M : the maximum inflight messages for QoS 1/2..
185 -n : send a null (zero length) message.
186 -p : network port to connect to. Defaults to 1883 for plain MQTT and 8883 for MQTT over TLS.
187 -P : provide a password
188 -q : quality of service level to use for all messages. Defaults to 0.
189 -r : message should be retained.
190 -s : read message from stdin, sending the entire input as a message.
191 -t : mqtt topic to publish to.
192 -u : provide a username
193 -V : specify the version of the MQTT protocol to use when connecting.
194 Can be mqttv5, mqttv311 or mqttv31. Defaults to mqttv311.
195 ~-~-help : display this message.
196 ~-~-repeat : if publish mode is -f, -m, or -s, then repeat the publish N times.
197 ~-~-repeat-delay : if using ~-~-repeat, wait time seconds between publishes. Defaults to 0.
198 ~-~-quiet : (% style="color:red" %)don't print error messages.
199 ~-~-will-payload : payload for the client Will, which is sent by the broker in case of
200 unexpected disconnection. If not given and will-topic is set, a zero
201 length message will be sent.
202 ~-~-will-qos : QoS level for the client Will.
203 ~-~-will-retain : if given, make the client Will retained.
204 ~-~-will-topic : the topic on which to publish the client Will.
205 ~-~-cafile : path to a file containing trusted CA certificates to enable encrypted
206 communication.
207 ~-~-capath : path to a directory containing trusted CA certificates to enable encrypted
208 communication.
209 ~-~-cert : client certificate for authentication, if required by server.
210 ~-~-key : client private key for authentication, if required by server.
211 ~-~-keyform : keyfile type, can be either "pem" or "engine".
212 ~-~-ciphers : openssl compatible list of TLS ciphers to support.
213 ~-~-tls-version : TLS protocol version, can be one of tlsv1.3 tlsv1.2 or tlsv1.1.
214 Defaults to tlsv1.2 if available.
215 ~-~-insecure : do not check that the server certificate hostname matches the remote
216 hostname. Using this option means that you cannot be sure that the
217 remote host is the server you wish to connect to and so is insecure.
218 Do not use this option in a production environment.
219 ~-~-tls-engine : If set, enables the use of a TLS engine device.
220 ~-~-tls-engine-kpass-sha1 : SHA1 of the key password to be used with the selected SSL engine.
221 ~-~-psk : pre-shared-key in hexadecimal (no leading 0x) to enable TLS-PSK mode.
222 ~-~-psk-identity : client identity string for TLS-PSK mode.
223 ~-~-proxy : SOCKS5 proxy URL of the form:
224 socks5h:~/~/[username[:password]@]hostname[:port]
225 Only "none" and "username" authentication is supported.
226 \\See https:~/~/mosquitto.org/ for more information.
227 )))
228
229
230 (((
231 and modify the /usr/bin/mqtt_process.sh script, the location to change the command option is below lines:
232 )))
233
234 (% class="box" %)
235 (((
236 //# Call MQTT Publish command
237 \\# 1. Case with User, Password and Client ID present  (e.g. Azure)
238 if [ ! -z "$pass" ] && [ ! -z "$user" ] && [ ! -z "$clientID" ]; then
239 case="1" 
240 mosquitto_pub $D -h $server -p $port -q $pub_qos -i $clientID -t $pub_topic -u $user -P "$pass" $C $cafile $PUB_FLAG "$mqtt_data"
241 \\# 2. Case with Certificate, Key and ClientID present (e.g. AWS)
242 elif [ ! -z "$certfile" ] && [ ! -z "$key" ] && [ ! -z "$clientID" ]; then
243 case="2" 
244 mosquitto_pub $D -h $server -p $port -q $pub_qos -i $clientID -t $pub_topic ~-~-cert $cert ~-~-key $key $C $cafile $PUB_FLAG "$mqtt_data"
245 \\# 3. Case with no User, Certificate or ClientID present
246 elif [ -z "$user" ] && [ -z "$certfile" ] && [ -z "$clientID" ]; then
247 case="3" 
248 mosquitto_pub $D -h $server -p $port -q $pub_qos -t $pub_topic $PUB_FLAG "$mqtt_data"
249 \\# 4. Case with no User, Certificate, but with ClientID present
250 elif [ -z "$user" ] && [ -z "$certfile" ] && [ ! -z "$clientID" ]; then
251 case="4" 
252 mosquitto_pub $D -h $server -p $port -q $pub_qos -i $clientID -t $pub_topic $PUB_FLAG "$mqtt_data"
253 \\# 5. Case with User and ClientID present, but no Password and no Certificate present
254 elif [ -z "$pass" ] && [ -z "$certfile" ] && [ ! -z "$user" ] && [ ! -z "$clientID" ]; then
255 case="5" 
256 mosquitto_pub $D -h $server -p $port -q $pub_qos -i $clientID -t $pub_topic -u $user $PUB_FLAG "$mqtt_data"
257 \\# 6. Case with User and Password present, but no ClientID and no Certificate present
258 elif [ ! -z "$user" ] && [ ! -z "$pass" ] && [ -z "$clientID" ] && [ -z "$certfile" ]; then
259 case="6" 
260 mosquitto_pub $D -h $server -p $port -q $pub_qos  -t $pub_topic -u $user -P "$pass" $PUB_FLAG "$mqtt_data"
261 \\# 0. Else - invalid parameters, just log
262 else
263 case="(% style="color:red" %)Invalid parameters" (%%)
264 logger (% style="color:red" %)"[IoT.MQTT]:Invalid Parameters - mosquitto_pub not called."(%%)
265 fi//
266 )))
267
268
269 = 4. Example to communicate to a simple MQTT server =
270
271 == 4.1 Overview ==
272
273 This section is an example to show how to set up LG01-N to communicate with a MQTT server. The MQTT server is a simple utility set up in a local PC. Note: User can set up same server via [[this instruction>>url:http://www.steves-internet-guide.com/install-mosquitto-broker/]].
274
275
276 == 4.2 Simulate via MQTT.fx utility ==
277
278 The [[MQTT.fx>>url:http://mqttfx.jensd.de/index.php/download]] is a MQTT client tool. We can use this to simulate a MQTT connection to our MQTT broker first to make sure the MQTT broker works. This will also help us understand how it works.
279
280 (((
281 In this test, the MQTT broker and MQTT.fx are installed in the same PC, so the MQTT server address in MQTT.fx should be localhost. Below shows how to connect to the server.
282 )))
283
284 [[image:image-20220527134929-6.png]]
285
286 Connect to MQTT Broker
287
288
289 After connected, use publish to public some thing to MQTT server. This to simulate upsteam
290
291 [[image:image-20220527135037-7.png]]
292
293 Upstream: Publish message to MQTT Broker
294
295
296 To simulate a downstream, use MQTT.fx to subscribe a topic, and publish something to this topic. as Below:
297
298 [[image:image-20220527135215-8.png]]
299
300 Downstream: Subscribe a topic to get downstream
301
302
303 == 4.3 Simulate via Dragino Command Line ==
304
305 For first try of MQTT connection, simulate via command line is recommend, there are many servers / connection type for MQTT. They are using different connection parameters. Simulating the connection via command line will help us rapidly connect to server and debug.
306
307 In the Dragino Gateway, we use [[mosquitto client>>url:https://mosquitto.org/]] for MQTT connection.
308
309
310 (% style="color:#4f81bd" %)**For Upstream**
311
312 command is [[mosquitto_pub>>url:https://mosquitto.org/man/mosquitto_pub-1.html]]
313
314 Example: mosquitto_pub -h 192.168.199.148 -p 1883 -t /channel/6543 -m temp=36
315
316 Note: 192.168.199.148 is MQTT broker address, the gateway and the MQTT broker PC are in the same network.
317
318 [[image:image-20220527135310-9.png]]
319
320 mosquitto_pub
321
322
323 (% style="color:#4f81bd" %)**For Downstream**
324
325 Use [[mosquitto_sub>>url:https://mosquitto.org/man/mosquitto_sub-1.html]] to subscribe the change on the topic.
326
327 [[image:image-20220527135440-10.png]]
328
329 mosquitto_sub
330
331
332 == 4.4 Configure Dragino UI for MQTT connection ==
333
334 This chapter are step by step to show to configure the Dragino Menu for MQTT auto connection.
335
336 (((
337 Go to (% style="color:#4f81bd" %)**Dragino Menu ~-~-> MQTT Client**
338 )))
339
340 [[image:https://wiki.dragino.com/images/thumb/b/bc/MQTT_Command_8.png/600px-MQTT_Command_8.png||height="249" width="600"]]
341
342 go to mqtt configure menu
343
344 (((
345 Select (% style="color:#4f81bd" %)**Forward to MQTT server**. (% style="color:red" %)**Notice**(%%): This option is removed from the latest firmware, in the latest firmware, if user submit "SAVE & APPLY" in MQTT page, the gateway will use MQTT service.
346 )))
347
348 [[image:image-20220527135742-11.png]]
349
350 forward to MQTT
351
352
353 === 4.4.1 Configure the MQTT Client for Upstream ===
354
355 Below screenshot is same as the publish command:
356
357 (% class="box" %)
358 (((
359 mosquitto_pub -h 192.168.199.148 -p 1883 -i dragino-1b7060 -t CLIENTID/CHANNEL/data -m DATA
360 ~/~/where the CLIENTID, CHANNEL & DATA are macro. represent for
361 ~/~/CLIENTID: dragino-1b7060
362 ~/~/CHANNEL: Remote ID in Channel settings; here is 78901 or 567456
363 ~/~/DATA: The data stores in /var/iot/channels/
364 )))
365
366 [[image:image-20220527135828-12.png]]
367
368 MQTT Publish configure
369
370
371 [[image:image-20220527135929-13.png]]
372
373 MQTT Channel settings
374
375
376 (((
377 For example, if we put a data(temp=46) on the file /var/iot/channels/4567, because 4567 match the remote channel 78901. the gateway will run this command:
378 )))
379
380 (% class="box" %)
381 (((
382 mosquitto_pub -h 192.168.199.148 -p 1883 -i dragino-1b7060 -t dragino-1b7060/78901/data -m temp=46
383 )))
384
385 (((
386 to MQTT broker.
387 )))
388
389
390 (((
391 Below is a simulation to put this data to active the MQTT publish.
392 )))
393
394 [[image:image-20220527140023-14.png]]
395
396 MQTT Publish
397
398
399 === 4.4.2 Configure the MQTT Client for Downstream ===
400
401 Below screen shot equal to this subscribe command:
402
403 (% class="box" %)
404 (((
405 mosquitto_sub -h 192.168.199.148 -p 1883 -i dragino-1b7060 -t command.
406 )))
407
408 [[image:image-20220527140100-15.png]]
409
410 MQTT Subscribe
411
412
413 (((
414 When MQTT broker receive a update on this topic, the gateway will get the update and use LoRa radio to broadcast this message. The LoRa parameters used for update is:
415 )))
416
417 [[image:image-20220527140148-16.png]]
418
419 LoRa Broadcast parameters.
420
421
422 And below is the subscribe simulation:
423
424 [[image:image-20220527140238-17.png]]
425
426 downstream simulation
427
428
429 == 4.5 Add LoRa support to communicate with remote sensor ==
430
431 In above section, we have configured the UI to support MQTT upstream and downstream. We can simulate via Linux command. In this section, we will guide how to communicate with remote LoRa End Node for upstream and downstream.
432
433
434 === 4.5.1 Use LoRa Raw protocol for communication ~-~- For LG01/LG02 ===
435
436 (((
437 We can use [[LoRa Shield>>url:http://www.dragino.com/products/lora/item/102-lora-shield.html]] to send LoRa Raw data to Gateway and receive data from gateway.
438 )))
439
440 (((
441 The example Sketch for LoRa Shield +Arduino is here: [[LoRa_Shield_Sketch_For_MQTT>>url:http://www.dragino.com/downloads/index.php?dir=LoraShield/]]
442 )))
443
444 (((
445 And this link is the required library: [[arduino-LoRa-master>>url:http://www.dragino.com/downloads/index.php?dir=LoraShield/]]. Unzip this library and put in Arduino library location.
446 )))
447
448 (((
449 What does the Arduino Sketch do? The Arduino Sketch will:
450 )))
451
452 * Upstream: Keep sending a LoRa Message every minutes with this payload : <4567>tem=xx&hum=yy (Where xx and yy are temperature and humidity value generated randomly).
453 * Downstream: Listening broadcast message from gateway, and print it in console.
454 * The LoRa parameter settings in Arduino should match the LoRa settings in gateway, as below:
455
456 [[image:image-20220527140354-18.png]]
457
458 LoRa Parameter should match
459
460
461 Below is the test result after the Arduino Sketch is running.
462
463 [[image:image-20220527140459-19.png]]
464
465 Upstream Data Flow
466
467
468 [[image:image-20220527140542-20.png]]
469
470 Downstream Data Flow
471
472
473 === 4.5.2 Use LoRaWAN Protocol for communication ~-~- For LG308/LPS8/DLOS8 ===
474
475 (((
476 Since firmware (% style="color:#4f81bd" %)**LG02_LG08~-~-build-v5.3.1585192026-20200326-1109,**(%%) Dragino LoRaWAN gateways support the communication to LoRaWAN (% style="color:#4f81bd" %)**ABP end node**(%%) locally without the need of LoRaWAN server. This feature allow us to integrate MQTT in the gateway to support LoRaWAN to MQTT forwarding or visa verse.
477 )))
478
479 (((
480 When use test this feature, please use the version higher then : (% style="color:#4f81bd" %)**LG02_LG08~-~-build-v5.4.1593400722-20200629-1120**(%%), in this version, the upload format is changed and readable, which is easier for integration.
481 (% style="color:#4f81bd" %)**Video Instruction**(%%): (% style="color:blue" %)[[https:~~/~~/youtu.be/qJTY441-t90>>url:https://youtu.be/qJTY441-t90]]
482 )))
483
484 (((
485 (% style="color:#4f81bd" %)**Step 1**(%%): Refer [[Communicate with ABP End Node>>Communicate with ABP End Node without LoRaWAN Network Server --- LG308]] to know how to set up LG308 to work with LoRaWAN End node.
486 )))
487
488 (((
489 (% style="color:#4f81bd" %)**Step 2**(%%): Make sure your Radio settings match the End Node settings.
490 )))
491
492 [[image:image-20220527141235-21.png]]
493
494 Use Same Frequency Band as End Node
495
496
497 (((
498 (((
499 (% style="color:#4f81bd" %)**Step 3**(%%): Set up publish format and MQTT channel. The LG308 will store the Data from End node in (% style="color:#4f81bd" %)**HEX format**(%%) in the file. And we need to config the format to (% style="color:#4f81bd" %)**META**
500 )))
501 )))
502
503 [[image:image-20220527141450-22.png]]
504
505 Publish mush use META as data format for LG308
506
507
508 (% style="color:#4f81bd" %)**Step 4**(%%): Map the Device Address to Remote ID in MQTT server.
509
510 [[image:image-20220527141727-23.png]]
511
512 Map Dev Addr to remote ID
513
514
515 (% style="color:#4f81bd" %)**Step 5: Upstream**(%%): Save the change, we can see the log info via "sytem log", End Node and MQTT Server
516
517 [[image:image-20220527141843-24.png]]
518
519 Upstream Process
520
521
522 [[image:image-20220527141933-25.png]]
523
524 Choose ASCII Format
525
526
527 [[image:image-20220527142028-26.png]]
528
529 LHT65 Decoder
530
531
532 (% style="color:#4f81bd" %)**Step 6: Set up subscribe**(%%)**:** Subscribe a topci for downstream.
533
534 [[image:image-20220527142115-27.png]]
535
536 Subscribe to a topic
537
538
539 (% style="color:#4f81bd" %)**Step 7: Downstream**(%%): Save the change, we can see the log info via "sytem log", End Node and MQTT Server.
540
541 [[image:image-20220527142239-28.png]]
542
543 Downstream Flow
544
545 (% style="color:red" %)**Notice: The text use for Downstream must meet the requirement from [[LG308 Downstream Payload>>Communicate with ABP End Node without LoRaWAN Network Server --- LG308||anchor="H2.2Downstream"]]**
546
547
548 = 5. How to Debug =
549
550 User can login the gateway's console and run (% style="color:#4f81bd" %)**logread -f**(%%). It will shows the output when there is packet arrive.User can see if it is correct.
551
552
553 = 6. How to ask for Support =
554
555 If a user still not have trouble making it works. please send a mail to support@dragino.com with the below info:
556
557 * Detail of your LoRaWAN end node.
558 * Gateway Model and firmware version
559 * A set of screenshots you configure in the gateway according to our instruction
560 * A full log of "logread -f"