Changes for page 2 Script

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

From version 38.1
edited by Hunter
on 2023/03/17 14:43
Change comment: There is no comment for this version
To version 40.1
edited by Hunter
on 2023/03/17 16:04
Change comment: There is no comment for this version

Summary

Details

Page properties
Content
... ... @@ -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))
518 + if g_mq then
519 + mqtt.close() --Close mqtt object
520 + 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
524 + 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  
544 +--Offline callback function
545 +function mqtt_msg_offline(cause)
546 + print("mqtt offline, cause:", cause)
547 +end
548 +
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 20 sec not connect, reconnect once
579 - if os.time() - LAST_TIME > 20 then
587 + --if exceed 5 sec not connect, reconnect once
588 + if os.time() - LAST_TIME > 5 then
580 580   LAST_TIME = os.time()
590 + --reinitial the mqtt object
591 + mqtt_init()
581 581   --connect to mqtt or reconnect
582 582   mqtt_connect()
583 583   end