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
-
... ... @@ -447,6 +447,20 @@ 447 447 end 448 448 {{/code}} 449 449 450 +== **1.9 High-Low Byte Switch** == 451 + 452 + 453 +{{code language="lua"}} 454 +function highLowByteSwitch(floatNumber) 455 + addr_setfloat("@W_0#HDW23036",floatNumber,0,2) 456 + local newFloat = addr_getfloat("@W_0#HDW23036") 457 + local formattedFloat = string.format("%.2f",newFloat) 458 + print("The formatted float value is the : "..formattedFloat) 459 + return formattedFloat 460 +end 461 +{{/code}} 462 + 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. ... ... @@ -474,6 +474,25 @@ 474 474 475 475 == **2.1 V-Box connect with test server(General Example)** == 476 476 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 + 477 477 {{code language="lua"}} 478 478 --MQTT configuration table 479 479 local MQTT_CFG={} ... ... @@ -515,9 +515,13 @@ 515 515 --initialize mqtt 516 516 function mqtt_init() 517 517 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 518 518 g_mq, err = mqtt.create(MQTT_URL, MQTT_CLIENT_ID) -- create mqtt object,and declare it as a global variable 519 519 if g_mq then 520 520 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 521 521 print("mqtt init success") 522 522 else 523 523 print("mqtt init failed:", err) ... ... @@ -537,6 +537,11 @@ 537 537 g_mq:subscribe(SUBSCRIBE_TOPIC, 0) 538 538 end 539 539 577 +--Offline callback function 578 +function mqtt_msg_offline(cause) 579 + print("mqtt offline, cause:", cause) 580 +end 581 + 540 540 -- Received message callback function 541 541 function mqtt_msg_callback(topic, msg) 542 542 print("topic:", topic) ... ... @@ -575,9 +575,11 @@ 575 575 if g_mq:isconnected() then 576 576 send_data() 577 577 else 578 - --if exceed 20sec not connect, reconnect once579 - if os.time() - LAST_TIME > 20then620 + --if exceed 5 sec not connect, reconnect once 621 + if os.time() - LAST_TIME > 5 then 580 580 LAST_TIME = os.time() 623 + --reinitial the mqtt object 624 + mqtt_init() 581 581 --connect to mqtt or reconnect 582 582 mqtt_connect() 583 583 end