Changes for page 2 Script

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

From version 41.1
edited by Hunter
on 2023/03/17 18:05
Change comment: There is no comment for this version
To version 38.1
edited by Hunter
on 2023/03/17 14:43
Change comment: There is no comment for this version

Summary

Details

Page properties
Content
... ... @@ -474,25 +474,6 @@
474 474  
475 475  == **2.1 V-Box connect with test server(General Example)** ==
476 476  
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 -
496 496  {{code language="lua"}}
497 497  --MQTT configuration table
498 498  local MQTT_CFG={}
... ... @@ -534,13 +534,9 @@
534 534  --initialize mqtt
535 535  function mqtt_init()
536 536   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
540 540   g_mq, err = mqtt.create(MQTT_URL, MQTT_CLIENT_ID) -- create mqtt object,and declare it as a global variable
541 541   if g_mq then
542 542   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
544 544   print("mqtt init success")
545 545   else
546 546   print("mqtt init failed:", err)
... ... @@ -560,11 +560,6 @@
560 560   g_mq:subscribe(SUBSCRIBE_TOPIC, 0)
561 561  end
562 562  
563 ---Offline callback function
564 -function mqtt_msg_offline(cause)
565 - print("mqtt offline, cause:", cause)
566 -end
567 -
568 568  -- Received message callback function
569 569  function mqtt_msg_callback(topic, msg)
570 570   print("topic:", topic)
... ... @@ -603,11 +603,9 @@
603 603   if g_mq:isconnected() then
604 604   send_data()
605 605   else
606 - --if exceed 5 sec not connect, reconnect once
607 - if os.time() - LAST_TIME > 5 then
578 + --if exceed 20 sec not connect, reconnect once
579 + if os.time() - LAST_TIME > 20 then
608 608   LAST_TIME = os.time()
609 - --reinitial the mqtt object
610 - mqtt_init()
611 611   --connect to mqtt or reconnect
612 612   mqtt_connect()
613 613   end