Changes for page 2 Script

Last modified by Devin Chen on 2025/06/06 14:03

From version 33.1
edited by Hunter
on 2022/11/22 15:38
Change comment: There is no comment for this version
To version 44.1
edited by Hunter
on 2023/03/30 16:34
Change comment: There is no comment for this version

Summary

Details

Page properties
Content
... ... @@ -447,6 +447,20 @@
447 447  end
448 448  {{/code}}
449 449  
450 +== **1.9 High-Low Byte Switch** ==
451 +
452 +The following example is converting the floating number from order 1234 to order 3412. 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"]].
453 +
454 +{{code language="lua"}}
455 +function highLowByteSwitch(floatNumber)
456 + addr_setfloat("@W_0#HDW23036",floatNumber,0,2)
457 + local newFloat = addr_getfloat("@W_0#HDW23036")
458 + local formattedFloat = string.format("%.2f",newFloat)
459 + print("The formatted float value is the : "..formattedFloat)
460 + return formattedFloat
461 +end
462 +{{/code}}
463 +
450 450  = **2 V-Box connect with third part server** =
451 451  
452 452  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.
... ... @@ -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" %)
482 +{{info}}
469 469  **✎Note: **Before program the script of MQTT, please make sure the server(MQTT broker) can be connected through MQTT Client tool.
484 +{{/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  
491 +The following example is trying to publish to the topic "testtopic/test/no1/7890", and subscribe the topic "testtopic/test/no1/123456".
492 +
493 +And the JSON message is like follows:
494 +
495 +{{code language="JSON"}}
496 +{
497 + "timestamp": 1631152760,
498 + "messageId": 1,
499 + "event": "test_data",
500 + "mfrs": "HMI/box",
501 + "data":
502 + {
503 + "id" : 1436217747670454274,
504 + "waterlevel" : 48,
505 + "temperture" : 23
506 + }
507 +}
508 +{{/code}}
509 +
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))
551 + if g_mq then
552 + mqtt.close() --Close mqtt object
553 + 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
557 + 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  
577 +--Offline callback function
578 +function mqtt_msg_offline(cause)
579 + print("mqtt offline, cause:", cause)
580 +end
581 +
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 20 sec not connect, reconnect once
578 - if os.time() - LAST_TIME > 20 then
620 + --if exceed 5 sec not connect, reconnect once
621 + if os.time() - LAST_TIME > 5 then
579 579   LAST_TIME = os.time()
623 + --reinitial the mqtt object
624 + mqtt_init()
580 580   --connect to mqtt or reconnect
581 581   mqtt_connect()
582 582   end
... ... @@ -1234,6 +1234,10 @@
1234 1234  
1235 1235  [[image:image-20220709165402-20.png]]
1236 1236  
1282 +{{info}}
1283 +**✎Note:** Before using the following demo script, please make sure the V-Box firmware is newer than 22110701
1284 +{{/info}}
1285 +
1237 1237  {{code language="lua"}}
1238 1238  sprint = print
1239 1239  
... ... @@ -1295,7 +1295,7 @@
1295 1295  
1296 1296   sprint(string.format("mqtt init mqtt_url:%s mqtt_clientid:%s", MQTT_URL, MQTT_CLIENTID))
1297 1297  
1298 - g_mq, err = mqtt.create(MQTT_URL, MQTT_CLIENTID) -- Create the object and declare it as a global variable
1347 + 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
1299 1299  
1300 1300   if g_mq then
1301 1301  
... ... @@ -1331,7 +1331,7 @@
1331 1331  
1332 1332   end
1333 1333  
1334 - g_mq:subscribe(TEST, 0)
1383 + g_mq:subscribe(Subscribe_RE_TOPIC1, 0)
1335 1335  
1336 1336  end
1337 1337