Changes for page 2 Script

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

From version 36.1
edited by Hunter
on 2022/12/26 16:29
Change comment: There is no comment for this version
To version 41.1
edited by Hunter
on 2023/03/17 18:05
Change comment: There is no comment for this version

Summary

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 20 sec not connect, reconnect once
578 - if os.time() - LAST_TIME > 20 then
606 + --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
... ... @@ -1234,7 +1234,9 @@
1234 1234  
1235 1235  [[image:image-20220709165402-20.png]]
1236 1236  
1237 -**Before using the following demo script, please make sure the V-Box firmware is newer than 22110701:**
1268 +{{info}}
1269 +**✎Note:** Before using the following demo script, please make sure the V-Box firmware is newer than 22110701
1270 +{{/info}}
1238 1238  
1239 1239  {{code language="lua"}}
1240 1240  sprint = print