Wiki source code of Through MQTT to upload data

Version 44.2 by Xiaoling on 2022/05/31 14:32

Hide last authors
Xiaoling 44.2 1 **Contents: **
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
Xiaoling 1.5 10 {{{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 11
12
Xiaoling 1.2 13 = 2. MQTT Features =
Xiaoling 1.1 14
Xiaoling 1.2 15 The MQTT protocol runs on TCP / IP or other network protocols and provides an ordered, lossless, bidirectional connection. Features include:
Xiaoling 1.1 16
Xiaoling 1.2 17 * The publish / subscribe messaging pattern used, which provides one-to-many messaging for decoupling from the application.
18 * Message-masking mechanism for payload content.
19 * There are three Quality of Service (QoS) for transmitting messages:
20 * At most, messages of this level may be lost or duplicated, and message postings depend on the underlying TCP / IP network. That is: <= 1
21 * At one time, this level will ensure that the message arrives, but the message may be repeated. That is:> = 1
22 * 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
23 * Notification mechanism, notify the transmission of both sides when an exception occurs
Xiaoling 1.1 24
25
Xiaoling 44.2 26
Xiaoling 1.2 27 = 3. What does this example shows? How it works? =
Xiaoling 1.1 28
Xiaoling 43.3 29 (((
Xiaoling 1.2 30 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 43.3 31 )))
Xiaoling 1.1 32
Xiaoling 3.2 33 [[image:image-20220531135402-1.png]]
Xiaoling 1.1 34
Xiaoling 1.2 35 Message Queuing Telemetry Transport
Xiaoling 1.1 36
37
Xiaoling 3.2 38 [[image:image-20220531135449-2.png]]
39
Xiaoling 1.2 40 working principle
Xiaoling 1.1 41
Xiaoling 3.2 42
Xiaoling 4.2 43 [[image:image-20220531135521-3.png]]
Xiaoling 1.2 44
Xiaoling 43.3 45 (((
Xiaoling 1.2 46 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 47 )))
Xiaoling 1.2 48
Xiaoling 4.2 49
50
Xiaoling 5.2 51 [[image:image-20220531135607-4.png]]
Xiaoling 1.2 52
Xiaoling 43.3 53 (((
Xiaoling 1.2 54 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 55 )))
Xiaoling 1.2 56
Xiaoling 43.3 57 (((
Xiaoling 1.2 58 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 59 )))
Xiaoling 1.2 60
Xiaoling 43.3 61 (((
Xiaoling 1.2 62 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 63 )))
Xiaoling 1.2 64
65
66 = 4. Preparation =
67
68 == 4.1 Hardware ==
69
Xiaoling 6.2 70 1. [[image:image-20220531135650-5.png]](((
Xiaoling 1.2 71 Listening on the LoRa wireless channel, while there is new LoRa packet arrives, parse it and send out to IoT Server.
Xiaoling 6.2 72
73
74
Xiaoling 1.2 75 )))
Xiaoling 7.2 76 1. [[image:image-20220531135750-6.png]](((
Xiaoling 1.2 77 The LoRa End node keeps getting temperature and humidity from the sensor and sends out via LoRa periodically.
78 )))
79
80 == 4.2 Software ==
81
82 1. [[4.3.4 version>>url:http://www.dragino.com/downloads/index.php?dir=motherboards/ms14/Firmware/IoT/]]
83 1. [[MQTT_Client>>url:https://github.com/dragino/Arduino-Profile-Examples/tree/master/libraries/Dragino/examples/IoTServer/ThingSpeak/MQTT_Client]]
84 1. [[MQTT_Simple_Server MQTT_Simeple_Server>>url:https://github.com/dragino/Arduino-Profile-Examples/tree/master/libraries/Dragino/examples/LoRa/MQTT_Simple_Server]]
85 1. [[DHTlib>>url:https://github.com/goodcheney/Lora/blob/patch-1/Lora%20Shield/Examples/DHTlib.zip]]
86
87 == 4.3 Server(For example) ==
88
89 * [[ThingSpeak>>url:https://thingspeak.com/]]
90
Xiaoling 1.3 91 == 4.4 Configure IoT Server ==
Xiaoling 1.2 92
93 * Need this keys:
94
Xiaoling 9.2 95 1. [[image:image-20220531140054-8.png]](((
Xiaoling 1.2 96 Channel ID,Write API Key
Xiaoling 8.3 97
98
99
Xiaoling 1.2 100 )))
Xiaoling 14.2 101 1. [[image:image-20220531140326-13.png]](((
Xiaoling 1.2 102 Account->My Profile,MQTT API Key
Xiaoling 10.2 103
104
105
Xiaoling 1.2 106 )))
107 1. (((
Xiaoling 14.2 108 [[image:image-20220531140304-12.png]]
Xiaoling 1.2 109 )))
110
Xiaoling 1.3 111 == 4.5 Configure LG01 settings ==
Xiaoling 1.2 112
113 Tips:Please confirm that your LG01 version is 4.3.3.
114
Xiaoling 14.2 115 [[image:image-20220531140248-11.png]]
Xiaoling 1.2 116
Xiaoling 19.3 117 **Overview**
Xiaoling 1.2 118
Xiaoling 14.3 119
120
Xiaoling 15.2 121 [[image:image-20220531140425-14.png]]
Xiaoling 1.2 122
Xiaoling 19.3 123 **Senosor->IOT Server**
Xiaoling 1.2 124
Xiaoling 15.2 125
Xiaoling 16.2 126 [[image:image-20220531140458-15.png]]
Xiaoling 1.2 127
Xiaoling 19.3 128 **MQTT->Configure MQTT Server**
Xiaoling 1.2 129
Xiaoling 16.2 130
131
Xiaoling 17.2 132 [[image:image-20220531140538-16.png]]
Xiaoling 1.2 133
Xiaoling 19.3 134 **MQTT->MQTT Channel**
Xiaoling 1.2 135
Xiaoling 17.2 136
137
Xiaoling 18.2 138 [[image:image-20220531140611-17.png]]
Xiaoling 1.2 139
Xiaoling 19.3 140 **Sensor->LoRa/LoraWAN**
Xiaoling 1.2 141
Xiaoling 18.2 142
143
Xiaoling 19.2 144 [[image:image-20220531140651-18.png]]
Xiaoling 1.2 145
Xiaoling 19.3 146 Configure network access.**Network->Internet Access**
Xiaoling 1.2 147
148
Xiaoling 1.3 149 == 4.6 Try MQTT API call with LG01 Linux command ==
Xiaoling 1.2 150
Xiaoling 1.3 151 === 4.6.1 Input the command at the console ===
Xiaoling 1.2 152
Xiaoling 21.2 153 1. [[image:image-20220531140811-19.png]](((
Xiaoling 1.2 154 Open this Application.
Xiaoling 21.2 155
156
Xiaoling 1.2 157 )))
Xiaoling 21.2 158 1. [[image:image-20220531140842-20.png]](((
Xiaoling 1.2 159 Input passward(dragino).After into the console.
Xiaoling 21.2 160
161
Xiaoling 1.2 162 )))
Xiaoling 22.2 163 1. [[image:image-20220531140915-21.png]](((
Xiaoling 1.2 164 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 165
166
Xiaoling 1.2 167 )))
Xiaoling 23.2 168 1. [[image:image-20220531140955-22.png]](((
Xiaoling 1.2 169 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 170
171
172
Xiaoling 1.2 173 )))
Xiaoling 24.2 174 1. [[image:image-20220531141026-23.png]](((
Xiaoling 1.2 175 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.
176 )))
177
178 * This command:
179
Xiaoling 24.3 180 (% class="box infomessage" %)
181 (((
182 **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"**
183 )))
Xiaoling 1.2 184
185 * Need replace:
186
Xiaoling 24.3 187 (% class="box infomessage" %)
188 (((
189 **UZ4NGHKJMKS9WR5E  ~/~/MQTT API
190 399171            ~/~/Channel ID
191 A4NNF6G8G63JOIUV  ~/~/Write API Key**
192 )))
Xiaoling 1.2 193
194
Xiaoling 1.3 195 === 4.6.2 Input the command at the console(LG01 new version:4.3.4) ===
Xiaoling 1.2 196
Xiaoling 25.2 197 1. [[image:image-20220531141150-24.png]](((
Xiaoling 1.2 198 Open this Application.
Xiaoling 25.2 199
200
201
Xiaoling 1.2 202 )))
Xiaoling 27.2 203 1. (((
204 [[image:image-20220531141400-27.png]]
205
Xiaoling 1.2 206 Check the settings.
Xiaoling 27.2 207
208
209
Xiaoling 1.2 210 )))
Xiaoling 28.2 211 1. (((
Xiaoling 29.2 212 [[image:image-20220531141601-30.png]]
213
Xiaoling 1.2 214 input this command.
Xiaoling 27.2 215
216
217
Xiaoling 1.2 218 )))
Xiaoling 28.2 219 1. [[image:image-20220531141515-29.png]](((
Xiaoling 1.2 220 Check the result.
Xiaoling 28.2 221
222
223
Xiaoling 1.2 224 )))
225
Xiaoling 1.3 226 == 4.7 Updata data to Server(Through MQTT) ==
Xiaoling 1.2 227
228 * This version of Sketch implements these features:
229
230 1. Read the LG01 configuration information from Linux.
231 1. Receive the LoRa node data and store the data in the /tmp/iot/channels
232 1. Send reply after then receive LoRa node data.
233 1. Sketch will write active content to /tmp/iot/status periodically (every 5 minutes).(Watchdog feature)
234
Xiaoling 30.2 235 1. [[image:image-20220531141656-31.png]](((
Xiaoling 1.2 236 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 237
238
239
Xiaoling 1.2 240 )))
Xiaoling 31.2 241 1. [[image:image-20220531141800-32.png]](((
Xiaoling 1.2 242 Open Serial monitor.
Xiaoling 31.2 243
244
245
Xiaoling 1.2 246 )))
Xiaoling 32.2 247 1. [[image:image-20220531141842-33.png]](((
Xiaoling 1.2 248 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 249
250
251
Xiaoling 1.2 252 )))
Xiaoling 33.2 253 1. [[image:image-20220531141927-34.png]](((
Xiaoling 1.2 254 Open Serial monitor.
Xiaoling 33.2 255
256
257
Xiaoling 1.2 258 )))
259 1. Go to IoT Server to check the result.
260 1. (((
Xiaoling 34.2 261 [[image:image-20220531142023-35.png]]
262
Xiaoling 35.2 263
Xiaoling 34.2 264
Xiaoling 1.2 265 )))
266 1. (((
Xiaoling 35.2 267 [[image:image-20220531142155-36.png]]
Xiaoling 35.3 268
269
270
Xiaoling 1.2 271 )))
272
Xiaoling 1.3 273 = 5. FAQ =
Xiaoling 1.2 274
Xiaoling 1.3 275 == 5.1 Configure to support general mqtt server ==
Xiaoling 1.2 276
277 First, try to run **mosquitto_pub** in Linux console to send a data to MQTT server.
278
279 For example: for ThingSpeak, a MQTT publish command is:
280
Xiaoling 36.2 281 (% class="box infomessage" %)
282 (((
283 **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"**
284 )))
Xiaoling 1.2 285
286 What now we need to put in the LG01 is how to combine this command.
287
288 1. LG01 web console -> Sensor -> MQTT
289 1. Choose general Server
Xiaoling 36.2 290 1. [[image:image-20220531142342-37.png]](((
Xiaoling 1.2 291 Refer to your server to fill in.
Xiaoling 36.2 292
293
294
Xiaoling 1.2 295 )))
Xiaoling 37.2 296 1. [[image:image-20220531142447-38.png]](((
Xiaoling 1.2 297 Ex:ThingSpeak Server
Xiaoling 37.2 298
299
Xiaoling 1.2 300 )))
301
302 According to MQTT commands: mosquitto_pub, we need to put
303
304 * server[-h] : mqtt.thingspeak.com
305 * port[-p] : 1883
306 * User Name[-u]: dragino
307 * Password[-P]:QZXTAKE88V3S7O2J
308 * Client ID[-i]: dragino_Client
309 * topic_format[-t]: channels/CHANNEL/publish/WRITE_API
310 * data_format[-m]: DATA&status=MQTTPUBLISH
311
312 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.
313
314 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.
315
316 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:
317
Xiaoling 38.2 318 [[image:image-20220531142525-39.png]]
Xiaoling 1.2 319
320 MQTT Logread example
321
322
Xiaoling 1.3 323 == 5.2 What is Watchdog feature? ==
Xiaoling 1.2 324
325 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.
326
Xiaoling 39.2 327 * [[image:image-20220531142558-40.png]](((
Xiaoling 1.2 328 Enable Sensor->Micro-Controller settings
Xiaoling 39.3 329
330
331
Xiaoling 1.2 332 )))
Xiaoling 40.2 333 * [[image:image-20220531142629-41.png]](((
Xiaoling 1.2 334 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 335
336
337
Xiaoling 1.2 338 )))
Xiaoling 41.2 339 * [[image:image-20220531142659-42.png]](((
Xiaoling 1.2 340 Each time the function loops, it uses the watchdog function once.
Xiaoling 41.2 341
342
343
Xiaoling 1.2 344 )))
Xiaoling 42.2 345 * [[image:image-20220531142720-43.png]](((
Xiaoling 1.2 346 Open SSH and check watchdog result.If the result change constantly,the feature enable successfully.
Xiaoling 42.2 347
348
349
Xiaoling 1.2 350 )))
Xiaoling 43.2 351 * [[image:image-20220531142748-44.png]](((
Xiaoling 1.2 352 If the time doesnt's change,you can refer it to calculate for checking status.
353 )))
354
Xiaoling 44.1 355 (% class="wikigeneratedid" id="H" %)
356