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