Wiki source code of 2 Script

Version 12.2 by Leo Wei on 2022/07/09 17:41

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||height="182" width="1000" class="img-thumbnail"]]
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:V-BOX.V-Net.1\.User Manual.04 Lua Script.01 Lua Functions.WebHome]]
17
18 == **1.2 Arithmetic** ==
19
20 (% style="text-align:center" %)
21 [[image:1624249623612-177.png||height="337" width="400" class="img-thumbnail"]]
22
23 == **1.3 Set 100 to D0~-~-D19** ==
24
25 (% style="text-align:center" %)
26 [[image:1624249693457-742.png||height="135" width="400" class="img-thumbnail"]]
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||height="385" width="400" class="img-thumbnail"]]
36
37 Script is as below:
38
39 {{code language="Lua"}}
40 function sms.main()
41 ------send condition------
42 local temp1 = addr_getword("@Temperature1")
43 local temp2 = addr_getword("@Temperature2")
44 local temp3 = addr_getword("@Temperature3")
45 local timer = addr_getword("@Timer")
46 local tag = addr_getbit("@Tag")
47 ------lasting time------
48 if temp1 > 5 and temp2 > 10 and temp3 < 20 then
49     timer = timer + 1
50     addr_setword("@Timer",timer)
51 else
52     timer = 0
53     addr_setword("@Timer",timer)
54 end
55 ------send sms & output Y0------
56 if timer > 5 then
57     if tag == 0 then
58         send_sms_ira("19859254700","alarm trigger")
59         addr_setbit("@Tag",1)
60     end
61 elseif tag == 1 then
62 send_sms_ira("19859254700","alarm release")
63 addr_setbit("@Tag",0)
64 end
65 end
66 {{/code}}
67
68 == **1.5 Telegram notification** ==
69
70 This example shows how to use the Bot API to send message into Telegram group or channel. When monitoring bit "@HDX" changes, it will trigger and send the message. Please replace with your own Token and chat id.
71
72 {{code language="Lua"}}
73 local tempBit = 0
74 local tempWord = 0
75
76 local botToken = "5504549693:AAEy6a5G-sOF3CINONxMNABeYnoS4ABVlfg"
77 local chatID = "-641959124"--The chat id from Channel or Group
78
79 local https = require("https")
80 local json = require("json")
81
82 -- Send http.get request and return response result
83 function getHttpsUrl(url)
84 local body = {}
85 local bodyJson = json.encode(body)
86 local header = {}
87 header["content-type"] = "application/json"
88 local result_table, code, headers, status = https.request(url, bodyJson)
89 print("code:"..code)
90 if code~= 200 then
91 return
92 else
93 return body
94 end
95 end
96
97 function sendAlarm(telegramBotToken, message, telegramChatID)
98 local url = "https://api.telegram.org/bot"..telegramBotToken.."/sendMessage?text="..message.."&chat_id="..telegramChatID
99 --local url = 'http://v-box.net'
100 --local url = 'https://www.google.com/'
101 print("Get the link:"..url)
102 getHttpsUrl(url)
103 end
104
105
106 function AlarmNotificate.main()
107 local bitValue = addr_getbit("@HDX");
108 local message = ''
109 print("b=="..bitValue)
110 if bitValue == 1 and bitValue ~= tempBit then
111 message = 'Alarm triggered, the monitoring point test value is '.. bitValue
112 sendAlarm(botToken, message, chatID)
113 print("Notification pushed of triggering alarm,"..bitValue)
114 elseif bitValue == 0 and bitValue ~= tempBit then
115 message = 'Alarm dismissed, the monitoring point test value is '.. bitValue
116 sendAlarm(botToken, message, chatID)
117 print("Notification pushed of dismissing alarm,"..bitValue)
118 end
119 tempBit = bitValue----Prevent monitoring values from continuous being sent to the platform
120
121 local wordValue = addr_getword("@HDW10")
122 print("w=="..wordValue)
123 --dosomething
124 if wordValue >= 100 and wordValue ~= tempWord and tempWord <= 100 then
125 message = 'Word alarm triggered, the word value is '.. wordValue
126 sendAlarm(botToken, message, chatID)
127 print("Notification pushed of triggering alarm,"..wordValue)
128 elseif wordValue < 100 and wordValue ~= tempWord and tempWord >= 100 then
129 message = 'Word alarm dismissed, the word value is '.. wordValue
130 sendAlarm(botToken, message, chatID)
131 print("Notification pushed of dismissing alarm,"..wordValue)
132 end
133 tempWord = wordValue----Prevent monitoring values from continuous being sent to the platform
134 end
135 {{/code}}
136
137
138 = **2 V-Box connect with third part server** =
139
140 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.
141
142 (% class="mark" %)1.Europe server:eu.v-box.net
143
144 (% class="mark" %)2.Asean server:asean.v-box.net
145
146 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
147
148 OpenCloud platform is used in configuring the script.Then V-Box can connect with third part server.
149
150 Both mode can support script  to connect with third part server.the difference:
151
152 (% class="mark" %)1.If you want to store in WECON server ,please use V-NET.
153
154 (% class="mark" %)2.If your server requires SSL certificate to log in,please use OpenCloud.Because only OpenCloud platform can support to upload certificate
155
156 == **2.1 V-Box connect with customer server:grouprobotinfo.com** ==
157
158 This demo does not use SSL certification. Script is as below
159
160 Demo1:
161
162 {{code language="Lua"}}
163 -- Meta class
164 --main
165 function mq.main()
166  if not mq.m then
167   local err = ""
168
169   mq.m, err = mqtt.create("tcp://grouprobotinfo.com:1883", "ClienID")  -- create connection
170   if mq.m then
171    mq.config = {
172     username = "",-- ID
173     password = "",-- password
174     netway = 1, -- Ethernet connection, WIFI=1
175     -- keepalive = 100, -- Optional, set the connection heartbeat interval for 100 seconds.
176     -- cleansession = 0, -- Optional, keep session
177    }
178    mq.m:on("message", function(topic, msg) -- Register for receiving message callbacks
179     local str = string.format("%s:%s", topic, msg)
180     -- print("mqtt msg:", str) -- Print out the received topics and content
181    end
182    )
183    mq.m:on("offline", function (cause) -- Register for lost connection callbacks
184     -- addr_setstring("@xxx", "cause"..(cause or " got nil"))
185    end)
186    mq.m:on("arrived", function() -- Registration for sending messages to callbacks 
187     print("msg arrived")
188    end)
189   else
190    print("mqtt create failed:", err) -- Create object failed
191   end
192  else
193   if mq.m:isconnected() then -- If online, post a message
194      local phaseStatus ="unknow"
195      if addr_getbit("@Standby")== 1 then
196          phaseStatus = "Standby"
197      elseif addr_getbit("@Pre-Freeze")==1 then
198          phaseStatus= "Pre-Freeze"
199      elseif addr_getbit("@Prepare")==1 then
200          phaseStatus ="Prepare"
201      elseif addr_getbit("@Primary Dry")==1 then
202          phaseStatus = "Primary dry"
203      elseif addr_getbit("@Secondary Dry")==1 then
204          phaseStatus = "Secondary Dry"
205      end
206 --   print(addr_getbit("@Primary Dry"))
207 -------------------------------------------------------------------------------------------------------------------------
208      local activating ="unknow"
209      if addr_getbit("@Compressor")==1 then
210          activating = ",".."Compressor"
211      end
212      if addr_getbit("@Silicone Pump")==1 then
213          activating = activating..",".."Silicone Pump"
214      end
215      if addr_getbit("@Vacuum Pump")==1 then
216          activating = activating..",".."Vacuum Pump"
217      end
218      if addr_getbit("@Root Pump")==1 then
219          activating = activating..",".."Root Pump"
220      end
221      if addr_getbit("@Heater")==1 then
222          activating = activating..",".."Heater"
223      end
224      if addr_getbit("@Valve Silicone")==1 then
225          activating = activating..",".."Valve Silicone"
226      end
227      if addr_getbit("@Valve Ice Condenser")==1 then
228          activating = activating..",".."Valve Ice Condenser"
229      end
230      if addr_getbit("@Valve Vacuum Pump")==1 then
231          activating = activating..",".."Valve Vacuum Pump"
232      end
233      local pr_activating =string.sub(activating,2)
234     --  print(pr_activating)  
235
236
237
238      local status_text ="unknow"
239      if addr_getbit("@Status Run")==1 then
240          status_text = "RUNNING"
241      else
242          status_text = "STOP"
243      end
244 -------------------------------------------------------------------------------------------------------------------------      
245
246      local js =  {type="status",
247                   mc_name ="FD300",
248                   status=status_text,
249                   elapsed_time={
250                                 hour=addr_getword("@Elapsed Time (Hour)"),
251                                 min=addr_getword("@Elapsed Time (Minute)"),
252                                 sec=addr_getword("@Elapsed Time (Second)")
253                                 },
254                    phase = phaseStatus,
255                    step = addr_getword("@Step"),
256                    activating_output = pr_activating,
257                    sv=addr_getshort("@SV Silicone")/10,
258                    pv=addr_getshort("@PV Silicone")/10,
259                    product1=addr_getshort("@Product 1")/10,
260
261                    product2=addr_getshort("@Product 2")/10,
262                    product3=addr_getshort("@Product 3")/10,
263                    product4=addr_getshort("@Product 4")/10,
264                    ice1=addr_getshort("@Ice condenser 1")/10,
265                    ice2=addr_getshort("@Ice condenser 2")/10,
266                    vacuum=addr_getfloat("@Vacuum")
267
268                 }
269      local jsAlarm = {  HPC = addr_getbit("@B_25395#W0.00"),
270                         ODPC = addr_getbit("@B_25395#W0.01"),
271                         MTPC=addr_getbit("@B_25395#W0.02"),
272                         HTT = addr_getbit("@B_25395#W1.03"),
273                         CPC = addr_getbit("@B_25395#W0.08"),
274                         CPSP =addr_getbit("@B_25395#W1.00"),
275                         CPVP =addr_getbit("@B_25395#W0.10"),
276                         CPRP =addr_getbit("@B_25395#W0.11"),
277                         HP =addr_getbit("@B_25395#W1.01"),
278                         PP= addr_getbit("@B_25395#W1.02"),
279                         PO=addr_getbit("@B_25395#W0.07"),
280                         FSE=addr_getbit("@B_25395#W2.04"),
281                         AVVSVV=addr_getbit("@B_25395#W1.12"),
282                         ICHT=addr_getbit("@B_25395#W3.06")
283   
284                 }
285
286     -- ("@B_25395#CIO1.02")
287      mq.m:publish("mqtt-v-box-epsilon-fd300", json.encode(js) , 0, 0)
288      mq.m:publish("mqtt-v-box-epsilon-alarm-fd300", json.encode(jsAlarm) , 0, 0)
289   else
290    local stat, err = mq.m:connect(mq.config) -- connection
291    if stat == nil then --Determine whether to connect
292     print("mqtt connect failed:", err)
293     return -- Connection failed, return directly
294    end
295    mq.m:subscribe("mqtt-v-box-epsilon", 0)-- Subscribe to topics
296
297   end
298   -- mq.m:unsubscribe("stc/test")
299   -- mq.m:disconnect() -- close matt
300   -- mq.m:close() -- close clase
301  end
302 end
303 {{/code}}
304
305 == **2.2 V-Box connect with Azure platform** ==
306
307 In this demo,V-Box connects with Azure by SSL certification.
308
309 Video link: [[https:~~/~~/youtu.be/cdI6rIQcpMY?list=PL_Bpnb2RgaksCic9HCcVAZhU9sYwCRKzW>>https://youtu.be/cdI6rIQcpMY?list=PL_Bpnb2RgaksCic9HCcVAZhU9sYwCRKzW]]
310
311 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]]
312
313 Script is as below
314
315 {{code language="Lua"}}
316 --https://support.huaweicloud.com/qs-IoT/iot_05_0005.html  mqtt.fx monitor to connect azure iot
317 sprint = print
318
319 --Get custom configuration parameters (vbox custom information)
320 --local CUSTOM = bns_get_config("bind")
321 --local DS_ID = CUSTOM.DSID or "60a71ccbbbe12002c08f3a1a_WECON"
322
323
324
325 --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)
326 local MQTT_URL, MQTT_CLIENTID, MQTT_CFG, MQTT_LWT, MQTT_CART = mqtt.setup_cfg()
327
328 --MQTT_CFG.username = '60a71ccbbbe12002c08f3a1a_WECON'
329 --MQTT_CFG.password='wecon123'
330 --MQTT_CLIENTID = '60a71ccbbbe12002c08f3a1a_WECON_0_0_2021052110usernxame:60a71ccbbbe12002c08f3a1a_WECONpassword:a0a951581855aa8e0262129da6cf1b43f2c0ecfac4fa56117fc5a20c90be169a'
331
332 --publish to topics
333 local pub_RE_TOPIC = string.format('devices/wecon_02/messages/events/')
334 --Subscribe topics
335 local Subscribe_RE_TOPIC1 = string.format('devices/wecon_02/messages/devicebound/#')
336
337 --variable
338 local last_time = 0
339
340
341
342 --Timing main function
343 function Azure.main()
344
345    sprint(os.date("%Y-%m-%d %H:%M %S", os.time()) .. " Azureiot.main start")
346    if g_mq then
347         if g_mq:isconnected() then
348             send_Data()
349         else
350             if os.time() - last_time > 20 then
351                 last_time = os.time()
352                 mymqtt_connect()
353             end
354         end
355     else
356         mymqtt_init()
357     end
358     sprint(os.date("%Y-%m-%d %H:%M %S", os.time()) .. " Azureiot.main end")
359 end
360
361 -- Initialize MQTT
362 function mymqtt_init()
363     sprint(string.format("mqtt init mqtt_url:%s mqtt_clientid:%s", MQTT_URL, MQTT_CLIENTID))
364     g_mq, err = mqtt.create(MQTT_URL, MQTT_CLIENTID) -- Create the object and declare it as a global variable
365     if g_mq then
366         g_mq:on("message", mymqtt_msg_callback) -- Register to receive message callbacks
367         sprint("mqtt init success")
368     else
369         sprint("mqtt init failed:", err)
370     end
371 end
372
373 -- Connect to MQTT server
374 function mymqtt_connect()
375     sprint("mqtt connecting...")
376     local stat, err = g_mq:connect(MQTT_CFG,MQTT_LWT, MQTT_CART)
377     if stat == nil then
378         sprint("mqtt connect failed:", err)
379         return
380     else
381         sprint("mqtt connected")
382     end
383     g_mq:subscribe(Subscribe_RE_TOPIC1, 0) 
384 end
385
386 -- Receive MQTT message callback function
387 function mymqtt_msg_callback(topic, msg)
388     print("topic:",topic)
389     print("revdata:",msg)
390    -- local revData = json.decode(msg)
391  --  if topic == Subscribe_RE_TOPIC1 then --Process topic information subscribed from the cloud
392 -- if string.match(topic,Subscribe_RE_TOPIC1) then
393      --   print("topi11:",topic)
394        setValue(revData)
395    -- end
396 end
397
398 --Process the received data
399 --function setValue(revData)
400    -- if revData ~=nil then 
401  --       for i,v in pairs(revData) do
402   --          print("Data received:",i,v)
403   --      end
404    -- end
405 --end
406
407 --Get real-time data
408 function getData()
409     local jdata = {}
410     local addr = bns_get_alldata()
411     print(json.encode(addr))
412     for i,v in pairs(addr) do
413         if v[2] == 1 then
414            jdata[v[3]] = v[4]
415         end
416     end
417     return jdata
418 end 
419
420
421
422 --send data
423 function send_Data()
424     local pub_data = {100
425      --   services={{
426
427             --serviceId ='Temperature',
428            -- properties={
429                -- value = 55
430            -- },
431        -- }}
432 }
433 sprint(json.encode(pub_data))
434 print("..........",pub_RE_TOPIC)
435     return g_mq:publish(pub_RE_TOPIC, json.encode(pub_data), 0, 0)
436 end
437 {{/code}}
438
439 (% class="box infomessage" %)
440 (((
441
442 )))
443
444 == **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)** ==
445
446 1.Register a account: [[https:~~/~~/www.huaweicloud.com/intl/en-us/s/JUlPVERNJQ>>https://www.huaweicloud.com/intl/en-us/s/JUlPVERNJQ]]
447
448 2.log in the Huawei IOTDA
449
450 [[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]]
451
452 3.Create product
453
454 (% style="text-align:center" %)
455 [[image:1624433478954-859.png||height="497" width="1100" class="img-thumbnail"]]
456
457 4.Product name,manufacturer,device type and industry,set according to your own needs.
458
459 Protocol: MQTT
460
461 Data Type: JSON
462
463 After finishing configuration,please click "OK"
464
465 (% style="text-align:center" %)
466 [[image:1624433531968-337.png||height="568" width="700" class="img-thumbnail"]]
467
468 5.Device
469
470 After product register,continue to configure "individual register".Click "Device"~-~->"individual register"
471
472 (% style="text-align:center" %)
473 [[image:1624434757597-117.png||class="img-thumbnail"]]
474
475 **Notes for registering device:**
476
477 Product: Previous product registration.
478
479 Node ID, Device Name: set according to your own needs.
480
481 Secret: need to be configured, will be used when connecting later
482
483 After configuration, click OK to generate a device ID and password, which will be used for device access later.
484
485 (% style="text-align:center" %)
486 [[image:1624436421499-613.png||height="499" width="700" class="img-thumbnail"]]
487
488 (% style="text-align:center" %)
489 [[image:1624437798012-126.png||height="366" width="500" class="img-thumbnail"]]
490
491 6. Connection authentication (use MQTT.fx tool to access the IoT platform)
492
493 (1)Open mqttClientIdGenerator tool Java(TM) Platform SE binary
494
495 **[[Download>>https://wecon-disk.oss-ap-southeast-1.aliyuncs.com/Download/WIKI/V-BOX/Demo/Huawei/mqttClientIdGenerator-19.2.0.zip]]**
496
497 (% style="text-align:center" %)
498 [[image:1624437573798-815.png||height="351" width="700" class="img-thumbnail"]]
499
500 (2)Fill in the device ID and secret (deviceid and secret generated when registering the device) to generate connection message
501
502 Client ID, user name, password
503
504 (% style="text-align:center" %)
505 [[image:1624437756866-251.png||height="405" width="700" class="img-thumbnail"]]
506
507 (3) Download certificate file"North-Beijing4"
508
509 [[https:~~/~~/support.huaweicloud.com/en-us/devg-iothub/iot_02_1004.html>>https://support.huaweicloud.com/en-us/devg-iothub/iot_02_1004.html]]
510
511 (% style="text-align:center" %)
512 [[image:1624438225398-363.png||height="403" width="800" class="img-thumbnail"]]
513
514 (% style="text-align:center" %)
515 [[image:1624438260025-610.png||height="408" width="700" class="img-thumbnail"]]
516
517 7.Run MQTTfx tool to connect with Huawei
518
519 Download link: [[http:~~/~~/mqttfx.jensd.de/index.php/download>>url:http://mqttfx.jensd.de/index.php/download]]
520
521 (1)Click on the setting ICON
522
523 (% style="text-align:center" %)
524 [[image:1624438821280-974.png||height="198" width="500" class="img-thumbnail"]]
525
526 (2)Fill in IIOT MQTT device access address, and configure authentication parameters.
527 First: It is the server and port connected to Huawei IOT, which can be viewed through the overview of the interface.
528
529 (% style="text-align:center" %)
530 [[image:1624439086268-985.png||class="img-thumbnail"]]
531
532 Domain name:iot-mqtts.cn-north-4.myhuaweicloud.com
533
534 Port: 8883
535
536 Client ID: check step 6
537
538 (% style="text-align:center" %)
539 [[image:1624439672168-492.png||height="458" width="600" class="img-thumbnail"]]
540
541 (3)Upload SSL certificate file,check step 6
542
543 Select folder java~-~->DigiCertGlobalRootCA.crt.pem and click OK or apply button
544
545 (% style="text-align:center" %)
546 [[image:1624439912938-659.png||height="458" width="600" class="img-thumbnail"]]
547
548 (4)Connect and test publish and subscribe
549
550 (% style="text-align:center" %)
551 [[image:1624440014872-688.png||height="232" width="700" class="img-thumbnail"]]
552
553 (% style="text-align:center" %)
554 [[image:1624440026937-386.png||height="215" width="700" class="img-thumbnail"]]
555
556 Huawei publish topic format: $oc/devices/{device_id}/sys/properties/report
557
558 (% style="text-align:center" %)
559 [[image:1624440404119-815.png||class="img-thumbnail"]]
560
561 Huawei subscribe topic format: **$oc/devices/{device_id}/sys/commands/#**
562
563 (% style="text-align:center" %)
564 [[image:1624447157493-672.png||class="img-thumbnail"]]
565
566 (% style="text-align:center" %)
567 [[image:1624447209982-715.png||class="img-thumbnail"]]
568
569 (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:
570 ①Select the corresponding product from Huawei products to view
571
572 (% style="text-align:center" %)
573 [[image:1624440647663-632.png||class="img-thumbnail"]]
574
575 ②Custom model: used to display the service ID name of the configuration report.
576
577 (% style="text-align:center" %)
578 [[image:1624440793982-974.png||height="410" width="700" class="img-thumbnail"]]
579
580 (% style="text-align:center" %)
581 [[image:1624440883015-105.png||height="370" width="600" class="img-thumbnail"]]
582
583 ③Add property, ID of monitoring point, and data format:
584
585 (% style="text-align:center" %)
586 [[image:1624441052296-108.png||height="477" width="600" class="img-thumbnail"]]
587
588 ④After the configuration is complete, check the received data on the device
589
590 (% style="text-align:center" %)
591 [[image:1624441186851-536.png||height="434" width="700" class="img-thumbnail"]]
592
593 == **2.4 V-Box connect with Huawei platform** ==
594
595 In this demo,V-Box connects with Huawei by SSL certification.
596
597 1.Create a project access for Huawei IOT
598
599 2.configure MQTT configuration
600
601 (% style="text-align:center" %)
602 [[image:1624506363847-661.png||height="507" width="1000" class="img-thumbnail"]]
603
604 3.Create a script with the demo as below.
605
606 Script is as below
607
608 (% class="box infomessage" %)
609 (((
610 (% class="box infomessage" %)
611 (((
612 ~-~- mqtt.fx simulated access to Huawei Cloud IoT platform refer to 2.4
613 sprint = print
614
615 ~-~-Get custom configuration parameters (gateway customization information)
616 local CUSTOM = bns_get_config("bind")
617 local DS_ID = CUSTOM.DSID or "5dfa0700df1ae506179afb9c_wecon"
618
619
620 ~-~-OpenCloud mode interface, obtain MQTT information configured on cloud platform: (5 returned, respectively server address, client ID, connection table, last word table, certificate table)
621 local MQTT_URL, MQTT_CLIENTID, MQTT_CFG, MQTT_LWT, MQTT_CART = mqtt.setup_cfg()
622
623 MQTT_CFG.username =DS_ID
624 MQTT_CFG.password='d030d92338fcc18cd10fabb3003a4a0f6620fa6822cd3c23b1d9bc790200c6e7'
625 MQTT_CLIENTID = '5dfa0700df1ae506179afb9c_wecon_0_0_2019121819'
626
627 ~-~-publish topic format:$oc/devices/{device id}/sys/properties/report
628
629 local pub_RE_TOPIC = string.format('$oc/devices/60a71ccbbbe12002c08f3a1a_WECON/sys/properties/report')
630
631 ~-~-variate
632 local last_time = 0
633
634
635 ~-~-Timing principal function
636 function hwyiot.main()
637
638 sprint(os.date("%Y-%m-%d %H:%M %S", os.time()) .. " hwyiot.main start")
639 if g_mq then
640 if g_mq:isconnected() then
641 send_Data()
642 else
643 if os.time() - last_time > 20 then
644 last_time = os.time()
645 mymqtt_connect()
646 end
647 end
648 else
649 mymqtt_init()
650 end
651 sprint(os.date("%Y-%m-%d %H:%M %S", os.time()) .. " hwyiot.main end")
652 end
653
654 ~-~- initializationMQTT
655 function mymqtt_init()
656 sprint(string.format("mqtt init mqtt_url:%s mqtt_clientid:%s", MQTT_URL, MQTT_CLIENTID))
657 g_mq, err = mqtt.create(MQTT_URL, MQTT_CLIENTID) ~-~- Create the object and declare it as a global variable
658 if g_mq then
659 g_mq:on("message", mymqtt_msg_callback) ~-~- Register to receive message callbacks
660 sprint("mqtt init success")
661 else
662 sprint("mqtt init failed:", err)
663 end
664 end
665
666 ~-~- Connect to the MQTT server
667 function mymqtt_connect()
668 sprint("mqtt connecting...")
669 local stat, err = g_mq:connect(MQTT_CFG,MQTT_LWT, MQTT_CART)
670 if stat == nil then
671 sprint("mqtt connect failed:", err)
672 return
673 else
674 sprint("mqtt connected")
675 end
676
677 ~-~-subscribe topic format:$oc/devices/{device_id}/sys/commands/#
678
679 g_mq:subscribe('$oc/devices/60a71ccbbbe12002c08f3a1a_WECON/sys/commands/#', 0) 
680 end
681
682 ~-~- Receive the message callback function
683 function mymqtt_msg_callback(topic, msg)
684 sprint("recv data!")
685 sprint("topic:msg", topic,msg)
686 print(msg)
687 local revData = json.decode(msg)
688 \\end
689
690 ~-~-Send data
691 function send_Data()
692 local pub_data = {
693 msgType = 'deviceReq',
694 data = ~{~{
695 serviceId ='Battery',
696 serviceData={
697 batteryLevel = 55
698 }
699 }}
700 }
701 return g_mq:publish(pub_RE_TOPIC, json.encode(pub_data), 0, 0)
702 end
703 )))
704 )))
705
706 4.Download project access into V-Box to test in debug page
707
708 (% style="text-align:center" %)
709 [[image:1624506710354-406.png||height="658" width="1000" class="img-thumbnail"]]
710
711 (% style="text-align:center" %)
712 [[image:1624506666650-161.png||height="547" width="1000" class="img-thumbnail"]]
713
714 == **2.5 V-Box connect with AWS platform** ==
715
716 === **Log in AWS** ===
717
718 Login aws account and click“Connect an IoT device”
719
720 [[image:image-20220709165402-1.png]]
721
722 [[image:image-20220709165402-2.png]]
723
724
725 === **Create policy** ===
726
727 Click “Secure”~-~-->“Policies”~-~-->“Create policy”~-~-->Click “Create”
728
729 [[image:image-20220709165402-3.png]]
730
731 Name the policy~-~-->Click “JSON”~-~-->Copy the following content~-~-->Click “Create”
732
733 [[image:image-20220709165402-5.png]]
734
735 [[image:image-20220709165402-4.png]]
736
737 {{code language="java"}}
738 {
739
740   "Version": "2012-10-17",
741
742   "Statement": [
743
744     {
745
746       "Effect": "Allow",
747
748       "Action": [
749
750         "iot:Connect",
751
752         "iot:Publish",
753
754         "iot:Subscribe",
755
756         "iot:Receive",
757
758         "greengrass:Discover"
759
760       ],
761
762       "Resource": "*"
763
764     }
765
766   ]
767
768 }
769 {{/code}}
770
771 1. **Create things**
772
773 Click “Manage”~-~-->“Things”~-~-->“Create things”~-~-->“Create single thing”
774
775
776 |
777 | |[[image:image-20220709165402-6.png]]
778
779 |
780 | |[[image:image-20220709165402-7.png]]
781
782 |
783 | |[[image:image-20220709165402-8.png]]
784
785 Name the thing~-~-->Click “Next”
786
787
788 Select the way to create certificate
789
790
791 |
792 | |[[image:image-20220709165402-9.png]]
793
794 Select policy
795
796
797 |
798 | |[[image:image-20220709165402-10.png]]
799
800
801
802
803
804
805
806
807 |
808 | |[[image:image-20220709165402-11.png]]
809
810
811
812
813
814 1. **Test with MQTT.fx tool**
815
816 Click “View Setting” to get the “Broker Adress”
817
818
819 |
820 | |[[image:image-20220709165402-12.png]]
821
822 |
823 | |[[image:image-20220709165402-13.png]]
824
825
826
827
828
829
830
831 |
832 | |[[image:image-20220709165402-14.png]]
833
834 Create one connection in MQTT.fx tool, set broker port as 8883.
835
836 Upload the CA File, Client Certificate File, Client Key File
837
838
839 |
840 | |[[image:image-20220709165402-15.png]]
841
842 Publish message to topic “TEST”
843
844
845 |
846 | |[[image:image-20220709165402-16.png]]
847
848 |
849 | |[[image:image-20220709165402-17.png]]
850
851 Click”Test”~-~-->”MQTT test client”~-~-->”Subscrible to a topic”, to get message publish from MQTT.fx tool.
852
853 And we can also send message form AWS platform to MQTT.fx tool.
854
855
856 |
857 | |[[image:image-20220709165402-18.png]]
858
859 1. **Configurate in CloudTool**
860
861 Copy the same setting in MQTT.fx to MQTT configuration
862
863
864 |
865 | |[[image:image-20220709165402-19.png]]
866
867 Add a lua script and copy the lua demo into it.
868
869
870 |
871 | |[[image:image-20220709165402-20.png]]
872
873
874 sprint = print
875
876 ~-~-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)
877
878 local MQTT_URL, MQTT_CLIENTID, MQTT_CFG, MQTT_LWT, MQTT_CART = mqtt.setup_cfg()
879
880 ~-~-publish to topics
881
882 local pub_RE_TOPIC = string.format('TEST')
883
884 ~-~-Subscribe topics
885
886 local Subscribe_RE_TOPIC1 = string.format('TEST')
887
888 ~-~-variable
889
890 local last_time = 0
891
892 ~-~-Timing main function
893
894 function aws.main()
895
896 sprint(os.date("%Y-%m-%d %H:%M %S", os.time()) .. " aws.main start")
897
898 if g_mq then
899
900 if g_mq:isconnected() then
901
902 send_Data()
903
904 else
905
906 if os.time() - last_time > 5 then
907
908 last_time = os.time()
909
910 mymqtt_connect()
911
912 end
913
914 end
915
916 else
917
918 mymqtt_init()
919
920 end
921
922 sprint(os.date("%Y-%m-%d %H:%M %S", os.time()) .. " aws.main end")
923
924 end
925
926
927 ~-~- Initialize MQTT
928
929 function mymqtt_init()
930
931 sprint(string.format("mqtt init mqtt_url:%s mqtt_clientid:%s", MQTT_URL, MQTT_CLIENTID))
932
933 g_mq, err = mqtt.create(MQTT_URL, MQTT_CLIENTID) ~-~- Create the object and declare it as a global variable
934
935 if g_mq then
936
937 g_mq:on("message", mymqtt_msg_callback) ~-~- Register to receive message callbacks
938
939 sprint("mqtt init success")
940
941 else
942
943 sprint("mqtt init failed:", err)
944
945 end
946
947 end
948
949 ~-~- Connect to MQTT server
950
951 function mymqtt_connect()
952
953 sprint("mqtt connecting...")
954
955 local stat, err = g_mq:connect(MQTT_CFG,MQTT_LWT, MQTT_CART)
956
957 if stat == nil then
958
959 sprint("mqtt connect failed:", err)
960
961 return
962
963 else
964
965 sprint("mqtt connected")
966
967 end
968
969 g_mq:subscribe(TEST, 0)
970
971 end
972
973 ~-~- Receive MQTT message callback function
974
975 function mymqtt_msg_callback(topic, msg)
976
977 print("topic:",topic)
978
979 print("revdata:",msg)
980
981 local revData = json.decode(msg)
982
983 print (revData)
984
985 if topic == Subscribe_RE_TOPIC1 then ~-~-Process topic information subscribed from the cloud
986
987 if string.match(topic,Subscribe_RE_TOPIC1) then
988
989 ~-~-if revData ~~= nil then
990
991 for k,v in pairs (revData) do
992
993 print("printing revdata after kv here")
994
995 print (k,v)
996
997 end
998
999 print ("current state is",fanstate)
1000
1001 ~-~-end
1002
1003 end
1004
1005 end
1006
1007 end
1008
1009
1010 ~-~-Get real-time data
1011
1012 function getData()
1013
1014 local jdata = {}
1015
1016 local addr = bns_get_alldata()
1017
1018 print(json.encode(addr))
1019
1020 for i,v in pairs(addr) do
1021
1022 if v[2] == 1 then
1023
1024 jdata[v[3]] = v[4]
1025
1026 end
1027
1028 end
1029
1030 return jdata
1031
1032 end
1033
1034 ~-~-send data
1035
1036 function send_Data()
1037
1038 local pub_data =
1039
1040 {
1041
1042 123
1043
1044 }
1045
1046 sprint(json.encode(pub_data))
1047
1048 print("..........",pub_RE_TOPIC)
1049
1050 return g_mq:publish(pub_RE_TOPIC, json.encode(pub_data), 0, 0)
1051
1052 end
1053
1054
1055
1056 Get message in AWS
1057
1058
1059 |
1060 | |[[image:image-20220709165402-21.png]]