Changes for page 2 Script
Last modified by Devin Chen on 2025/06/06 14:03
Summary
-
Page properties (1 modified, 0 added, 0 removed)
Details
- Page properties
-
- Content
-
... ... @@ -465,8 +465,9 @@ 465 465 466 466 (% class="mark" %)2.If your server requires SSL certificate to log in,please use OpenCloud.Because only OpenCloud platform can support to upload certificate 467 467 468 - (% class="wikigeneratedid" %)468 +{{info}} 469 469 **✎Note: **Before program the script of MQTT, please make sure the server(MQTT broker) can be connected through MQTT Client tool. 470 +{{/info}} 470 470 471 471 (% class="wikigeneratedid" %) 472 472 Tool link: **[[MQTT.fx>>http://mqttfx.jensd.de/index.php/download]]** ... ... @@ -473,6 +473,25 @@ 473 473 474 474 == **2.1 V-Box connect with test server(General Example)** == 475 475 477 +The following example is trying to publish to the topic "testtopic/test/no1/7890", and subscribe the topic "testtopic/test/no1/123456". 478 + 479 +And the JSON message is like follows: 480 + 481 +{{code language="JSON"}} 482 +{ 483 + "timestamp": 1631152760, 484 + "messageId": 1, 485 + "event": "test_data", 486 + "mfrs": "HMI/box", 487 + "data": 488 + { 489 + "id" : 1436217747670454274, 490 + "waterlevel" : 48, 491 + "temperture" : 23 492 + } 493 +} 494 +{{/code}} 495 + 476 476 {{code language="lua"}} 477 477 --MQTT configuration table 478 478 local MQTT_CFG={} ... ... @@ -514,9 +514,13 @@ 514 514 --initialize mqtt 515 515 function mqtt_init() 516 516 print(string.format("mqtt init mqtt_url:%s mqtt_clientid:%s", MQTT_URL, MQTT_CLIENT_ID)) 537 + if g_mq then 538 + mqtt.close() --Close mqtt object 539 + end 517 517 g_mq, err = mqtt.create(MQTT_URL, MQTT_CLIENT_ID) -- create mqtt object,and declare it as a global variable 518 518 if g_mq then 519 519 g_mq:on("message", mqtt_msg_callback) -- Register a callback for receiving messages 543 + g_mq:on("offline", mqtt_msg_offline) -- Register a callback for offline 520 520 print("mqtt init success") 521 521 else 522 522 print("mqtt init failed:", err) ... ... @@ -536,6 +536,11 @@ 536 536 g_mq:subscribe(SUBSCRIBE_TOPIC, 0) 537 537 end 538 538 563 +--Offline callback function 564 +function mqtt_msg_offline(cause) 565 + print("mqtt offline, cause:", cause) 566 +end 567 + 539 539 -- Received message callback function 540 540 function mqtt_msg_callback(topic, msg) 541 541 print("topic:", topic) ... ... @@ -574,9 +574,11 @@ 574 574 if g_mq:isconnected() then 575 575 send_data() 576 576 else 577 - --if exceed 20sec not connect, reconnect once578 - if os.time() - LAST_TIME > 20then606 + --if exceed 5 sec not connect, reconnect once 607 + if os.time() - LAST_TIME > 5 then 579 579 LAST_TIME = os.time() 609 + --reinitial the mqtt object 610 + mqtt_init() 580 580 --connect to mqtt or reconnect 581 581 mqtt_connect() 582 582 end ... ... @@ -1156,35 +1156,20 @@ 1156 1156 1157 1157 {{code language="java"}} 1158 1158 { 1159 - 1160 1160 "Version": "2012-10-17", 1161 - 1162 1162 "Statement": [ 1163 - 1164 1164 { 1165 - 1166 1166 "Effect": "Allow", 1167 - 1168 1168 "Action": [ 1169 - 1170 1170 "iot:Connect", 1171 - 1172 1172 "iot:Publish", 1173 - 1174 1174 "iot:Subscribe", 1175 - 1176 1176 "iot:Receive", 1177 - 1178 1178 "greengrass:Discover" 1179 - 1180 1180 ], 1181 - 1182 1182 "Resource": "*" 1183 - 1184 1184 } 1185 - 1186 1186 ] 1187 - 1188 1188 } 1189 1189 {{/code}} 1190 1190 ... ... @@ -1249,185 +1249,190 @@ 1249 1249 1250 1250 [[image:image-20220709165402-20.png]] 1251 1251 1268 +{{info}} 1269 +**✎Note:** Before using the following demo script, please make sure the V-Box firmware is newer than 22110701 1270 +{{/info}} 1271 + 1272 +{{code language="lua"}} 1252 1252 sprint = print 1253 1253 1254 - ~-~-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)1275 +--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) 1255 1255 1256 1256 local MQTT_URL, MQTT_CLIENTID, MQTT_CFG, MQTT_LWT, MQTT_CART = mqtt.setup_cfg() 1257 1257 1258 - ~-~-publish to topics1279 +--publish to topics 1259 1259 1260 1260 local pub_RE_TOPIC = string.format('TEST') 1261 1261 1262 - ~-~-Subscribe topics1283 +--Subscribe topics 1263 1263 1264 1264 local Subscribe_RE_TOPIC1 = string.format('TEST') 1265 1265 1266 - ~-~-variable1287 +--variable 1267 1267 1268 1268 local last_time = 0 1269 1269 1270 - ~-~-Timing main function1291 +--Timing main function 1271 1271 1272 1272 function aws.main() 1273 1273 1274 - sprint(os.date("%Y-%m-%d %H:%M %S", os.time()) .. " aws.main start") 1295 + sprint(os.date("%Y-%m-%d %H:%M %S", os.time()) .. " aws.main start") 1275 1275 1276 - if g_mq then 1297 + if g_mq then 1277 1277 1278 - if g_mq:isconnected() then 1299 + if g_mq:isconnected() then 1279 1279 1280 - send_Data() 1301 + send_Data() 1281 1281 1282 - else 1303 + else 1283 1283 1284 - if os.time() - last_time > 5 then 1305 + if os.time() - last_time > 5 then 1285 1285 1286 - last_time = os.time() 1307 + last_time = os.time() 1287 1287 1288 - mymqtt_connect() 1309 + mymqtt_connect() 1289 1289 1311 + end 1312 + 1290 1290 end 1291 1291 1292 - e nd1315 + else 1293 1293 1294 - else1317 + mymqtt_init() 1295 1295 1296 - mymqtt_init()1319 + end 1297 1297 1298 - end 1321 + sprint(os.date("%Y-%m-%d %H:%M %S", os.time()) .. " aws.main end") 1299 1299 1300 - sprint(os.date("%Y-%m-%d %H:%M %S", os.time()) .. " aws.main end") 1301 - 1302 1302 end 1303 1303 1304 1304 1305 -~-~- Initialize MQTT 1306 1306 1327 +-- Initialize MQTT 1328 + 1307 1307 function mymqtt_init() 1308 1308 1309 - sprint(string.format("mqtt init mqtt_url:%s mqtt_clientid:%s", MQTT_URL, MQTT_CLIENTID)) 1331 + sprint(string.format("mqtt init mqtt_url:%s mqtt_clientid:%s", MQTT_URL, MQTT_CLIENTID)) 1310 1310 1311 - g_mq, err = mqtt.create(MQTT_URL, MQTT_CLIENTID) ~-~- Create the object and declare it as a global variable1333 + 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 1312 1312 1313 - if g_mq then 1335 + if g_mq then 1314 1314 1315 - g_mq:on("message", mymqtt_msg_callback) ~-~- Register to receive message callbacks1337 + g_mq:on("message", mymqtt_msg_callback) -- Register to receive message callbacks 1316 1316 1317 - sprint("mqtt init success") 1339 + sprint("mqtt init success") 1318 1318 1319 - else 1341 + else 1320 1320 1321 - sprint("mqtt init failed:", err) 1343 + sprint("mqtt init failed:", err) 1322 1322 1323 - end 1345 + end 1324 1324 1325 1325 end 1326 1326 1327 - ~-~- Connect to MQTT server1349 +-- Connect to MQTT server 1328 1328 1329 1329 function mymqtt_connect() 1330 1330 1331 - sprint("mqtt connecting...") 1353 + sprint("mqtt connecting...") 1332 1332 1333 - local stat, err = g_mq:connect(MQTT_CFG,MQTT_LWT, MQTT_CART) 1355 + local stat, err = g_mq:connect(MQTT_CFG,MQTT_LWT, MQTT_CART) 1334 1334 1335 - if stat == nil then 1357 + if stat == nil then 1336 1336 1337 - sprint("mqtt connect failed:", err) 1359 + sprint("mqtt connect failed:", err) 1338 1338 1339 - return 1361 + return 1340 1340 1341 - else 1363 + else 1342 1342 1343 - sprint("mqtt connected") 1365 + sprint("mqtt connected") 1344 1344 1345 - end 1367 + end 1346 1346 1347 - g_mq:subscribe( TEST, 0)1369 + g_mq:subscribe(Subscribe_RE_TOPIC1, 0) 1348 1348 1349 1349 end 1350 1350 1351 - ~-~- Receive MQTT message callback function1373 +-- Receive MQTT message callback function 1352 1352 1353 1353 function mymqtt_msg_callback(topic, msg) 1354 1354 1355 - print("topic:",topic) 1377 + print("topic:",topic) 1356 1356 1357 - print("revdata:",msg) 1379 + print("revdata:",msg) 1358 1358 1359 - local revData = json.decode(msg) 1381 + local revData = json.decode(msg) 1360 1360 1361 - print (revData) 1383 + print (revData) 1362 1362 1363 - if topic == Subscribe_RE_TOPIC1 then ~-~-Process topic information subscribed from the cloud1385 + if topic == Subscribe_RE_TOPIC1 then --Process topic information subscribed from the cloud 1364 1364 1365 -if string.match(topic,Subscribe_RE_TOPIC1) then 1387 + if string.match(topic,Subscribe_RE_TOPIC1) then 1366 1366 1367 - ~-~-if revData ~~= nil then1389 + --if revData ~= nil then 1368 1368 1369 - for k,v in pairs (revData) do 1391 + for k,v in pairs (revData) do 1370 1370 1371 - print("printing revdata after kv here") 1393 + print("printing revdata after kv here") 1372 1372 1373 - print (k,v) 1395 + print (k,v) 1374 1374 1375 - end 1397 + end 1376 1376 1377 - print ("current state is",fanstate) 1399 + print ("current state is",fanstate) 1378 1378 1379 - ~-~-end1401 + --end 1380 1380 1381 -end 1403 + end 1382 1382 1383 -end 1405 + end 1384 1384 1385 1385 end 1386 1386 1387 1387 1388 -~-~-Get real-time data 1389 1389 1411 +--Get real-time data 1412 + 1390 1390 function getData() 1391 1391 1392 - local jdata = {} 1415 + local jdata = {} 1393 1393 1394 - local addr = bns_get_alldata() 1417 + local addr = bns_get_alldata() 1395 1395 1396 - print(json.encode(addr)) 1419 + print(json.encode(addr)) 1397 1397 1398 - for i,v in pairs(addr) do 1421 + for i,v in pairs(addr) do 1399 1399 1400 - if v[2] == 1 then 1423 + if v[2] == 1 then 1401 1401 1402 - jdata[v[3]] = v[4] 1425 + jdata[v[3]] = v[4] 1403 1403 1404 - end 1427 + end 1405 1405 1406 - end 1429 + end 1407 1407 1408 - return jdata 1431 + return jdata 1409 1409 1410 1410 end 1411 1411 1412 - ~-~-send data1435 +--send data 1413 1413 1414 1414 function send_Data() 1415 1415 1416 - local pub_data = 1439 + local pub_data = 1440 + { 1441 + 123 1442 + } 1417 1417 1418 - { 1419 - 1420 -123 1421 - 1422 -} 1423 - 1424 1424 sprint(json.encode(pub_data)) 1425 1425 1426 1426 print("..........",pub_RE_TOPIC) 1427 1427 1428 - return g_mq:publish(pub_RE_TOPIC, json.encode(pub_data), 0, 0) 1448 + return g_mq:publish(pub_RE_TOPIC, json.encode(pub_data), 0, 0) 1429 1429 1430 1430 end 1451 +{{/code}} 1431 1431 1432 1432 Get message in AWS 1433 1433