Show last authors
1 {{toc/}}
2
3
4
5
6 = 1. What is MQTT API? =
7
8 {{{MQTT is a publish/subscribe model that runs over TCP/IP sockets or WebSockets. MQTT over WebSockets can be secured with SSL.}}}
9
10
11 = 2. MQTT Features =
12
13 The MQTT protocol runs on TCP / IP or other network protocols and provides an ordered, lossless, bidirectional connection. Features include:
14
15 * The publish / subscribe messaging pattern used, which provides one-to-many messaging for decoupling from the application.
16 * Message-masking mechanism for payload content.
17 * There are three Quality of Service (QoS) for transmitting messages:
18 * At most, messages of this level may be lost or duplicated, and message postings depend on the underlying TCP / IP network. That is: <= 1
19 * At one time, this level will ensure that the message arrives, but the message may be repeated. That is:> = 1
20 * 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
21 * Notification mechanism, notify the transmission of both sides when an exception occurs
22
23
24 = 3. What does this example shows? How it works? =
25
26 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.
27
28 [[image:image-20220531135402-1.png]]
29
30 Message Queuing Telemetry Transport
31
32
33 [[image:image-20220531135449-2.png]]
34
35 working principle
36
37
38 [[image:image-20220531135521-3.png]]
39
40 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.
41
42
43
44 [[image:image-20220531135607-4.png]]
45
46 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.
47
48 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.
49
50 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/]] .
51
52
53 = 4. Preparation =
54
55 == 4.1 Hardware ==
56
57 1. [[image:image-20220531135650-5.png]](((
58 Listening on the LoRa wireless channel, while there is new LoRa packet arrives, parse it and send out to IoT Server.
59
60
61
62 )))
63 1. [[image:image-20220531135750-6.png]](((
64 The LoRa End node keeps getting temperature and humidity from the sensor and sends out via LoRa periodically.
65 )))
66
67
68 == 4.2 Software ==
69
70 1. [[4.3.4 version>>url:http://www.dragino.com/downloads/index.php?dir=motherboards/ms14/Firmware/IoT/]]
71 1. [[MQTT_Client>>url:https://github.com/dragino/Arduino-Profile-Examples/tree/master/libraries/Dragino/examples/IoTServer/ThingSpeak/MQTT_Client]]
72 1. [[MQTT_Simple_Server MQTT_Simeple_Server>>url:https://github.com/dragino/Arduino-Profile-Examples/tree/master/libraries/Dragino/examples/LoRa/MQTT_Simple_Server]]
73 1. [[DHTlib>>url:https://github.com/goodcheney/Lora/blob/patch-1/Lora%20Shield/Examples/DHTlib.zip]]
74
75
76 == 4.3 Server(For example) ==
77
78 * [[ThingSpeak>>url:https://thingspeak.com/]]
79
80
81 == 4.4 Configure IoT Server ==
82
83 * Need this keys:
84
85 1. [[image:image-20220531140054-8.png]](((
86 Channel ID,Write API Key
87
88
89
90 )))
91 1. [[image:image-20220531140326-13.png]](((
92 Account->My Profile,MQTT API Key
93
94
95
96 )))
97 1. (((
98 [[image:image-20220531140304-12.png]]
99 )))
100
101
102 == 4.5 Configure LG01 settings ==
103
104 Tips:Please confirm that your LG01 version is 4.3.3.
105
106 [[image:image-20220531140248-11.png]]
107
108 **Overview**
109
110
111
112 [[image:image-20220531140425-14.png]]
113
114 **Senosor->IOT Server**
115
116
117 [[image:image-20220531140458-15.png]]
118
119 **MQTT->Configure MQTT Server**
120
121
122
123 [[image:image-20220531140538-16.png]]
124
125 **MQTT->MQTT Channel**
126
127
128
129 [[image:image-20220531140611-17.png]]
130
131 **Sensor->LoRa/LoraWAN**
132
133
134
135 [[image:image-20220531140651-18.png]]
136
137 Configure network access.**Network->Internet Access**
138
139
140 == 4.6 Try MQTT API call with LG01 Linux command ==
141
142 === 4.6.1 Input the command at the console ===
143
144 1. [[image:image-20220531140811-19.png]](((
145 Open this Application.
146
147
148 )))
149 1. [[image:image-20220531140842-20.png]](((
150 Input passward(dragino).After into the console.
151
152
153 )))
154 1. [[~[~[image:https://wiki.dragino.com/images/thumb/5/5d/3_5.png/400px-3_5.png~|~|height="310" width="400"~]~]>>url:https://wiki.dragino.com/index.php/File:3_5.png]](((
155 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
156 )))
157 1. [[~[~[image:https://wiki.dragino.com/images/thumb/6/63/3_3.png/400px-3_3.png~|~|height="213" width="400"~]~]>>url:https://wiki.dragino.com/index.php/File:3_3.png]](((
158 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.
159 )))
160 1. [[~[~[image:https://wiki.dragino.com/images/thumb/3/35/3_4.png/500px-3_4.png~|~|height="180" width="500"~]~]>>url:https://wiki.dragino.com/index.php/File:3_4.png]](((
161 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.
162 )))
163
164 * This command:
165
166 {{{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"
167 }}}
168
169 * Need replace:
170
171 {{{UZ4NGHKJMKS9WR5E //MQTT API
172 399171 //Channel ID
173 A4NNF6G8G63JOIUV //Write API Key
174 }}}
175
176
177 === 4.6.2 Input the command at the console(LG01 new version:4.3.4) ===
178
179 1. [[~[~[image:https://wiki.dragino.com/images/8/84/3_1.png~|~|height="99" width="93"~]~]>>url:https://wiki.dragino.com/index.php/File:3_1.png]](((
180 Open this Application.
181 )))
182 1. [[~[~[image:https://wiki.dragino.com/images/thumb/6/6e/MQTT_Command_2.png/400px-MQTT_Command_2.png~|~|height="76" width="400"~]~]>>url:https://wiki.dragino.com/index.php/File:MQTT_Command_2.png]](((
183 Check the settings.
184 )))
185 1. [[~[~[image:https://wiki.dragino.com/images/thumb/8/8d/MQTT_Command_1.png/400px-MQTT_Command_1.png~|~|height="343" width="400"~]~]>>url:https://wiki.dragino.com/index.php/File:MQTT_Command_1.png]](((
186 input this command.
187 )))
188 1. [[~[~[image:https://wiki.dragino.com/images/thumb/d/de/MQTT_Command_3.png/400px-MQTT_Command_3.png~|~|height="207" width="400"~]~]>>url:https://wiki.dragino.com/index.php/File:MQTT_Command_3.png]](((
189 Check the result.
190 )))
191
192 == 4.7 Updata data to Server(Through MQTT) ==
193
194 * This version of Sketch implements these features:
195
196 1. Read the LG01 configuration information from Linux.
197 1. Receive the LoRa node data and store the data in the /tmp/iot/channels
198 1. Send reply after then receive LoRa node data.
199 1. Sketch will write active content to /tmp/iot/status periodically (every 5 minutes).(Watchdog feature)
200
201
202 1. [[~[~[image:https://wiki.dragino.com/images/thumb/8/82/MQTT_Sketch_2.png/400px-MQTT_Sketch_2.png~|~|height="233" width="400"~]~]>>url:https://wiki.dragino.com/index.php/File:MQTT_Sketch_2.png]](((
203 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.
204 )))
205 1. [[~[~[image:https://wiki.dragino.com/images/thumb/7/73/MQTT_Sketch2.png/400px-MQTT_Sketch2.png~|~|height="540" width="400"~]~]>>url:https://wiki.dragino.com/index.php/File:MQTT_Sketch2.png]](((
206 Open Serial monitor.
207 )))
208 1. [[~[~[image:https://wiki.dragino.com/images/thumb/9/98/MQTT_Sketch_1.png/400px-MQTT_Sketch_1.png~|~|height="261" width="400"~]~]>>url:https://wiki.dragino.com/index.php/File:MQTT_Sketch_1.png]](((
209 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.
210 )))
211 1. [[~[~[image:https://wiki.dragino.com/images/thumb/3/3d/MQTT_Sketch3.png/400px-MQTT_Sketch3.png~|~|height="371" width="400"~]~]>>url:https://wiki.dragino.com/index.php/File:MQTT_Sketch3.png]](((
212 Open Serial monitor.
213 )))
214 1. Go to IoT Server to check the result.
215 1. (((
216 [[~[~[image:https://wiki.dragino.com/images/thumb/4/43/MQTT_result_1.png/400px-MQTT_result_1.png~|~|alt="MQTT result 1.png" height="181" width="400"~]~]>>url:https://wiki.dragino.com/index.php/File:MQTT_result_1.png]]
217 )))
218 1. (((
219 [[~[~[image:https://wiki.dragino.com/images/thumb/7/70/MQTT_result_2.png/400px-MQTT_result_2.png~|~|alt="MQTT result 2.png" height="190" width="400"~]~]>>url:https://wiki.dragino.com/index.php/File:MQTT_result_2.png]]
220 )))
221
222 = 5. FAQ =
223
224 == 5.1 Configure to support general mqtt server ==
225
226 First, try to run **mosquitto_pub** in Linux console to send a data to MQTT server.
227
228 For example: for ThingSpeak, a MQTT publish command is:
229
230 {{{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"
231 }}}
232
233 What now we need to put in the LG01 is how to combine this command.
234
235 1. LG01 web console -> Sensor -> MQTT
236 1. Choose general Server
237 1. [[~[~[image:https://wiki.dragino.com/images/thumb/e/ea/MQTT_Configure1.png/600px-MQTT_Configure1.png~|~|height="367" width="600"~]~]>>url:https://wiki.dragino.com/index.php/File:MQTT_Configure1.png]](((
238 Refer to your server to fill in.
239 )))
240 1. [[~[~[image:https://wiki.dragino.com/images/0/07/MQTT_Configure2.png~|~|height="93" width="538"~]~]>>url:https://wiki.dragino.com/index.php/File:MQTT_Configure2.png]](((
241 Ex:ThingSpeak Server
242 )))
243
244 According to MQTT commands: mosquitto_pub, we need to put
245
246 * server[-h] : mqtt.thingspeak.com
247 * port[-p] : 1883
248 * User Name[-u]: dragino
249 * Password[-P]:QZXTAKE88V3S7O2J
250 * Client ID[-i]: dragino_Client
251 * topic_format[-t]: channels/CHANNEL/publish/WRITE_API
252 * data_format[-m]: DATA&status=MQTTPUBLISH
253
254 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.
255
256 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.
257
258 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:
259
260 [[~[~[image:https://wiki.dragino.com/images/thumb/8/81/MQTT_Configure3.png/600px-MQTT_Configure3.png~|~|height="114" width="600"~]~]>>url:https://wiki.dragino.com/index.php/File:MQTT_Configure3.png]]
261
262 MQTT Logread example
263
264
265 == 5.2 What is Watchdog feature? ==
266
267 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.
268
269 * [[~[~[image:https://wiki.dragino.com/images/thumb/f/f2/Wathcdog.png/400px-Wathcdog.png~|~|height="184" width="400"~]~]>>url:https://wiki.dragino.com/index.php/File:Wathcdog.png]](((
270 Enable Sensor->Micro-Controller settings
271 )))
272 * [[~[~[image:https://wiki.dragino.com/images/thumb/8/82/MQTT_Sketch_2.png/400px-MQTT_Sketch_2.png~|~|height="233" width="400"~]~]>>url:https://wiki.dragino.com/index.php/File:MQTT_Sketch_2.png]](((
273 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.
274 )))
275 * [[~[~[image:https://wiki.dragino.com/images/2/21/Loop.png~|~|height="181" width="388"~]~]>>url:https://wiki.dragino.com/index.php/File:Loop.png]](((
276 Each time the function loops, it uses the watchdog function once.
277 )))
278 * [[~[~[image:https://wiki.dragino.com/images/thumb/3/35/Dog.png/400px-Dog.png~|~|height="161" width="400"~]~]>>url:https://wiki.dragino.com/index.php/File:Dog.png]](((
279 Open SSH and check watchdog result.If the result change constantly,the feature enable successfully.
280 )))
281 * [[~[~[image:https://wiki.dragino.com/images/thumb/a/ac/Checkdog.png/400px-Checkdog.png~|~|height="222" width="400"~]~]>>url:https://wiki.dragino.com/index.php/File:Checkdog.png]](((
282 If the time doesnt's change,you can refer it to calculate for checking status.
283 )))
284
285 == ==
Copyright ©2010-2022 Dragino Technology Co., LTD. All rights reserved
Dragino Wiki v2.0