Wiki source code of 2 Script

Version 1.1 by Leo Wei on 2022/06/08 12:58

Show last authors
1 = **1 General Script Demo** =
2
3 == **1.1 Address Operation:Write/Read data from address A to B** ==
4
5 (((
6 For example:transfer D2 to D0
7 )))
8
9 (% style="text-align:center" %)
10 [[image:1624245865976-320.png||class="img-thumbnail" height="182" width="1000"]]
11
12 Depend on diffferent format of data.V-Box use different script functions.
13 for example. addr_setshort(addr,num) Function: Write 16-bit signed decimal address
14 addr_getshort(addr) Function:Read 16-bit signed decimal address
15 addr_getword(string addr)Function: Read 16-bit unsigned decimal address
16 More script function are in the second section of [[“V-BOX Script Interface Manual”>>doc:xwiki:V-BOX.V-Net.1\.User Manual.04\.Lua script function and operation.01\.Lua script function.WebHome]]
17
18 == **1.2 Arithmetic** ==
19
20 (% style="text-align:center" %)
21 [[image:1624249623612-177.png||class="img-thumbnail" height="337" width="400"]]
22
23 == **1.3 set 100 to D0~-~-D19** ==
24
25 (% style="text-align:center" %)
26 [[image:1624249693457-742.png||class="img-thumbnail" height="135" width="400"]]
27
28 == **1.4 short message** ==
29
30 When the alarm condition is reached: temp1 > 5 & temp2 >10 & temp3 < 20(lasts more than 5 seconds) , then send an "alarm trigger" sms.
31
32 When the alarm condition is released,then send an "alarm release" sms.
33
34 (% style="text-align:center" %)
35 [[image:1645535936750-316.png||class="img-thumbnail" height="385" width="400"]]
36
37 Script is as below:
38
39 (% class="box infomessage" %)
40 (((
41 function sms.main()
42 ~-~-~-~-~-~-send condition~-~-~-~-~-~-
43 local temp1 = addr_getword("@Temperature1")
44 local temp2 = addr_getword("@Temperature2")
45 local temp3 = addr_getword("@Temperature3")
46 local timer = addr_getword("@Timer")
47 local tag = addr_getbit("@Tag")
48 ~-~-~-~-~-~-lasting time~-~-~-~-~-~-
49 if temp1 > 5 and temp2 > 10 and temp3 < 20 then
50 timer = timer + 1
51 addr_setword("@Timer",timer)
52 else
53 timer = 0
54 addr_setword("@Timer",timer)
55 end
56 ~-~-~-~-~-~-send sms & output Y0~-~-~-~-~-~-
57 if timer > 5 then
58 if tag == 0 then
59 send_sms_ira("19859254700","alarm trigger")
60 addr_setbit("@Tag",1)
61 end
62 elseif tag == 1 then
63 send_sms_ira("19859254700","alarm release")
64 addr_setbit("@Tag",0)
65 end
66 end
67 )))
68
69 = **2 V-Box connect with third part server** =
70
71 V-Box have two mode.One is for V-Net,User need to use WECON server to store data.We call this V-NET platform.
72
73 (% class="mark" %)1.Europe server:eu.v-box.net
74
75 (% class="mark" %)2.Asean server:asean.v-box.net
76
77 Second is for OpenCloud mode.It means V-Box data will transfer to third part server and do not want to store data to WECON server.We call OpenCloud paltform
78
79 OpenCloud platform is used in configuring the script.Then V-Box can connect with third part server.
80
81 Both mode can support script  to connect with third part server.the difference:
82
83 (% class="mark" %)1.If you want to store in WECON server ,please use V-NET.
84
85 (% class="mark" %)2.If your server requires SSL certificate to log in,please use OpenCloud.Because only OpenCloud platform can support to upload certificate
86
87 == **2.1 V-Box connect with customer server:grouprobotinfo.com** ==
88
89 This demo does not use SSL certification. Script is as below
90
91 Demo1:
92
93 (% class="box infomessage" %)
94 (((
95 ~-~- Meta class
96 ~-~-main
97 function mq.main()
98 if not mq.m then
99 local err = ""
100 \\ mq.m, err = mqtt.create("tcp:~/~/grouprobotinfo.com:1883", "ClienID") ~-~- create connection
101 if mq.m then
102 mq.config = {
103 username = "",~-~- ID
104 password = "",~-~- password
105 netway = 1, ~-~- Ethernet connection, WIFI=1
106 ~-~- keepalive = 100, ~-~- Optional, set the connection heartbeat interval for 100 seconds.
107 ~-~- cleansession = 0, ~-~- Optional, keep session
108 }
109 mq.m:on("message", function(topic, msg) ~-~- Register for receiving message callbacks
110 local str = string.format("%s:%s", topic, msg)
111 ~-~- print("mqtt msg:", str) ~-~- Print out the received topics and content
112 end
113 )
114 mq.m:on("offline", function (cause) ~-~- Register for lost connection callbacks
115 ~-~- addr_setstring("@xxx", "cause"..(cause or " got nil"))
116 end)
117 mq.m:on("arrived", function() ~-~- Registration for sending messages to callbacks 
118 print("msg arrived")
119 end)
120 else
121 print("mqtt create failed:", err) ~-~- Create object failed
122 end
123 else
124 if mq.m:isconnected() then ~-~- If online, post a message
125 local phaseStatus ="unknow"
126 if addr_getbit("@Standby")== 1 then
127 phaseStatus = "Standby"
128 elseif addr_getbit("@Pre-Freeze")==1 then
129 phaseStatus= "Pre-Freeze"
130 elseif addr_getbit("@Prepare")==1 then
131 phaseStatus ="Prepare"
132 elseif addr_getbit("@Primary Dry")==1 then
133 phaseStatus = "Primary dry"
134 elseif addr_getbit("@Secondary Dry")==1 then
135 phaseStatus = "Secondary Dry"
136 end
137 ~-~- print(addr_getbit("@Primary Dry"))
138 ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~--
139 local activating ="unknow"
140 if addr_getbit("@Compressor")==1 then
141 activating = ",".."Compressor"
142 end
143 if addr_getbit("@Silicone Pump")==1 then
144 activating = activating..",".."Silicone Pump"
145 end
146 if addr_getbit("@Vacuum Pump")==1 then
147 activating = activating..",".."Vacuum Pump"
148 end
149 if addr_getbit("@Root Pump")==1 then
150 activating = activating..",".."Root Pump"
151 end
152 if addr_getbit("@Heater")==1 then
153 activating = activating..",".."Heater"
154 end
155 if addr_getbit("@Valve Silicone")==1 then
156 activating = activating..",".."Valve Silicone"
157 end
158 if addr_getbit("@Valve Ice Condenser")==1 then
159 activating = activating..",".."Valve Ice Condenser"
160 end
161 if addr_getbit("@Valve Vacuum Pump")==1 then
162 activating = activating..",".."Valve Vacuum Pump"
163 end
164 local pr_activating =string.sub(activating,2)
165 ~-~- print(pr_activating) 
166
167
168 local status_text ="unknow"
169 if addr_getbit("@Status Run")==1 then
170 status_text = "RUNNING"
171 else
172 status_text = "STOP"
173 end
174 ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~--
175 \\ local js = {type="status",
176 mc_name ="FD300",
177 status=status_text,
178 elapsed_time={
179 hour=addr_getword("@Elapsed Time (Hour)"),
180 min=addr_getword("@Elapsed Time (Minute)"),
181 sec=addr_getword("@Elapsed Time (Second)")
182 },
183 phase = phaseStatus,
184 step = addr_getword("@Step"),
185 activating_output = pr_activating,
186 sv=addr_getshort("@SV Silicone")/10,
187 pv=addr_getshort("@PV Silicone")/10,
188 product1=addr_getshort("@Product 1")/10,
189
190 product2=addr_getshort("@Product 2")/10,
191 product3=addr_getshort("@Product 3")/10,
192 product4=addr_getshort("@Product 4")/10,
193 ice1=addr_getshort("@Ice condenser 1")/10,
194 ice2=addr_getshort("@Ice condenser 2")/10,
195 vacuum=addr_getfloat("@Vacuum")
196
197 }
198 local jsAlarm = { HPC = addr_getbit("@B_25395#W0.00"),
199 ODPC = addr_getbit("@B_25395#W0.01"),
200 MTPC=addr_getbit("@B_25395#W0.02"),
201 HTT = addr_getbit("@B_25395#W1.03"),
202 CPC = addr_getbit("@B_25395#W0.08"),
203 CPSP =addr_getbit("@B_25395#W1.00"),
204 CPVP =addr_getbit("@B_25395#W0.10"),
205 CPRP =addr_getbit("@B_25395#W0.11"),
206 HP =addr_getbit("@B_25395#W1.01"),
207 PP= addr_getbit("@B_25395#W1.02"),
208 PO=addr_getbit("@B_25395#W0.07"),
209 FSE=addr_getbit("@B_25395#W2.04"),
210 AVVSVV=addr_getbit("@B_25395#W1.12"),
211 ICHT=addr_getbit("@B_25395#W3.06")
212
213 }
214 \\ ~-~- ("@B_25395#CIO1.02")
215 mq.m:publish("mqtt-v-box-epsilon-fd300", json.encode(js) , 0, 0)
216 mq.m:publish("mqtt-v-box-epsilon-alarm-fd300", json.encode(jsAlarm) , 0, 0)
217 else
218 local stat, err = mq.m:connect(mq.config) ~-~- connection
219 if stat == nil then ~-~-Determine whether to connect
220 print("mqtt connect failed:", err)
221 return ~-~- Connection failed, return directly
222 end
223 mq.m:subscribe("mqtt-v-box-epsilon", 0)~-~- Subscribe to topics
224 \\ end
225 ~-~- mq.m:unsubscribe("stc/test")
226 ~-~- mq.m:disconnect() ~-~- close matt
227 ~-~- mq.m:close() ~-~- close clase
228 end
229 end
230 )))
231
232 == **2.2 V-Box connect with Azure platform** ==
233
234 In this demo,V-Box connects with Azure by SSL certification.
235
236 Video link: [[https:~~/~~/youtu.be/cdI6rIQcpMY?list=PL_Bpnb2RgaksCic9HCcVAZhU9sYwCRKzW>>https://youtu.be/cdI6rIQcpMY?list=PL_Bpnb2RgaksCic9HCcVAZhU9sYwCRKzW]]
237
238 Tool Download link: [[https:~~/~~/wecon-disk.oss-ap-southeast-1.aliyuncs.com/Download/WIKI/V-BOX/Demo/Azure/Azure%20tool.zip>>https://wecon-disk.oss-ap-southeast-1.aliyuncs.com/Download/WIKI/V-BOX/Demo/Azure/Azure%20tool.zip]]
239
240 Script is as below
241
242 (% class="box infomessage" %)
243 (((
244 ~-~-https:~/~/support.huaweicloud.com/qs-IoT/iot_05_0005.html mqtt.fx monitor to connect azure iot
245 sprint = print
246
247 ~-~-Get custom configuration parameters (vbox custom information)
248 ~-~-local CUSTOM = bns_get_config("bind")
249 ~-~-local DS_ID = CUSTOM.DSID or "60a71ccbbbe12002c08f3a1a_WECON"
250
251
252 ~-~-Cloud mode interface to obtain the MQTT information configured by the cloud platform: (5 returns, namely the server address, client ID, connection table, last word table, certificate table)
253 local MQTT_URL, MQTT_CLIENTID, MQTT_CFG, MQTT_LWT, MQTT_CART = mqtt.setup_cfg()
254
255 ~-~-MQTT_CFG.username = '60a71ccbbbe12002c08f3a1a_WECON'
256 ~-~-MQTT_CFG.password='wecon123'
257 ~-~-MQTT_CLIENTID = '60a71ccbbbe12002c08f3a1a_WECON_0_0_2021052110usernxame:60a71ccbbbe12002c08f3a1a_WECONpassword:a0a951581855aa8e0262129da6cf1b43f2c0ecfac4fa56117fc5a20c90be169a'
258
259 ~-~-publish to topics
260 local pub_RE_TOPIC = string.format('devices/wecon_02/messages/events/')
261 ~-~-Subscribe topics
262 local Subscribe_RE_TOPIC1 = string.format('devices/wecon_02/messages/devicebound/#')
263
264 ~-~-variable
265 local last_time = 0
266
267
268 ~-~-Timing main function
269 function Azure.main()
270
271 sprint(os.date("%Y-%m-%d %H:%M %S", os.time()) .. " Azureiot.main start")
272 if g_mq then
273 if g_mq:isconnected() then
274 send_Data()
275 else
276 if os.time() - last_time > 20 then
277 last_time = os.time()
278 mymqtt_connect()
279 end
280 end
281 else
282 mymqtt_init()
283 end
284 sprint(os.date("%Y-%m-%d %H:%M %S", os.time()) .. " Azureiot.main end")
285 end
286
287 ~-~- Initialize MQTT
288 function mymqtt_init()
289 sprint(string.format("mqtt init mqtt_url:%s mqtt_clientid:%s", MQTT_URL, MQTT_CLIENTID))
290 g_mq, err = mqtt.create(MQTT_URL, MQTT_CLIENTID) ~-~- Create the object and declare it as a global variable
291 if g_mq then
292 g_mq:on("message", mymqtt_msg_callback) ~-~- Register to receive message callbacks
293 sprint("mqtt init success")
294 else
295 sprint("mqtt init failed:", err)
296 end
297 end
298
299 ~-~- Connect to MQTT server
300 function mymqtt_connect()
301 sprint("mqtt connecting...")
302 local stat, err = g_mq:connect(MQTT_CFG,MQTT_LWT, MQTT_CART)
303 if stat == nil then
304 sprint("mqtt connect failed:", err)
305 return
306 else
307 sprint("mqtt connected")
308 end
309 g_mq:subscribe(Subscribe_RE_TOPIC1, 0) 
310 end
311
312 ~-~- Receive MQTT message callback function
313 function mymqtt_msg_callback(topic, msg)
314 print("topic:",topic)
315 print("revdata:",msg)
316 ~-~- local revData = json.decode(msg)
317 ~-~- if topic == Subscribe_RE_TOPIC1 then ~-~-Process topic information subscribed from the cloud
318 ~-~- if string.match(topic,Subscribe_RE_TOPIC1) then
319 ~-~- print("topi11:",topic)
320 setValue(revData)
321 ~-~- end
322 end
323
324 ~-~-Process the received data
325 ~-~-function setValue(revData)
326 ~-~- if revData ~~=nil then 
327 ~-~- for i,v in pairs(revData) do
328 ~-~- print("Data received:",i,v)
329 ~-~- end
330 ~-~- end
331 ~-~-end
332
333 ~-~-Get real-time data
334 function getData()
335 local jdata = {}
336 local addr = bns_get_alldata()
337 print(json.encode(addr))
338 for i,v in pairs(addr) do
339 if v[2] == 1 then
340 jdata[v[3]] = v[4]
341 end
342 end
343 return jdata
344 end
345
346
347 ~-~-send data
348 function send_Data()
349 local pub_data = {100
350 ~-~- services=~{~{
351 \\ ~-~-serviceId ='Temperature',
352 ~-~- properties={
353 ~-~- value = 55
354 ~-~- },
355 ~-~- }}
356 }
357 sprint(json.encode(pub_data))
358 print("..........",pub_RE_TOPIC)
359 return g_mq:publish(pub_RE_TOPIC, json.encode(pub_data), 0, 0)
360 end
361 )))
362
363 == **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)** ==
364
365 1.Register a account: [[https:~~/~~/www.huaweicloud.com/intl/en-us/s/JUlPVERNJQ>>https://www.huaweicloud.com/intl/en-us/s/JUlPVERNJQ]]
366
367 2.log in the Huawei IOTDA
368
369 [[https:~~/~~/console.huaweicloud.com/iotdm/?region=cn-north-4&locale=en-us#/dm-portal/home>>https://console.huaweicloud.com/iotdm/?region=cn-north-4&locale=en-us#/dm-portal/home]]
370
371 3.Create product
372
373 (% style="text-align:center" %)
374 [[image:1624433478954-859.png||class="img-thumbnail" height="497" width="1100"]]
375
376 4.Product name,manufacturer,device type and industry,set according to your own needs.
377
378 Protocol: MQTT
379
380 Data Type: JSON
381
382 After finishing configuration,please click "OK"
383
384 (% style="text-align:center" %)
385 [[image:1624433531968-337.png||class="img-thumbnail" height="568" width="700"]]
386
387 5.Device
388
389 After product register,continue to configure "individual register".Click "Device"~-~->"individual register"
390
391 (% style="text-align:center" %)
392 [[image:1624434757597-117.png||class="img-thumbnail"]]
393
394 **Notes for registering device:**
395
396 Product: Previous product registration.
397
398 Node ID, Device Name: set according to your own needs.
399
400 Secret: need to be configured, will be used when connecting later
401
402 After configuration, click OK to generate a device ID and password, which will be used for device access later.
403
404 (% style="text-align:center" %)
405 [[image:1624436421499-613.png||class="img-thumbnail" height="499" width="700"]]
406
407 (% style="text-align:center" %)
408 [[image:1624437798012-126.png||class="img-thumbnail" height="366" width="500"]]
409
410 6. Connection authentication (use MQTT.fx tool to access the IoT platform)
411
412 (1)Open mqttClientIdGenerator tool Java(TM) Platform SE binary
413
414 **[[Download>>https://wecon-disk.oss-ap-southeast-1.aliyuncs.com/Download/WIKI/V-BOX/Demo/Huawei/mqttClientIdGenerator-19.2.0.zip]]**
415
416 (% style="text-align:center" %)
417 [[image:1624437573798-815.png||class="img-thumbnail" height="351" width="700"]]
418
419 (2)Fill in the device ID and secret (deviceid and secret generated when registering the device) to generate connection message
420
421 Client ID, user name, password
422
423 (% style="text-align:center" %)
424 [[image:1624437756866-251.png||class="img-thumbnail" height="405" width="700"]]
425
426 (3) Download certificate file"North-Beijing4"
427
428 [[https:~~/~~/support.huaweicloud.com/en-us/devg-iothub/iot_02_1004.html>>https://support.huaweicloud.com/en-us/devg-iothub/iot_02_1004.html]]
429
430 (% style="text-align:center" %)
431 [[image:1624438225398-363.png||class="img-thumbnail" height="403" width="800"]]
432
433 (% style="text-align:center" %)
434 [[image:1624438260025-610.png||class="img-thumbnail" height="408" width="700"]]
435
436 7.Run MQTTfx tool to connect with Huawei
437
438 Download link: [[http:~~/~~/mqttfx.jensd.de/index.php/download>>url:http://mqttfx.jensd.de/index.php/download]]
439
440 (1)Click on the setting ICON
441
442 (% style="text-align:center" %)
443 [[image:1624438821280-974.png||class="img-thumbnail" height="198" width="500"]]
444
445 (2)Fill in IIOT MQTT device access address, and configure authentication parameters.
446 First: It is the server and port connected to Huawei IOT, which can be viewed through the overview of the interface.
447
448 (% style="text-align:center" %)
449 [[image:1624439086268-985.png||class="img-thumbnail"]]
450
451 Domain name:iot-mqtts.cn-north-4.myhuaweicloud.com
452
453 Port: 8883
454
455 Client ID: check step 6
456
457 (% style="text-align:center" %)
458 [[image:1624439672168-492.png||class="img-thumbnail" height="458" width="600"]]
459
460 (3)Upload SSL certificate file,check step 6
461
462 Select folder java~-~->DigiCertGlobalRootCA.crt.pem and click OK or apply button
463
464 (% style="text-align:center" %)
465 [[image:1624439912938-659.png||class="img-thumbnail" height="458" width="600"]]
466
467 (4)Connect and test publish and subscribe
468
469 (% style="text-align:center" %)
470 [[image:1624440014872-688.png||class="img-thumbnail" height="232" width="700"]]
471
472 (% style="text-align:center" %)
473 [[image:1624440026937-386.png||class="img-thumbnail" height="215" width="700"]]
474
475 Huawei publish topic format: $oc/devices/{device_id}/sys/properties/report
476
477 (% style="text-align:center" %)
478 [[image:1624440404119-815.png||class="img-thumbnail"]]
479
480 Huawei subscribe topic format: **$oc/devices/{device_id}/sys/commands/#**
481
482 (% style="text-align:center" %)
483 [[image:1624447157493-672.png||class="img-thumbnail"]]
484
485 (% style="text-align:center" %)
486 [[image:1624447209982-715.png||class="img-thumbnail"]]
487
488 (5).How to configure to view the received data format intuitively, multiple products (Huawei) are required to configure the model. The specific steps are as follows:
489 ①Select the corresponding product from Huawei products to view
490
491 (% style="text-align:center" %)
492 [[image:1624440647663-632.png||class="img-thumbnail"]]
493
494 ②Custom model: used to display the service ID name of the configuration report.
495
496 (% style="text-align:center" %)
497 [[image:1624440793982-974.png||class="img-thumbnail" height="410" width="700"]]
498
499 (% style="text-align:center" %)
500 [[image:1624440883015-105.png||class="img-thumbnail" height="370" width="600"]]
501
502 ③Add property, ID of monitoring point, and data format:
503
504 (% style="text-align:center" %)
505 [[image:1624441052296-108.png||class="img-thumbnail" height="477" width="600"]]
506
507 ④After the configuration is complete, check the received data on the device
508
509 (% style="text-align:center" %)
510 [[image:1624441186851-536.png||class="img-thumbnail" height="434" width="700"]]
511
512 == **2.4 V-Box connect with Huawei platform** ==
513
514 In this demo,V-Box connects with Huawei by SSL certification.
515
516 1.Create a project access for Huawei IOT
517
518 2.configure MQTT configuration
519
520 (% style="text-align:center" %)
521 [[image:1624506363847-661.png||class="img-thumbnail" height="507" width="1000"]]
522
523 3.Create a script with the demo as below.
524
525 Script is as below
526
527 (% class="box infomessage" %)
528 (((
529 (% class="box infomessage" %)
530 (((
531 ~-~- mqtt.fx simulated access to Huawei Cloud IoT platform refer to 2.4
532 sprint = print
533
534 ~-~-Get custom configuration parameters (gateway customization information)
535 local CUSTOM = bns_get_config("bind")
536 local DS_ID = CUSTOM.DSID or "5dfa0700df1ae506179afb9c_wecon"
537
538
539 ~-~-OpenCloud mode interface, obtain MQTT information configured on cloud platform: (5 returned, respectively server address, client ID, connection table, last word table, certificate table)
540 local MQTT_URL, MQTT_CLIENTID, MQTT_CFG, MQTT_LWT, MQTT_CART = mqtt.setup_cfg()
541
542 MQTT_CFG.username =DS_ID
543 MQTT_CFG.password='d030d92338fcc18cd10fabb3003a4a0f6620fa6822cd3c23b1d9bc790200c6e7'
544 MQTT_CLIENTID = '5dfa0700df1ae506179afb9c_wecon_0_0_2019121819'
545
546 ~-~-publish topic format:$oc/devices/{device id}/sys/properties/report
547
548 local pub_RE_TOPIC = string.format('$oc/devices/60a71ccbbbe12002c08f3a1a_WECON/sys/properties/report')
549
550 ~-~-variate
551 local last_time = 0
552
553
554 ~-~-Timing principal function
555 function hwyiot.main()
556
557 sprint(os.date("%Y-%m-%d %H:%M %S", os.time()) .. " hwyiot.main start")
558 if g_mq then
559 if g_mq:isconnected() then
560 send_Data()
561 else
562 if os.time() - last_time > 20 then
563 last_time = os.time()
564 mymqtt_connect()
565 end
566 end
567 else
568 mymqtt_init()
569 end
570 sprint(os.date("%Y-%m-%d %H:%M %S", os.time()) .. " hwyiot.main end")
571 end
572
573 ~-~- initializationMQTT
574 function mymqtt_init()
575 sprint(string.format("mqtt init mqtt_url:%s mqtt_clientid:%s", MQTT_URL, MQTT_CLIENTID))
576 g_mq, err = mqtt.create(MQTT_URL, MQTT_CLIENTID) ~-~- Create the object and declare it as a global variable
577 if g_mq then
578 g_mq:on("message", mymqtt_msg_callback) ~-~- Register to receive message callbacks
579 sprint("mqtt init success")
580 else
581 sprint("mqtt init failed:", err)
582 end
583 end
584
585 ~-~- Connect to the MQTT server
586 function mymqtt_connect()
587 sprint("mqtt connecting...")
588 local stat, err = g_mq:connect(MQTT_CFG,MQTT_LWT, MQTT_CART)
589 if stat == nil then
590 sprint("mqtt connect failed:", err)
591 return
592 else
593 sprint("mqtt connected")
594 end
595
596 ~-~-subscribe topic format:$oc/devices/{device_id}/sys/commands/#
597
598 g_mq:subscribe('$oc/devices/60a71ccbbbe12002c08f3a1a_WECON/sys/commands/#', 0) 
599 end
600
601 ~-~- Receive the message callback function
602 function mymqtt_msg_callback(topic, msg)
603 sprint("recv data!")
604 sprint("topic:msg", topic,msg)
605 print(msg)
606 local revData = json.decode(msg)
607 \\end
608
609 ~-~-Send data
610 function send_Data()
611 local pub_data = {
612 msgType = 'deviceReq',
613 data = ~{~{
614 serviceId ='Battery',
615 serviceData={
616 batteryLevel = 55
617 }
618 }}
619 }
620 return g_mq:publish(pub_RE_TOPIC, json.encode(pub_data), 0, 0)
621 end
622 )))
623 )))
624
625 4.Download project access into V-Box to test in debug page
626
627 (% style="text-align:center" %)
628 [[image:1624506710354-406.png||class="img-thumbnail" height="658" width="1000"]]
629
630 (% style="text-align:center" %)
631 [[image:1624506666650-161.png||class="img-thumbnail" height="547" width="1000"]]
632
633 == **2.5 V-Box connect with AWS platform** ==
634
635 [[https:~~/~~/ftp.we-con.com.cn/Download/WIKI/V-BOX/Demo/AWS/AWS.zip>>https://ftp.we-con.com.cn/Download/WIKI/V-BOX/Demo/AWS/AWS.zip]]