Wiki source code of Through MQTT to upload data

Version 44.6 by Xiaoling on 2022/05/31 14:33

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