Changes for page 2 Script
Last modified by Devin Chen on 2025/06/06 14:03
From version 28.1
edited by Hunter
on 2022/11/21 17:01
on 2022/11/21 17:01
Change comment:
There is no comment for this version
To version 56.1
edited by Theodore Xu
on 2023/08/24 17:12
on 2023/08/24 17:12
Change comment:
There is no comment for this version
Summary
-
Page properties (2 modified, 0 added, 0 removed)
-
Attachments (0 modified, 1 added, 0 removed)
Details
- Page properties
-
- Author
-
... ... @@ -1,1 +1,1 @@ 1 -XWiki. Hunter1 +XWiki.AiXia - Content
-
... ... @@ -27,25 +27,19 @@ 27 27 28 28 == **1.4 Short message** == 29 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.30 +The following demo shows that when the alarm condition is reached: temp1 > 5 & temp2 >10 & temp3 < 20(lasts more than 5 seconds) , then send an "alarm trigger" sms. 31 31 32 -When the alarm condition is released,then send an "alarm release" sms. 32 +When the alarm condition is released,then send an "alarm release" sms. Script is as below: 33 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 -((( 34 +{{code language="lua"}} 41 41 function sms.main() 42 - ~-~-~-~-~-~-send condition~-~-~-~-~-~-36 +------send condition------ 43 43 local temp1 = addr_getword("@Temperature1") 44 44 local temp2 = addr_getword("@Temperature2") 45 45 local temp3 = addr_getword("@Temperature3") 46 46 local timer = addr_getword("@Timer") 47 47 local tag = addr_getbit("@Tag") 48 - ~-~-~-~-~-~-lasting time~-~-~-~-~-~-42 +------lasting time------ 49 49 if temp1 > 5 and temp2 > 10 and temp3 < 20 then 50 50 timer = timer + 1 51 51 addr_setword("@Timer",timer) ... ... @@ -53,7 +53,7 @@ 53 53 timer = 0 54 54 addr_setword("@Timer",timer) 55 55 end 56 - ~-~-~-~-~-~-send sms & output Y0~-~-~-~-~-~-50 +------send sms & output Y0------ 57 57 if timer > 5 then 58 58 if tag == 0 then 59 59 send_sms_ira("19859254700","alarm trigger") ... ... @@ -64,7 +64,7 @@ 64 64 addr_setbit("@Tag",0) 65 65 end 66 66 end 67 - )))61 +{{/code}} 68 68 69 69 == **1.5 Telegram notification** == 70 70 ... ... @@ -145,7 +145,7 @@ 145 145 146 146 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. 147 147 148 -{{code}} 142 +{{code language="lua"}} 149 149 local tempBit = 0 150 150 local tempWord = 0 151 151 ... ... @@ -349,6 +349,135 @@ 349 349 end 350 350 {{/code}} 351 351 346 +== **1.8 How to parse value from HTTP response body** == 347 + 348 +This example use [[https:~~/~~/www.weatherapi.com/>>https://www.weatherapi.com/]] as example, to show how to parse value from HTTP response body. When we input the city name into address "@HDW5050": 349 + 350 +(% style="text-align:center" %) 351 +[[image:InputHTTPparameter.png]] 352 + 353 +Then the response body would be like as following: 354 + 355 +{{code language="json"}} 356 +{ 357 + "location": { 358 + "name": "Madrid", 359 + "region": "Madrid", 360 + "country": "Spain", 361 + "lat": 40.4, 362 + "lon": -3.68, 363 + "tz_id": "Europe/Madrid", 364 + "localtime_epoch": 1669022636, 365 + "localtime": "2022-11-21 10:23" 366 + }, 367 + "current": { 368 + "last_updated_epoch": 1669022100, 369 + "last_updated": "2022-11-21 10:15", 370 + "temp_c": 13.0, 371 + "temp_f": 55.4, 372 + "is_day": 1, 373 + "condition": { 374 + "text": "Partly cloudy", 375 + "icon": "//cdn.weatherapi.com/weather/64x64/day/116.png", 376 + "code": 1003 377 + }, 378 + "wind_mph": 11.9, 379 + "wind_kph": 19.1, 380 + "wind_degree": 210, 381 + "wind_dir": "SSW", 382 + "pressure_mb": 1015.0, 383 + "pressure_in": 29.97, 384 + "precip_mm": 0.0, 385 + "precip_in": 0.0, 386 + "humidity": 88, 387 + "cloud": 75, 388 + "feelslike_c": 10.8, 389 + "feelslike_f": 51.4, 390 + "vis_km": 10.0, 391 + "vis_miles": 6.0, 392 + "uv": 3.0, 393 + "gust_mph": 22.1, 394 + "gust_kph": 35.6 395 + } 396 +} 397 +{{/code}} 398 + 399 +(% class="wikigeneratedid" %) 400 +So we decode json into lua object to assign the value into addresses HDW6060(temperature), HDW7070(humidity), the code example like follows: 401 + 402 +{{code language="lua"}} 403 +local APIkey = '70faaecf926b4341b1974006221711' 404 + 405 + 406 +local http = require("socket.http") 407 +local json = require("json") 408 + 409 +-- Send http.get request and return response result 410 +function getHttpsUrl(url) 411 + local result_table, code, headers, status = http.request(url) 412 + print("code:"..code) 413 + if code~= 200 then 414 + return 415 + else 416 + return result_table 417 + end 418 +end 419 + 420 +function sendAPI(key, city) 421 + local url = "http://api.weatherapi.com/v1/current.json?key="..key.."&q="..city.."&aqi=no" 422 + --local url = 'http://v-box.net' 423 + --local url = 'https://www.google.com/' 424 + --http://api.weatherapi.com/v1/current.json?key=70faaecf926b4341b1974006221711&q=Barcelona&aqi=no 425 + print("Get the link:"..url) 426 + local body = getHttpsUrl(url) 427 + --print(body) 428 + local jsonBody = json.decode(body) 429 + --print(jsonBody["current"]["temp_c"]) 430 + --print(type(jsonBody["current"]["temp_c"])) 431 + --print(type(jsonBody["current"]["humidity"])) 432 + addr_setfloat("@HDW6060", jsonBody["current"]["temp_c"]) 433 + addr_setword("@HDW7070", jsonBody["current"]["humidity"]) 434 +end 435 + 436 + 437 +function Weather.main() 438 + local cityName = addr_getstring("@HDW5050",6) 439 + print("cityName: "..cityName) 440 + sendAPI(APIkey, cityName) 441 +end 442 +{{/code}} 443 + 444 +== **1.9 High-Low Byte Switch** == 445 + 446 +The following example is converting the floating number from order 1234 to order 3412, and formating output the number with 2 decimal point. About which high-low word order corresponding to which value, please refer to the [[Address Operation Table>>doc:V-BOX.V-Net.Manual.04 Lua Script.01 Lua Functions.WebHome||anchor="H2Addressoperation"]]. 447 + 448 +{{code language="lua"}} 449 +function highLowByteSwitch(floatNumber) 450 + addr_setfloat("@W_0#HDW23036",floatNumber,0,2) 451 + local newFloat = addr_getfloat("@W_0#HDW23036") 452 + local formattedFloat = string.format("%.2f",newFloat) 453 + print("The formatted float value is the : "..formattedFloat) 454 + return formattedFloat 455 +end 456 +{{/code}} 457 + 458 +== **1.10 Read 64bits Unsigned Value** == 459 + 460 +In our built-in function library doesn't have the function for reading 64-bit unsigned format value, so the following function is for solve this. But if the number is greater 2^53, the precision will be lost. So the final result will be a little bit different from the original value. 461 + 462 +{{code language="lua"}} 463 +function addr_getquatra(address) 464 + local highAddress = addr_newnoaddr(address,2) 465 + local low32 = addr_getdword(address) 466 + local high32 = addr_getdword(highAddress) 467 + --print("the low number is "..low32) 468 + --print("the high number is "..high32) 469 + local formatVal = string.format("%64.0f",2^32*high32+low32) 470 + print("the format value is ".. formatVal) 471 + return formatVal 472 +end 473 +{{/code}} 474 + 352 352 = **2 V-Box connect with third part server** = 353 353 354 354 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. ... ... @@ -367,17 +367,38 @@ 367 367 368 368 (% class="mark" %)2.If your server requires SSL certificate to log in,please use OpenCloud.Because only OpenCloud platform can support to upload certificate 369 369 370 - (% class="wikigeneratedid" %)493 +{{info}} 371 371 **✎Note: **Before program the script of MQTT, please make sure the server(MQTT broker) can be connected through MQTT Client tool. 495 +{{/info}} 372 372 373 373 (% class="wikigeneratedid" %) 374 374 Tool link: **[[MQTT.fx>>http://mqttfx.jensd.de/index.php/download]]** 375 375 376 -== **2.1 V-Box connect with test server(General Example)** ==500 +== **2.1 Test server(General Example)** == 377 377 502 +The following example is trying to publish to the topic "testtopic/test/no1/7890", and subscribe the topic "testtopic/test/no1/123456". 503 + 504 +And the JSON message is like follows: 505 + 506 +{{code language="JSON"}} 507 +{ 508 + "timestamp": 1631152760, 509 + "messageId": 1, 510 + "event": "test_data", 511 + "mfrs": "HMI/box", 512 + "data": 513 + { 514 + "id" : 1436217747670454274, 515 + "waterlevel" : 48, 516 + "temperture" : 23 517 + } 518 +} 519 +{{/code}} 520 + 378 378 {{code language="lua"}} 379 379 --MQTT configuration table 380 380 local MQTT_CFG={} 524 +--if there is no need the username and password, please put them as "" 381 381 MQTT_CFG.username = "weconsupport" 382 382 MQTT_CFG.password = "123456" 383 383 MQTT_CFG.netway = 0 ... ... @@ -416,9 +416,13 @@ 416 416 --initialize mqtt 417 417 function mqtt_init() 418 418 print(string.format("mqtt init mqtt_url:%s mqtt_clientid:%s", MQTT_URL, MQTT_CLIENT_ID)) 563 + if g_mq then 564 + mqtt.close(g_mq) --Close mqtt object 565 + end 419 419 g_mq, err = mqtt.create(MQTT_URL, MQTT_CLIENT_ID) -- create mqtt object,and declare it as a global variable 420 420 if g_mq then 421 421 g_mq:on("message", mqtt_msg_callback) -- Register a callback for receiving messages 569 + g_mq:on("offline", mqtt_msg_offline) -- Register a callback for offline 422 422 print("mqtt init success") 423 423 else 424 424 print("mqtt init failed:", err) ... ... @@ -438,6 +438,11 @@ 438 438 g_mq:subscribe(SUBSCRIBE_TOPIC, 0) 439 439 end 440 440 589 +--Offline callback function 590 +function mqtt_msg_offline(cause) 591 + print("mqtt offline, cause:", cause) 592 +end 593 + 441 441 -- Received message callback function 442 442 function mqtt_msg_callback(topic, msg) 443 443 print("topic:", topic) ... ... @@ -476,9 +476,11 @@ 476 476 if g_mq:isconnected() then 477 477 send_data() 478 478 else 479 - --if exceed 20sec not connect, reconnect once480 - if os.time() - LAST_TIME > 20then632 + --if exceed 5 sec not connect, reconnect once 633 + if os.time() - LAST_TIME > 5 then 481 481 LAST_TIME = os.time() 635 + --reinitial the mqtt object 636 + mqtt_init() 482 482 --connect to mqtt or reconnect 483 483 mqtt_connect() 484 484 end ... ... @@ -491,7 +491,7 @@ 491 491 end 492 492 {{/code}} 493 493 494 -== **2.2 V-Box connect with customer server:grouprobotinfo.com** ==649 +== **2.2 Customer server:grouprobotinfo.com** == 495 495 496 496 This demo does not use SSL certification. Script is as below 497 497 ... ... @@ -632,7 +632,7 @@ 632 632 end 633 633 {{/code}} 634 634 635 -== **2.3 V-Box connect withAzure platform** ==790 +== **2.3 Azure platform** == 636 636 637 637 In this demo,V-Box connects with Azure by SSL certification. 638 638 ... ... @@ -763,8 +763,14 @@ 763 763 end 764 764 ))) 765 765 766 -== **2.4 H ow 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)** ==921 +== **2.4 Huawei platform** == 767 767 923 + 924 + 925 +{{info}} 926 +**✎Note**:**Huawei IOT DA function is only in China area.If you want this function,you need to use chinese mobile to register** 927 +{{/info}} 928 + 768 768 1.Register a account: [[https:~~/~~/www.huaweicloud.com/intl/en-us/s/JUlPVERNJQ>>https://www.huaweicloud.com/intl/en-us/s/JUlPVERNJQ]] 769 769 770 770 2.log in the Huawei IOTDA ... ... @@ -1033,7 +1033,7 @@ 1033 1033 (% style="text-align:center" %) 1034 1034 [[image:1624506666650-161.png||height="547" width="1000" class="img-thumbnail"]] 1035 1035 1036 -== **2.6 V-Box connect withAWS platform** ==1197 +== **2.6 AWS platform** == 1037 1037 1038 1038 === **Log in AWS** === 1039 1039 ... ... @@ -1058,35 +1058,20 @@ 1058 1058 1059 1059 {{code language="java"}} 1060 1060 { 1061 - 1062 1062 "Version": "2012-10-17", 1063 - 1064 1064 "Statement": [ 1065 - 1066 1066 { 1067 - 1068 1068 "Effect": "Allow", 1069 - 1070 1070 "Action": [ 1071 - 1072 1072 "iot:Connect", 1073 - 1074 1074 "iot:Publish", 1075 - 1076 1076 "iot:Subscribe", 1077 - 1078 1078 "iot:Receive", 1079 - 1080 1080 "greengrass:Discover" 1081 - 1082 1082 ], 1083 - 1084 1084 "Resource": "*" 1085 - 1086 1086 } 1087 - 1088 1088 ] 1089 - 1090 1090 } 1091 1091 {{/code}} 1092 1092 ... ... @@ -1113,7 +1113,7 @@ 1113 1113 [[image:image-20220709165402-11.png]] 1114 1114 1115 1115 1116 -=== ** Test withMQTT.fx tool** ===1262 +=== **MQTT.fx tool** === 1117 1117 1118 1118 Click “View Setting” to get the “Broker Adress” 1119 1119 ... ... @@ -1141,7 +1141,7 @@ 1141 1141 1142 1142 [[image:image-20220709165402-18.png]] 1143 1143 1144 -=== **C onfigurate in CloudTool** ===1290 +=== **CloudTool** === 1145 1145 1146 1146 Copy the same setting in MQTT.fx to MQTT configuration 1147 1147 ... ... @@ -1151,185 +1151,190 @@ 1151 1151 1152 1152 [[image:image-20220709165402-20.png]] 1153 1153 1300 +{{info}} 1301 +**✎Note:** Before using the following demo script, please make sure the V-Box firmware is newer than 22110701 1302 +{{/info}} 1303 + 1304 +{{code language="lua"}} 1154 1154 sprint = print 1155 1155 1156 - ~-~-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)1307 +--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) 1157 1157 1158 1158 local MQTT_URL, MQTT_CLIENTID, MQTT_CFG, MQTT_LWT, MQTT_CART = mqtt.setup_cfg() 1159 1159 1160 - ~-~-publish to topics1311 +--publish to topics 1161 1161 1162 1162 local pub_RE_TOPIC = string.format('TEST') 1163 1163 1164 - ~-~-Subscribe topics1315 +--Subscribe topics 1165 1165 1166 1166 local Subscribe_RE_TOPIC1 = string.format('TEST') 1167 1167 1168 - ~-~-variable1319 +--variable 1169 1169 1170 1170 local last_time = 0 1171 1171 1172 - ~-~-Timing main function1323 +--Timing main function 1173 1173 1174 1174 function aws.main() 1175 1175 1176 - sprint(os.date("%Y-%m-%d %H:%M %S", os.time()) .. " aws.main start") 1327 + sprint(os.date("%Y-%m-%d %H:%M %S", os.time()) .. " aws.main start") 1177 1177 1178 - if g_mq then 1329 + if g_mq then 1179 1179 1180 - if g_mq:isconnected() then 1331 + if g_mq:isconnected() then 1181 1181 1182 - send_Data() 1333 + send_Data() 1183 1183 1184 - else 1335 + else 1185 1185 1186 - if os.time() - last_time > 5 then 1337 + if os.time() - last_time > 5 then 1187 1187 1188 - last_time = os.time() 1339 + last_time = os.time() 1189 1189 1190 - mymqtt_connect() 1341 + mymqtt_connect() 1191 1191 1343 + end 1344 + 1192 1192 end 1193 1193 1194 - e nd1347 + else 1195 1195 1196 - else1349 + mymqtt_init() 1197 1197 1198 - mymqtt_init()1351 + end 1199 1199 1200 - end 1353 + sprint(os.date("%Y-%m-%d %H:%M %S", os.time()) .. " aws.main end") 1201 1201 1202 - sprint(os.date("%Y-%m-%d %H:%M %S", os.time()) .. " aws.main end") 1203 - 1204 1204 end 1205 1205 1206 1206 1207 -~-~- Initialize MQTT 1208 1208 1359 +-- Initialize MQTT 1360 + 1209 1209 function mymqtt_init() 1210 1210 1211 - sprint(string.format("mqtt init mqtt_url:%s mqtt_clientid:%s", MQTT_URL, MQTT_CLIENTID)) 1363 + sprint(string.format("mqtt init mqtt_url:%s mqtt_clientid:%s", MQTT_URL, MQTT_CLIENTID)) 1212 1212 1213 - g_mq, err = mqtt.create(MQTT_URL, MQTT_CLIENTID) ~-~- Create the object and declare it as a global variable1365 + g_mq, err = mqtt.create(MQTT_URL, MQTT_CLIENTID, 1) -- Create the object and declare it as a global variable, 1 means using the domain to connect 1214 1214 1215 - if g_mq then 1367 + if g_mq then 1216 1216 1217 - g_mq:on("message", mymqtt_msg_callback) ~-~- Register to receive message callbacks1369 + g_mq:on("message", mymqtt_msg_callback) -- Register to receive message callbacks 1218 1218 1219 - sprint("mqtt init success") 1371 + sprint("mqtt init success") 1220 1220 1221 - else 1373 + else 1222 1222 1223 - sprint("mqtt init failed:", err) 1375 + sprint("mqtt init failed:", err) 1224 1224 1225 - end 1377 + end 1226 1226 1227 1227 end 1228 1228 1229 - ~-~- Connect to MQTT server1381 +-- Connect to MQTT server 1230 1230 1231 1231 function mymqtt_connect() 1232 1232 1233 - sprint("mqtt connecting...") 1385 + sprint("mqtt connecting...") 1234 1234 1235 - local stat, err = g_mq:connect(MQTT_CFG,MQTT_LWT, MQTT_CART) 1387 + local stat, err = g_mq:connect(MQTT_CFG,MQTT_LWT, MQTT_CART) 1236 1236 1237 - if stat == nil then 1389 + if stat == nil then 1238 1238 1239 - sprint("mqtt connect failed:", err) 1391 + sprint("mqtt connect failed:", err) 1240 1240 1241 - return 1393 + return 1242 1242 1243 - else 1395 + else 1244 1244 1245 - sprint("mqtt connected") 1397 + sprint("mqtt connected") 1246 1246 1247 - end 1399 + end 1248 1248 1249 - g_mq:subscribe( TEST, 0)1401 + g_mq:subscribe(Subscribe_RE_TOPIC1, 0) 1250 1250 1251 1251 end 1252 1252 1253 - ~-~- Receive MQTT message callback function1405 +-- Receive MQTT message callback function 1254 1254 1255 1255 function mymqtt_msg_callback(topic, msg) 1256 1256 1257 - print("topic:",topic) 1409 + print("topic:",topic) 1258 1258 1259 - print("revdata:",msg) 1411 + print("revdata:",msg) 1260 1260 1261 - local revData = json.decode(msg) 1413 + local revData = json.decode(msg) 1262 1262 1263 - print (revData) 1415 + print (revData) 1264 1264 1265 - if topic == Subscribe_RE_TOPIC1 then ~-~-Process topic information subscribed from the cloud1417 + if topic == Subscribe_RE_TOPIC1 then --Process topic information subscribed from the cloud 1266 1266 1267 -if string.match(topic,Subscribe_RE_TOPIC1) then 1419 + if string.match(topic,Subscribe_RE_TOPIC1) then 1268 1268 1269 - ~-~-if revData ~~= nil then1421 + --if revData ~= nil then 1270 1270 1271 - for k,v in pairs (revData) do 1423 + for k,v in pairs (revData) do 1272 1272 1273 - print("printing revdata after kv here") 1425 + print("printing revdata after kv here") 1274 1274 1275 - print (k,v) 1427 + print (k,v) 1276 1276 1277 - end 1429 + end 1278 1278 1279 - print ("current state is",fanstate) 1431 + print ("current state is",fanstate) 1280 1280 1281 - ~-~-end1433 + --end 1282 1282 1283 -end 1435 + end 1284 1284 1285 -end 1437 + end 1286 1286 1287 1287 end 1288 1288 1289 1289 1290 -~-~-Get real-time data 1291 1291 1443 +--Get real-time data 1444 + 1292 1292 function getData() 1293 1293 1294 - local jdata = {} 1447 + local jdata = {} 1295 1295 1296 - local addr = bns_get_alldata() 1449 + local addr = bns_get_alldata() 1297 1297 1298 - print(json.encode(addr)) 1451 + print(json.encode(addr)) 1299 1299 1300 - for i,v in pairs(addr) do 1453 + for i,v in pairs(addr) do 1301 1301 1302 - if v[2] == 1 then 1455 + if v[2] == 1 then 1303 1303 1304 - jdata[v[3]] = v[4] 1457 + jdata[v[3]] = v[4] 1305 1305 1306 - end 1459 + end 1307 1307 1308 - end 1461 + end 1309 1309 1310 - return jdata 1463 + return jdata 1311 1311 1312 1312 end 1313 1313 1314 - ~-~-send data1467 +--send data 1315 1315 1316 1316 function send_Data() 1317 1317 1318 - local pub_data = 1471 + local pub_data = 1472 + { 1473 + 123 1474 + } 1319 1319 1320 - { 1321 - 1322 -123 1323 - 1324 -} 1325 - 1326 1326 sprint(json.encode(pub_data)) 1327 1327 1328 1328 print("..........",pub_RE_TOPIC) 1329 1329 1330 - return g_mq:publish(pub_RE_TOPIC, json.encode(pub_data), 0, 0) 1480 + return g_mq:publish(pub_RE_TOPIC, json.encode(pub_data), 0, 0) 1331 1331 1332 1332 end 1483 +{{/code}} 1333 1333 1334 1334 Get message in AWS 1335 1335
- InputHTTPparameter.png
-
- Author
-
... ... @@ -1,0 +1,1 @@ 1 +XWiki.Hunter - Size
-
... ... @@ -1,0 +1,1 @@ 1 +17.9 KB - Content