Hide last authors
Xiaoling 50.2 1 **Table of Contents: **
Xiaoling 44.2 2
Xiaoling 1.4 3 {{toc/}}
Xiaoling 1.1 4
Xiaoling 1.4 5
6
7
Xiaoling 1.2 8 = 1. What is MQTT API? =
Xiaoling 1.1 9
10
Xiaoling 50.2 11 MQTT is a publish/subscribe model that runs over TCP/IP sockets or WebSockets. MQTT over WebSockets can be secured with SSL.
Xiaoling 1.1 12
Xiaoling 50.2 13
14
Xiaoling 1.2 15 = 2. MQTT Features =
Xiaoling 1.1 16
Xiaoling 50.2 17
Xiaoling 1.2 18 The MQTT protocol runs on TCP / IP or other network protocols and provides an ordered, lossless, bidirectional connection. Features include:
Xiaoling 1.1 19
Xiaoling 1.2 20 * The publish / subscribe messaging pattern used, which provides one-to-many messaging for decoupling from the application.
21 * Message-masking mechanism for payload content.
22 * There are three Quality of Service (QoS) for transmitting messages:
23 * At most, messages of this level may be lost or duplicated, and message postings depend on the underlying TCP / IP network. That is: <= 1
24 * At one time, this level will ensure that the message arrives, but the message may be repeated. That is:> = 1
25 * Only once, make sure the message arrives only once. That is: = 1. In some demanding billing systems, you can use this level Data transfer and protocol exchange is minimized (protocol header only 2 bytes) to reduce network traffic
26 * Notification mechanism, notify the transmission of both sides when an exception occurs
Xiaoling 1.1 27
28
Xiaoling 44.2 29
Xiaoling 50.2 30
31
Xiaoling 1.2 32 = 3. What does this example shows? How it works? =
Xiaoling 1.1 33
Xiaoling 50.2 34
Xiaoling 43.3 35 (((
Xiaoling 1.2 36 The server we use here is ThingSpeak which has an intuitive chart to show the test result for our test.The ThingSpeak IoT service now supports MQTT subscriptions to receive instant updates when [[ThingSpeak>>url:https://thingspeak.com/]] channel gets updated. The method here is general and can be used with other IoT servers for MQTT connection as well.
Xiaoling 50.2 37
38
Xiaoling 43.3 39 )))
Xiaoling 1.1 40
Xiaoling 3.2 41 [[image:image-20220531135402-1.png]]
Xiaoling 1.1 42
Xiaoling 1.2 43 Message Queuing Telemetry Transport
Xiaoling 1.1 44
45
Xiaoling 3.2 46 [[image:image-20220531135449-2.png]]
47
Xiaoling 1.2 48 working principle
Xiaoling 1.1 49
Xiaoling 3.2 50
Xiaoling 50.2 51
Xiaoling 4.2 52 [[image:image-20220531135521-3.png]]
Xiaoling 1.2 53
Xiaoling 43.3 54 (((
Xiaoling 1.2 55 Schematic diagram: uplink.In this section, we will try to program LG01 to uplink data to ThingSpeak. The data flow in this example.We have already tried ① and ② in the above simple LoRa example. Now we will try the step ③ first, after it work as expect, we will integrate these three steps together for a complete uplink example.
Xiaoling 43.3 56 )))
Xiaoling 1.2 57
Xiaoling 4.2 58
59
Xiaoling 5.2 60 [[image:image-20220531135607-4.png]]
Xiaoling 1.2 61
Xiaoling 50.2 62
Xiaoling 43.3 63 (((
Xiaoling 1.2 64 Schematic diagram:downlink.In this section, we will try to program LG01 to fetch download data from ThingSpeak, then broadcast this data to local LoRa network. The end node will get this message and check if they need to do something. Similar with Uplink Example, we will first try to do it in PC, then do it in Linux side, and finally integrate it with LoRa.
Xiaoling 43.3 65 )))
Xiaoling 1.2 66
Xiaoling 43.3 67 (((
Xiaoling 1.2 68 MQTT agreement to achieve the need: client and server There are three identities in the MQTT protocol: Publish, Broker (server), Subscribe. Among them, the publisher and the subscriber of the message are all clients, the broker is the server, and the publisher can be both the subscriber and the subscriber. MQTT transmission of information is divided into: Topic and payload in two parts Topic, can be understood as the type of message subscribers Subscribe, you will receive the theme of the message content (payload) The payload, which can be understood as the content of the message, refers to the specific content to be used by the subscribers.
Xiaoling 43.3 69 )))
Xiaoling 1.2 70
Xiaoling 43.3 71 (((
Xiaoling 1.2 72 To use the server, we need to register an account on [[ThingSpeak>>url:https://thingspeak.com/]] . Then create a channel and type the channel info. As shown below, the Channel ID is the unique ID to store our data in [[ThingSpeak>>url:https://thingspeak.com/]] .
Xiaoling 43.3 73 )))
Xiaoling 1.2 74
75
Xiaoling 44.3 76
Xiaoling 1.2 77 = 4. Preparation =
78
Xiaoling 50.2 79
Xiaoling 1.2 80 == 4.1 Hardware ==
81
Xiaoling 50.2 82
Xiaoling 6.2 83 1. [[image:image-20220531135650-5.png]](((
Xiaoling 50.2 84
85
Xiaoling 1.2 86 Listening on the LoRa wireless channel, while there is new LoRa packet arrives, parse it and send out to IoT Server.
Xiaoling 6.2 87
88
89
Xiaoling 1.2 90 )))
Xiaoling 7.2 91 1. [[image:image-20220531135750-6.png]](((
Xiaoling 50.2 92
93
Xiaoling 1.2 94 The LoRa End node keeps getting temperature and humidity from the sensor and sends out via LoRa periodically.
Xiaoling 50.2 95
96
97
Xiaoling 1.2 98 )))
99
100 == 4.2 Software ==
101
Xiaoling 50.2 102
Xiaoling 1.2 103 1. [[4.3.4 version>>url:http://www.dragino.com/downloads/index.php?dir=motherboards/ms14/Firmware/IoT/]]
104 1. [[MQTT_Client>>url:https://github.com/dragino/Arduino-Profile-Examples/tree/master/libraries/Dragino/examples/IoTServer/ThingSpeak/MQTT_Client]]
105 1. [[MQTT_Simple_Server MQTT_Simeple_Server>>url:https://github.com/dragino/Arduino-Profile-Examples/tree/master/libraries/Dragino/examples/LoRa/MQTT_Simple_Server]]
106 1. [[DHTlib>>url:https://github.com/goodcheney/Lora/blob/patch-1/Lora%20Shield/Examples/DHTlib.zip]]
107
Xiaoling 44.4 108
109
Xiaoling 50.2 110
111
Xiaoling 1.2 112 == 4.3 Server(For example) ==
113
Xiaoling 50.2 114
Xiaoling 1.2 115 * [[ThingSpeak>>url:https://thingspeak.com/]]
116
Xiaoling 44.5 117
118
Xiaoling 50.2 119
120
Xiaoling 1.3 121 == 4.4 Configure IoT Server ==
Xiaoling 1.2 122
Xiaoling 50.2 123
Xiaoling 1.2 124 * Need this keys:
125
Xiaoling 9.2 126 1. [[image:image-20220531140054-8.png]](((
Xiaoling 1.2 127 Channel ID,Write API Key
Xiaoling 8.3 128
129
130
Xiaoling 1.2 131 )))
Xiaoling 14.2 132 1. [[image:image-20220531140326-13.png]](((
Xiaoling 50.2 133
134
Xiaoling 1.2 135 Account->My Profile,MQTT API Key
Xiaoling 10.2 136
137
138
Xiaoling 1.2 139 )))
140 1. (((
Xiaoling 14.2 141 [[image:image-20220531140304-12.png]]
Xiaoling 44.6 142
143
144
Xiaoling 1.2 145 )))
146
Xiaoling 1.3 147 == 4.5 Configure LG01 settings ==
Xiaoling 1.2 148
Xiaoling 44.7 149
Xiaoling 50.2 150 Tips:  Please confirm that your LG01 version is 4.3.3.
Xiaoling 1.2 151
Xiaoling 50.2 152
Xiaoling 14.2 153 [[image:image-20220531140248-11.png]]
Xiaoling 1.2 154
Xiaoling 19.3 155 **Overview**
Xiaoling 1.2 156
Xiaoling 14.3 157
158
Xiaoling 15.2 159 [[image:image-20220531140425-14.png]]
Xiaoling 1.2 160
Xiaoling 19.3 161 **Senosor->IOT Server**
Xiaoling 1.2 162
Xiaoling 15.2 163
Xiaoling 50.2 164
Xiaoling 16.2 165 [[image:image-20220531140458-15.png]]
Xiaoling 1.2 166
Xiaoling 19.3 167 **MQTT->Configure MQTT Server**
Xiaoling 1.2 168
Xiaoling 16.2 169
170
Xiaoling 17.2 171 [[image:image-20220531140538-16.png]]
Xiaoling 1.2 172
Xiaoling 19.3 173 **MQTT->MQTT Channel**
Xiaoling 1.2 174
Xiaoling 17.2 175
176
Xiaoling 18.2 177 [[image:image-20220531140611-17.png]]
Xiaoling 1.2 178
Xiaoling 19.3 179 **Sensor->LoRa/LoraWAN**
Xiaoling 1.2 180
Xiaoling 18.2 181
182
Xiaoling 19.2 183 [[image:image-20220531140651-18.png]]
Xiaoling 1.2 184
Xiaoling 50.2 185
Xiaoling 19.3 186 Configure network access.**Network->Internet Access**
Xiaoling 1.2 187
188
Xiaoling 50.2 189
Xiaoling 1.3 190 == 4.6 Try MQTT API call with LG01 Linux command ==
Xiaoling 1.2 191
Xiaoling 50.2 192
Xiaoling 1.3 193 === 4.6.1 Input the command at the console ===
Xiaoling 1.2 194
Xiaoling 50.2 195
Xiaoling 21.2 196 1. [[image:image-20220531140811-19.png]](((
Xiaoling 50.2 197
198
Xiaoling 1.2 199 Open this Application.
Xiaoling 21.2 200
Xiaoling 50.2 201
Xiaoling 21.2 202
Xiaoling 1.2 203 )))
Xiaoling 21.2 204 1. [[image:image-20220531140842-20.png]](((
Xiaoling 50.2 205
206
Xiaoling 1.2 207 Input passward(dragino).After into the console.
Xiaoling 21.2 208
Xiaoling 50.2 209
Xiaoling 21.2 210
Xiaoling 1.2 211 )))
Xiaoling 22.2 212 1. [[image:image-20220531140915-21.png]](((
Xiaoling 50.2 213
214
Xiaoling 1.2 215 First, we need to make sure the LG01 has internet access. We can log in the SSH and ping an Internet address and see if it get through. As below
Xiaoling 22.3 216
Xiaoling 50.2 217
Xiaoling 22.3 218
Xiaoling 1.2 219 )))
Xiaoling 23.2 220 1. [[image:image-20220531140955-22.png]](((
Xiaoling 50.2 221
222
Xiaoling 1.2 223 LG01 has built-in Linux tool mosquitto. It is a very powerful tool for http communication. We can use this tool to handle MQTT API call in LG01. Input this command and replace your keys.
Xiaoling 23.2 224
225
226
Xiaoling 1.2 227 )))
Xiaoling 24.2 228 1. [[image:image-20220531141026-23.png]](((
Xiaoling 50.2 229
230
Xiaoling 1.2 231 Go to Server check out result.We success to use LG01 to uplink data to ThingSpeak,the mosquitto_pub command is executed in the Linux side, finally, we will have to call mosquitto_pub command with sensor data variable in Arduino side. This is through the process class in Arduino and we will show it in the final sketch.
Xiaoling 50.2 232
233
Xiaoling 1.2 234 )))
235
236 * This command:
237
Xiaoling 24.3 238 (% class="box infomessage" %)
239 (((
240 **mosquitto_pub -h mqtt.thingspeak.com -p 1883 -u dragino -P UZ4NGHKJMKS9WR5E  -i dragino_Client -t channels/399171/publish/A4NNF6G8G63JOIUV -m "field1=23.5&field2=57.5&status=MQTTPUBLISH"**
241 )))
Xiaoling 1.2 242
243 * Need replace:
244
Xiaoling 24.3 245 (% class="box infomessage" %)
246 (((
247 **UZ4NGHKJMKS9WR5E  ~/~/MQTT API
248 399171            ~/~/Channel ID
249 A4NNF6G8G63JOIUV  ~/~/Write API Key**
250 )))
Xiaoling 1.2 251
252
Xiaoling 50.2 253
Xiaoling 1.3 254 === 4.6.2 Input the command at the console(LG01 new version:4.3.4) ===
Xiaoling 1.2 255
Xiaoling 50.2 256
Xiaoling 25.2 257 1. [[image:image-20220531141150-24.png]](((
Xiaoling 50.2 258
259
Xiaoling 1.2 260 Open this Application.
Xiaoling 25.2 261
262
263
Xiaoling 1.2 264 )))
Xiaoling 27.2 265 1. (((
266 [[image:image-20220531141400-27.png]]
267
Xiaoling 50.2 268
Xiaoling 1.2 269 Check the settings.
Xiaoling 27.2 270
271
272
Xiaoling 1.2 273 )))
Xiaoling 28.2 274 1. (((
Xiaoling 29.2 275 [[image:image-20220531141601-30.png]]
276
Xiaoling 50.2 277
Xiaoling 1.2 278 input this command.
Xiaoling 27.2 279
280
281
Xiaoling 1.2 282 )))
Xiaoling 28.2 283 1. [[image:image-20220531141515-29.png]](((
Xiaoling 50.2 284
285
Xiaoling 1.2 286 Check the result.
Xiaoling 28.2 287
288
289
Xiaoling 1.2 290 )))
291
Xiaoling 1.3 292 == 4.7 Updata data to Server(Through MQTT) ==
Xiaoling 1.2 293
Xiaoling 50.2 294
Xiaoling 1.2 295 * This version of Sketch implements these features:
296
297 1. Read the LG01 configuration information from Linux.
298 1. Receive the LoRa node data and store the data in the /tmp/iot/channels
299 1. Send reply after then receive LoRa node data.
300 1. Sketch will write active content to /tmp/iot/status periodically (every 5 minutes).(Watchdog feature)
301
Xiaoling 50.2 302
303
Xiaoling 49.2 304 1. [[image:image-20220531143822-49.png]](((
Xiaoling 50.2 305
306
Xiaoling 1.2 307 Open the sketch [[MQTT_Simple_Server>>url:https://github.com/dragino/Arduino-Profile-Examples/tree/master/libraries/Dragino/examples/LoRa/MQTT_Simple_Server]].(Board:Dragino Yun + UNO or LG01/OLG01,Port:Network ports.)and upload.
Xiaoling 30.2 308
309
310
Xiaoling 1.2 311 )))
Xiaoling 31.2 312 1. [[image:image-20220531141800-32.png]](((
Xiaoling 50.2 313
314
Xiaoling 1.2 315 Open Serial monitor.
Xiaoling 31.2 316
317
318
Xiaoling 1.2 319 )))
Xiaoling 48.2 320 1. [[image:image-20220531143748-48.png]](((
Xiaoling 50.2 321
322
Xiaoling 1.2 323 Open the Sketch [[MQTT_Client>>url:https://github.com/dragino/Arduino-Profile-Examples/tree/master/libraries/Dragino/examples/IoTServer/ThingSpeak/MQTT_Client]]. (Board:Arduino/Genuino Uno.Port:Serial ports.)and upload.
Xiaoling 32.2 324
325
326
Xiaoling 1.2 327 )))
Xiaoling 33.2 328 1. [[image:image-20220531141927-34.png]](((
Xiaoling 50.2 329
330
Xiaoling 1.2 331 Open Serial monitor.
Xiaoling 33.2 332
333
334
Xiaoling 1.2 335 )))
336 1. Go to IoT Server to check the result.
337 1. (((
Xiaoling 47.2 338 [[image:image-20220531143707-47.png]]
Xiaoling 34.2 339
Xiaoling 35.2 340
Xiaoling 34.2 341
Xiaoling 1.2 342 )))
343 1. (((
Xiaoling 46.2 344 [[image:image-20220531143632-46.png]]
Xiaoling 35.3 345
346
347
Xiaoling 1.2 348 )))
349
Xiaoling 1.3 350 = 5. FAQ =
Xiaoling 1.2 351
Xiaoling 50.2 352
Xiaoling 1.3 353 == 5.1 Configure to support general mqtt server ==
Xiaoling 1.2 354
Xiaoling 50.2 355
Xiaoling 1.2 356 First, try to run **mosquitto_pub** in Linux console to send a data to MQTT server.
357
358 For example: for ThingSpeak, a MQTT publish command is:
359
Xiaoling 36.2 360 (% class="box infomessage" %)
361 (((
362 **mosquitto_pub -h mqtt.thingspeak.com -p 1883 -u dragino -P QZXTAKE88V3S7O2J  -i dragino_Client -t channels/200893/publish/B9Z0R25QNVEBKIFY -m "field1=34&field2=89&status=MQTTPUBLISH"**
363 )))
Xiaoling 1.2 364
365 What now we need to put in the LG01 is how to combine this command.
366
367 1. LG01 web console -> Sensor -> MQTT
368 1. Choose general Server
Xiaoling 36.2 369 1. [[image:image-20220531142342-37.png]](((
Xiaoling 50.2 370
371
Xiaoling 1.2 372 Refer to your server to fill in.
Xiaoling 36.2 373
374
375
Xiaoling 1.2 376 )))
Xiaoling 37.2 377 1. [[image:image-20220531142447-38.png]](((
Xiaoling 50.2 378
379
Xiaoling 1.2 380 Ex:ThingSpeak Server
Xiaoling 37.2 381
382
Xiaoling 1.2 383 )))
384
385 According to MQTT commands: mosquitto_pub, we need to put
386
387 * server[-h] : mqtt.thingspeak.com
388 * port[-p] : 1883
389 * User Name[-u]: dragino
390 * Password[-P]:QZXTAKE88V3S7O2J
391 * Client ID[-i]: dragino_Client
392 * topic_format[-t]: channels/CHANNEL/publish/WRITE_API
393 * data_format[-m]: DATA&status=MQTTPUBLISH
394
395 In the mqtt script, the upper **CHANNEL** will be replaced by the parameter (remote channel in IoT server). and the **WRITE_API** will be replaced by the settings in write api key. the **DATA** will be replaced by the value stored in the /var/iot/channels/LOCAL_CHANNEL file.
396
397 MQTT script will keep checking the files in /var/iot/channels/ . If it find a match Local channel, then the MQTT script will send out the data of this local channel to a remote channel according to the setting above.
398
399 User can also enable MQTT debug in the LG01 ~-~-> IoT Server settings and run logread in Linux console to see how the mqtt command is compose. below is an example:
400
Xiaoling 38.2 401 [[image:image-20220531142525-39.png]]
Xiaoling 1.2 402
Xiaoling 50.2 403
Xiaoling 1.2 404 MQTT Logread example
405
406
Xiaoling 50.2 407
Xiaoling 1.3 408 == 5.2 What is Watchdog feature? ==
Xiaoling 1.2 409
Xiaoling 50.2 410
Xiaoling 1.2 411 LG01 for 4.3.3 version,we have added watchdog feature. In order to avoid the gateway running process will get stuck. So add writing action on every times.
412
Xiaoling 50.2 413
Xiaoling 39.2 414 * [[image:image-20220531142558-40.png]](((
Xiaoling 1.2 415 Enable Sensor->Micro-Controller settings
Xiaoling 39.3 416
417
418
Xiaoling 1.2 419 )))
Xiaoling 40.2 420 * [[image:image-20220531142629-41.png]](((
Xiaoling 50.2 421
422
Xiaoling 1.2 423 Open the sketch [[MQTT_Simple_Server>>url:https://github.com/dragino/Arduino-Profile-Examples/tree/master/libraries/Dragino/examples/LoRa/MQTT_Simple_Server]].(Board:Dragino Yun + UNO or LG01/OLG01,Port:Network ports.)and upload.
Xiaoling 40.2 424
425
426
Xiaoling 1.2 427 )))
Xiaoling 41.2 428 * [[image:image-20220531142659-42.png]](((
Xiaoling 50.2 429
430
Xiaoling 1.2 431 Each time the function loops, it uses the watchdog function once.
Xiaoling 41.2 432
433
434
Xiaoling 1.2 435 )))
Xiaoling 42.2 436 * [[image:image-20220531142720-43.png]](((
Xiaoling 50.2 437
438
Xiaoling 1.2 439 Open SSH and check watchdog result.If the result change constantly,the feature enable successfully.
Xiaoling 42.2 440
441
442
Xiaoling 1.2 443 )))
Xiaoling 43.2 444 * [[image:image-20220531142748-44.png]](((
Xiaoling 50.2 445
446
Xiaoling 1.2 447 If the time doesnt's change,you can refer it to calculate for checking status.
448 )))
449
Xiaoling 44.1 450 (% class="wikigeneratedid" id="H" %)
451
Copyright ©2010-2022 Dragino Technology Co., LTD. All rights reserved
Dragino Wiki v2.0