<
From version < 53.1 >
edited by Kilight Cao
on 2024/08/31 16:00
To version < 51.1 >
edited by Xiaoling
on 2024/08/19 15:00
>
Change comment: There is no comment for this version

Summary

Details

Page properties
Author
... ... @@ -1,1 +1,1 @@
1 -XWiki.Kilight
1 +XWiki.Xiaoling
Content
... ... @@ -14,28 +14,278 @@
14 14  
15 15  This MQTT forward instruction is for below devices:
16 16  
17 +* Firmware Version > LG02_LG08-5.3.1580178039 [[Firmware Download>>url:http://www.dragino.com/downloads/index.php?dir=LoRa_Gateway/LPS8/Firmware/Release/]]
17 17  * LG01N, OLG01N ((% style="color:red" %)**Warning**(%%): LG01-P LG01-S use another instruction: [[MQTT for LG01-P/LG01S>>doc:Main.Through MQTT to upload data.WebHome]])
18 18  * LG02, OLG02
19 -* LG308, DLOS8, LPS8, LIG16
20 +* LG308, DLOS8
21 +* LPS8
22 +* LIG16
20 20  * MS14 series if installed with the same firmware. (in this case, the MQTT forward will work , but no LoRa support)
21 -* LG308N, DLOS8N, LPS8N
22 -* LPS8V2
23 23  
24 24  
25 -The MQTT Client Utility used in Dragino is **mosquitto_pub** and **mosquitto_sub**. User can add more options to the mqtt commands.
26 += 2.  Firmware Change Log for MQTT feature =
26 26  
27 -Check out this link for more mosquito-related directives **[[https:~~/~~/mosquitto.org/man/mosquitto_pub-1.html>>https://mosquitto.org/man/mosquitto_pub-1.html]]**
28 28  
29 +(((
30 +This instruction is wrote start from LG02_LG08-5.3.1580178039. Below is related change log since this version of firmware.
31 +)))
29 29  
30 -= 2.  Example to communicate to a simple MQTT server =
33 +* LG02_LG08-5.3.1580178039
34 +** Initiate version
31 31  
32 -== 2.1  Overview ==
33 33  
37 += 3.  MQTT forward operating principle =
34 34  
39 +== 3.1  Network Structure ==
40 +
41 +
42 +Below shows the network structure for MQTT forwarding.
43 +
44 +* **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.
45 +* **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.
46 +
47 +[[image:image-20220527133547-1.png]]
48 +
49 +General MQTT structure
50 +
51 +
52 +== 3.2  How sensor data is forwarded ==
53 +
54 +
55 +In this MQTT forward feature, the key point is how the gateway process the sensor data.
56 +
57 +
58 +=== 3.2.1  Upstream ===
59 +
60 +
61 +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.
62 +
63 +(((
64 +The data flow works as below diagram.
65 +)))
66 +
67 +[[image:image-20220527134000-2.png]]
68 +
69 +(((
70 +Upstream path
71 +
72 +
73 +)))
74 +
75 +(((
76 +(% style="color:red" %)**Note: The sensor data can base or LoRa or other method, as long as there are data on the file /var/iot/channels. /span>**
77 +)))
78 +
79 +(((
80 +
81 +)))
82 +
83 +=== 3.2.2  Downstream ===
84 +
85 +
86 +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.
87 +
88 +(((
89 +Below are the data flow for downstream.
90 +)))
91 +
92 +[[image:image-20220527134038-3.png]]
93 +
94 +Downstream path
95 +
96 +
97 +== 3.3  Macro Definition ==
98 +
99 +
100 +The MQTT publish command use Macro settings to generate flexible upstream payload for MQTT publish.
101 +
102 +(((
103 +Currently the (% style="color:#4f81bd" %)**-t (topic)**(%%) and (% style="color:#4f81bd" %)**-m (message)**(%%) support Macros.
104 +
105 +
106 +)))
107 +
108 +=== 3.3.1  -t topic macro ===
109 +
110 +
111 +* CHANNEL: Remote Channel ID
112 +* CLIENTID: Client ID , Same as -i
113 +* WRITE_API: Remote Channel Write API
114 +* USERNAME: User ID (-u)
115 +* HOSTNAME: Device Hostname
116 +
117 +
118 +=== 3.3.2  -m message macro ===
119 +
120 +
121 +* HOSTNAME: Device Hostname
122 +* CHANNEL: Remote Channel ID
123 +* DATA: Sensor Data without time stamp and rssi
124 +* META: Completely sensor data with time stamp and rssi
125 +* JSON: Convert META to json format.
126 +
127 +
128 +=== 3.3.3  Example for Macro ===
129 +
130 +
131 +[[image:image-20220527134251-4.png]]
132 +
133 +MQTT Publish configure
134 +
135 +
136 +**Above screen shots shows below format:**
137 +
138 +* -t: CLIENTID/CHANNEL/data
139 +* -m: DATA
140 +
141 +When there is a LoRa sensor arrive. it will be store at the /var/iot/channels as below:
142 +
143 +[[image:image-20220527134332-5.png]]
144 +
145 +Sensor Data
146 +
147 +
148 +(((
149 +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.
150 +)))
151 +
152 +
153 +== 3.4  Modify the MQTT to support more options ==
154 +
155 +
156 +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:
157 +
158 +(% class="box" %)
159 +(((
160 +root@dragino-1ec39c:~~# mosquitto_pub ~-~-help
161 +mosquitto_pub is a simple mqtt client that will publish a message on a single topic and exit.
162 +mosquitto_pub version 1.6.4 running on libmosquitto 1.6.4.
163 +\\Usage: mosquitto_pub {[-h host] [-p port] [-u username] [-P password] -t topic | -L URL}
164 + {-f file | -l | -n | -m message}
165 + [-c] [-k keepalive] [-q qos] [-r] [~-~-repeat N] [~-~-repeat-delay time]
166 + [-A bind_address]
167 + [-i id] [-I id_prefix]
168 + [-d] [~-~-quiet]
169 + [-M max_inflight]
170 + [-u username [-P password]]
171 + [~-~-will-topic [~-~-will-payload payload] [~-~-will-qos qos] [~-~-will-retain]]
172 + [{~-~-cafile file | ~-~-capath dir} [~-~-cert file] [~-~-key file]
173 + [~-~-ciphers ciphers] [~-~-insecure]
174 + [~-~-tls-alpn protocol]
175 + [~-~-tls-engine engine] [~-~-keyform keyform] [~-~-tls-engine-kpass-sha1]]
176 + [~-~-psk hex-key ~-~-psk-identity identity [~-~-ciphers ciphers]]
177 + [~-~-proxy socks-url]
178 + [~-~-property command identifier value]
179 + [-D command identifier value]
180 + mosquitto_pub ~-~-help
181 +\\-A : bind the outgoing socket to this host/ip address. Use to control which interface
182 + the client communicates over.
183 +-d : enable debug messages.
184 +-D : Define MQTT v5 properties. See the documentation for more details.
185 +-f : send the contents of a file as the message.
186 +-h : mqtt host to connect to. Defaults to localhost.
187 +-i : id to use for this client. Defaults to mosquitto_pub_ appended with the process id.
188 +-I : define the client id as id_prefix appended with the process id. Useful for when the
189 + broker is using the clientid_prefixes option.
190 +-k : keep alive in seconds for this client. Defaults to 60.
191 +-L : specify user, password, hostname, port and topic as a URL in the form:
192 + mqtt(s):~/~/[username[:password]@]host[:port]/topic
193 +-l : read messages from stdin, sending a separate message for each line.
194 +-m : message payload to send.
195 +-M : the maximum inflight messages for QoS 1/2..
196 +-n : send a null (zero length) message.
197 +-p : network port to connect to. Defaults to 1883 for plain MQTT and 8883 for MQTT over TLS.
198 +-P : provide a password
199 +-q : quality of service level to use for all messages. Defaults to 0.
200 +-r : message should be retained.
201 +-s : read message from stdin, sending the entire input as a message.
202 +-t : mqtt topic to publish to.
203 +-u : provide a username
204 +-V : specify the version of the MQTT protocol to use when connecting.
205 + Can be mqttv5, mqttv311 or mqttv31. Defaults to mqttv311.
206 +~-~-help : display this message.
207 +~-~-repeat : if publish mode is -f, -m, or -s, then repeat the publish N times.
208 +~-~-repeat-delay : if using ~-~-repeat, wait time seconds between publishes. Defaults to 0.
209 +~-~-quiet : (% style="color:red" %)don't print error messages.
210 +~-~-will-payload : payload for the client Will, which is sent by the broker in case of
211 + unexpected disconnection. If not given and will-topic is set, a zero
212 + length message will be sent.
213 +~-~-will-qos : QoS level for the client Will.
214 +~-~-will-retain : if given, make the client Will retained.
215 +~-~-will-topic : the topic on which to publish the client Will.
216 +~-~-cafile : path to a file containing trusted CA certificates to enable encrypted
217 + communication.
218 +~-~-capath : path to a directory containing trusted CA certificates to enable encrypted
219 + communication.
220 +~-~-cert : client certificate for authentication, if required by server.
221 +~-~-key : client private key for authentication, if required by server.
222 +~-~-keyform : keyfile type, can be either "pem" or "engine".
223 +~-~-ciphers : openssl compatible list of TLS ciphers to support.
224 +~-~-tls-version : TLS protocol version, can be one of tlsv1.3 tlsv1.2 or tlsv1.1.
225 + Defaults to tlsv1.2 if available.
226 +~-~-insecure : do not check that the server certificate hostname matches the remote
227 + hostname. Using this option means that you cannot be sure that the
228 + remote host is the server you wish to connect to and so is insecure.
229 + Do not use this option in a production environment.
230 +~-~-tls-engine : If set, enables the use of a TLS engine device.
231 +~-~-tls-engine-kpass-sha1 : SHA1 of the key password to be used with the selected SSL engine.
232 +~-~-psk : pre-shared-key in hexadecimal (no leading 0x) to enable TLS-PSK mode.
233 +~-~-psk-identity : client identity string for TLS-PSK mode.
234 +~-~-proxy : SOCKS5 proxy URL of the form:
235 + socks5h:~/~/[username[:password]@]hostname[:port]
236 + Only "none" and "username" authentication is supported.
237 +\\See https:~/~/mosquitto.org/ for more information.
238 +)))
239 +
240 +
241 +(((
242 +and modify the /usr/bin/mqtt_process.sh script, the location to change the command option is below lines:
243 +)))
244 +
245 +(% class="box" %)
246 +(((
247 +//# Call MQTT Publish command
248 +\\# 1. Case with User, Password and Client ID present  (e.g. Azure)
249 +if [ ! -z "$pass" ] && [ ! -z "$user" ] && [ ! -z "$clientID" ]; then
250 +case="1" 
251 +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"
252 +\\# 2. Case with Certificate, Key and ClientID present (e.g. AWS)
253 +elif [ ! -z "$certfile" ] && [ ! -z "$key" ] && [ ! -z "$clientID" ]; then
254 +case="2" 
255 +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"
256 +\\# 3. Case with no User, Certificate or ClientID present
257 +elif [ -z "$user" ] && [ -z "$certfile" ] && [ -z "$clientID" ]; then
258 +case="3" 
259 +mosquitto_pub $D -h $server -p $port -q $pub_qos -t $pub_topic $PUB_FLAG "$mqtt_data"
260 +\\# 4. Case with no User, Certificate, but with ClientID present
261 +elif [ -z "$user" ] && [ -z "$certfile" ] && [ ! -z "$clientID" ]; then
262 +case="4" 
263 +mosquitto_pub $D -h $server -p $port -q $pub_qos -i $clientID -t $pub_topic $PUB_FLAG "$mqtt_data"
264 +\\# 5. Case with User and ClientID present, but no Password and no Certificate present
265 +elif [ -z "$pass" ] && [ -z "$certfile" ] && [ ! -z "$user" ] && [ ! -z "$clientID" ]; then
266 +case="5" 
267 +mosquitto_pub $D -h $server -p $port -q $pub_qos -i $clientID -t $pub_topic -u $user $PUB_FLAG "$mqtt_data"
268 +\\# 6. Case with User and Password present, but no ClientID and no Certificate present
269 +elif [ ! -z "$user" ] && [ ! -z "$pass" ] && [ -z "$clientID" ] && [ -z "$certfile" ]; then
270 +case="6" 
271 +mosquitto_pub $D -h $server -p $port -q $pub_qos  -t $pub_topic -u $user -P "$pass" $PUB_FLAG "$mqtt_data"
272 +\\# 0. Else - invalid parameters, just log
273 +else
274 +case="(% style="color:red" %)Invalid parameters" (%%)
275 +logger (% style="color:red" %)"[IoT.MQTT]:Invalid Parameters - mosquitto_pub not called."(%%)
276 +fi//
277 +)))
278 +
279 +
280 += 4.  Example to communicate to a simple MQTT server =
281 +
282 +== 4.1  Overview ==
283 +
284 +
35 35  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/]].
36 36  
37 37  
38 -== 2.2  Simulate via MQTT.fx utility ==
288 +== 4.2  Simulate via MQTT.fx utility ==
39 39  
40 40  
41 41  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.
... ... @@ -63,27 +63,158 @@
63 63  Downstream: Subscribe a topic to get downstream
64 64  
65 65  
316 +== 4.3  Simulate via Dragino Command Line ==
66 66  
67 -= 3. Add LoRa support to communicate with remote sensor =
68 68  
319 +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.
69 69  
321 +In the Dragino Gateway, we use [[mosquitto client>>url:https://mosquitto.org/]] for MQTT connection.
322 +
323 +
324 +(% style="color:blue" %)**For Upstream**
325 +
326 +command is [[mosquitto_pub>>url:https://mosquitto.org/man/mosquitto_pub-1.html]]
327 +
328 +Example: mosquitto_pub -h 192.168.199.148 -p 1883 -t /channel/6543 -m temp=36
329 +
330 +Note: 192.168.199.148 is MQTT broker address, the gateway and the MQTT broker PC are in the same network.
331 +
332 +[[image:image-20220527135310-9.png]]
333 +
334 +mosquitto_pub
335 +
336 +
337 +(% style="color:blue" %)**For Downstream**
338 +
339 +Use [[mosquitto_sub>>url:https://mosquitto.org/man/mosquitto_sub-1.html]] to subscribe the change on the topic.
340 +
341 +[[image:image-20220527135440-10.png]]
342 +
343 +mosquitto_sub
344 +
345 +
346 +== 4.4  Configure Dragino UI for MQTT connection ==
347 +
348 +
349 +This chapter are step by step to show to configure the Dragino Menu for MQTT auto connection.
350 +
351 +(((
352 +Go to (% style="color:#4f81bd" %)**Dragino Menu ~-~-> MQTT Client**
353 +)))
354 +
355 +[[image:image-20220714155901-1.png]]
356 +
357 +go to mqtt configure menu
358 +
359 +
360 +(((
361 +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.
362 +)))
363 +
364 +[[image:image-20220527135742-11.png]]
365 +
366 +forward to MQTT
367 +
368 +
369 +=== 4.4.1  Configure the MQTT Client for Upstream ===
370 +
371 +
372 +**Below screenshot is same as the publish command:**
373 +
374 +(% class="box" %)
375 +(((
376 +**mosquitto_pub -h 192.168.199.148 -p 1883 -i dragino-1b7060 -t CLIENTID/CHANNEL/data -m DATA
377 +~/~/where the CLIENTID, CHANNEL & DATA are macro. represent for
378 +~/~/CLIENTID: dragino-1b7060
379 +~/~/CHANNEL: Remote ID in Channel settings; here is 78901 or 567456
380 +~/~/DATA: The data stores in /var/iot/channels/**
381 +)))
382 +
383 +
384 +[[image:image-20220527135828-12.png]]
385 +
386 +MQTT Publish configure
387 +
388 +
389 +[[image:image-20220527135929-13.png]]
390 +
391 +MQTT Channel settings
392 +
393 +
394 +(((
395 +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:
396 +)))
397 +
398 +(% class="box" %)
399 +(((
400 +mosquitto_pub -h 192.168.199.148 -p 1883 -i dragino-1b7060 -t dragino-1b7060/78901/data -m temp=46
401 +)))
402 +
403 +(((
404 +to MQTT broker.
405 +)))
406 +
407 +
408 +(((
409 +Below is a simulation to put this data to active the MQTT publish.
410 +)))
411 +
412 +[[image:image-20220527140023-14.png]]
413 +
414 +MQTT Publish
415 +
416 +
417 +=== 4.4.2  Configure the MQTT Client for Downstream ===
418 +
419 +
420 +Below screen shot equal to this subscribe command:
421 +
422 +(% class="box" %)
423 +(((
424 +mosquitto_sub -h 192.168.199.148 -p 1883 -i dragino-1b7060 -t command.
425 +)))
426 +
427 +[[image:image-20220527140100-15.png]]
428 +
429 +MQTT Subscribe
430 +
431 +
432 +(((
433 +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:
434 +)))
435 +
436 +[[image:image-20220527140148-16.png]]
437 +
438 +LoRa Broadcast parameters.
439 +
440 +
441 +And below is the subscribe simulation:
442 +
443 +[[image:image-20220527140238-17.png]]
444 +
445 +downstream simulation
446 +
447 +
448 +== 4.5  Add LoRa support to communicate with remote sensor ==
449 +
450 +
70 70  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.
71 71  
72 72  
73 73  
74 -== 3.1  Use LoRa Raw protocol for communication ~-~- For LG01/LG02 ==
455 +=== 4.5.1  Use LoRa Raw protocol for communication ~-~- For LG01/LG02 ===
75 75  
76 76  
77 77  (((
78 -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.
459 +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.
79 79  )))
80 80  
81 81  (((
82 -The example Sketch for LoRa Shield +Arduino is here: **[[LoRa_Shield_Sketch_For_MQTT>>url:http://www.dragino.com/downloads/index.php?dir=LoraShield/]]**
463 +The example Sketch for LoRa Shield +Arduino is here: [[LoRa_Shield_Sketch_For_MQTT>>url:http://www.dragino.com/downloads/index.php?dir=LoraShield/]]
83 83  )))
84 84  
85 85  (((
86 -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.
467 +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.
87 87  
88 88  
89 89  )))
... ... @@ -113,7 +113,7 @@
113 113  Downstream Data Flow
114 114  
115 115  
116 -== 3.2  Use LoRaWAN Protocol for communication ~-~- For LG308/LPS8/DLOS8/LG308N/LPS8N/DLOS8N ==
497 +=== 4.5.2  Use LoRaWAN Protocol for communication ~-~- For LG308/LPS8/DLOS8 ===
117 117  
118 118  
119 119  (((
... ... @@ -179,7 +179,9 @@
179 179  [[image:image-20220527142239-28.png]]
180 180  
181 181  
563 +=== 4.5.3 MQTT Downstream format ===
182 182  
565 +
183 183  mosquitto_pub -h $server_address -p $server_port -t $Client_ID -m "**dev_addr,imme/time,txt/hex,payload**"
184 184  
185 185  (% class="box infomessage" %)
... ... @@ -194,21 +194,26 @@
194 194  **mosquitto_pub -h 10.130.2.192 -p 1883 -t dragino-1d25dc/ -m "260211D,time,txt,hello,20,1,SF12,923300000,2"**
195 195  )))
196 196  
197 -(% 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"]]**
198 -
199 199  Or use [[MQTT.fx>>https://mqttfx.jensd.de/index.php/download]]
200 200  
201 201  [[image:image-20220613192816-6.png||height="440" width="1056"]]
202 202  
203 203  
204 -== 3.3 Use LoRaWAN Protocol for communication ~-~- For LPS8V2 ==
585 +(% 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"]]**
205 205  
206 206  
588 +Check out this link for more mosquito-related directives [[https:~~/~~/mosquitto.org/man/mosquitto_pub-1.html>>https://mosquitto.org/man/mosquitto_pub-1.html]]
207 207  
208 208  
209 -= 4.  How to ask for Support =
591 += 5.  How to Debug =
210 210  
211 211  
594 +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.
595 +
596 +
597 += 6.  How to ask for Support =
598 +
599 +
212 212  If a user still not have trouble making it works. please send a mail to support@dragino.com with the below info:
213 213  
214 214  * Detail of your LoRaWAN end node.
image-20240831160038-1.png
Author
... ... @@ -1,1 +1,0 @@
1 -XWiki.Kilight
Size
... ... @@ -1,1 +1,0 @@
1 -151.6 KB
Content
Copyright ©2010-2022 Dragino Technology Co., LTD. All rights reserved
Dragino Wiki v2.0