Changes for page 2 Script

Last modified by Devin Chen on 2025/06/06 14:03

From version 19.1
edited by Hunter
on 2022/07/29 15:56
Change comment: There is no comment for this version
To version 20.1
edited by Hunter
on 2022/07/29 16:47
Change comment: There is no comment for this version

Summary

Details

Page properties
Content
... ... @@ -154,8 +154,132 @@
154 154  
155 155  (% class="mark" %)2.If your server requires SSL certificate to log in,please use OpenCloud.Because only OpenCloud platform can support to upload certificate
156 156  
157 -== **2.1 V-Box connect with customer server:grouprobotinfo.com** ==
157 +(% class="wikigeneratedid" %)
158 +**✎Note: **Before program the script of MQTT, please make sure the server(MQTT broker) can be connected through MQTT Client tool.
158 158  
160 +(% class="wikigeneratedid" %)
161 +Tool link: **[[MQTT.fx>>http://mqttfx.jensd.de/index.php/download]]**
162 +
163 +== **2.1 V-Box connect with test server(General Example)** ==
164 +
165 +{{code language="lua"}}
166 +--MQTT configuration table
167 +local MQTT_CFG={}
168 +MQTT_CFG.username = "weconsupport"
169 +MQTT_CFG.password = "123456"
170 +MQTT_CFG.netway = 0
171 +MQTT_CFG.keepalive = 60
172 +MQTT_CFG.cleansession = 1
173 +--TCP URL
174 +MQTT_URL = "tcp://mq.tongxinmao.com:1883"
175 +--Client ID
176 +MQTT_CLIENT_ID = "V-BOXH-AG"
177 +
178 +--Generate UUID
179 +function uuid()
180 + local seed = {'e','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}
181 + local tb = {}
182 + for i=1, 32 do
183 + table.insert(tb, seed[math.random(1,16)])
184 + end
185 + local sid=table.concat(tb)
186 + return string.format('%s',
187 + string.sub(sid,1,32)
188 + )
189 +end
190 +
191 +
192 +--Topic name to subscribed
193 +local SUBSCRIBE_TOPIC = 'testtopic/test/no1/123456'
194 +
195 +--Topic name to be published
196 +local PUBLISH_TOPIC = 'testtopic/test/no1/7890'
197 +
198 +
199 +--real time
200 +local LAST_TIME = 0
201 +
202 +
203 +--initialize mqtt
204 +function mqtt_init()
205 + print(string.format("mqtt init mqtt_url:%s mqtt_clientid:%s", MQTT_URL, MQTT_CLIENT_ID))
206 + g_mq, err = mqtt.create(MQTT_URL, MQTT_CLIENT_ID) -- create mqtt object,and declare it as a global variable
207 + if g_mq then
208 + g_mq:on("message", mqtt_msg_callback) -- Register a callback for receiving messages
209 + print("mqtt init success")
210 + else
211 + print("mqtt init failed:", err)
212 + end
213 +end
214 +
215 +-- connect to mqtt
216 +function mqtt_connect()
217 + print("mqtt connecting...")
218 + local stat, err = g_mq:connect(MQTT_CFG)
219 + if stat == nil then
220 + print("mqtt connect failed:", err)
221 + return
222 + else
223 + print("mqtt connected")
224 + end
225 + g_mq:subscribe(SUBSCRIBE_TOPIC, 0)
226 +end
227 +
228 +-- Received message callback function
229 +function mqtt_msg_callback(topic, msg)
230 + print("topic:", topic)
231 + print("msg:", msg)
232 + local objMsg = json.decode(msg)
233 + local water = objMsg.data.waterlevel
234 + local temp = objMsg.data.temperature
235 + addr_setword("@HDW20",water)
236 + addr_setword("@HDW10",temp)
237 +end
238 +
239 +--Send data (data upload to platform and encapsulate it with custom functions)
240 +function send_data()
241 + local pub_data = {
242 + timestamp = os.time(),
243 + messageId = 1,
244 + event = 'test_data',
245 + mfrs = 'V-Box',
246 + data = {
247 + id = uuid(),
248 + waterlevel = addr_getword("@HDW10"),
249 + temperature = addr_getword("@HDW20")
250 + }
251 + }
252 + return g_mq:publish(PUBLISH_TOPIC, json.encode(pub_data), 0, 0)
253 +end
254 +
255 +
256 +--main function fixed timed execution
257 +function MQTT.main()
258 + --dosomething
259 + print(os.date("%Y-%m-%d %H:%M %S", os.time()) .. " main start")
260 + --determine the mqtt object whether exist
261 + if g_mq then
262 + --determine the mqtt object whether has been connected or not
263 + if g_mq:isconnected() then
264 + send_data()
265 + else
266 + --if exceed 20 sec not connect, reconnect once
267 + if os.time() - LAST_TIME > 20 then
268 + LAST_TIME = os.time()
269 + --connect to mqtt or reconnect
270 + mqtt_connect()
271 + end
272 + end
273 + else
274 + --mqtt object does not exist so create new one
275 + mqtt_init()
276 + end
277 + print(os.date("%Y-%m-%d %H:%M %S", os.time()) .. " main end")
278 +end
279 +{{/code}}
280 +
281 +== **2.2 V-Box connect with customer server:grouprobotinfo.com** ==
282 +
159 159  This demo does not use SSL certification. Script is as below
160 160  
161 161  Demo1:
... ... @@ -303,7 +303,7 @@
303 303  end
304 304  {{/code}}
305 305  
306 -== **2.2 V-Box connect with Azure platform** ==
430 +== **2.3 V-Box connect with Azure platform** ==
307 307  
308 308  In this demo,V-Box connects with Azure by SSL certification.
309 309  
... ... @@ -434,7 +434,7 @@
434 434  end
435 435  )))
436 436  
437 -== **2.3 How to configure the Huawei platform?(✎Note: Huawei IOT DA function is only in China area.If you want this function,you need to use chinese mobile to register)** ==
561 +== **2.4 How to configure the Huawei platform?(✎Note: Huawei IOT DA function is only in China area.If you want this function,you need to use chinese mobile to register)** ==
438 438  
439 439  1.Register a account: [[https:~~/~~/www.huaweicloud.com/intl/en-us/s/JUlPVERNJQ>>https://www.huaweicloud.com/intl/en-us/s/JUlPVERNJQ]]
440 440  
... ... @@ -583,7 +583,7 @@
583 583  (% style="text-align:center" %)
584 584  [[image:1624441186851-536.png||height="434" width="700" class="img-thumbnail"]]
585 585  
586 -== **2.4 V-Box connect with Huawei platform** ==
710 +== **2.5 V-Box connect with Huawei platform** ==
587 587  
588 588  In this demo,V-Box connects with Huawei by SSL certification.
589 589  
... ... @@ -704,7 +704,7 @@
704 704  (% style="text-align:center" %)
705 705  [[image:1624506666650-161.png||height="547" width="1000" class="img-thumbnail"]]
706 706  
707 -== **2.5 V-Box connect with AWS platform** ==
831 +== **2.6 V-Box connect with AWS platform** ==
708 708  
709 709  === **Log in AWS** ===
710 710