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