Wiki source code of Through MQTT to upload data

Version 1.2 by Xiaoling on 2022/05/31 13:50

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