Logic Machine Forum
Mqtt disconnects after some time - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: Mqtt disconnects after some time (/showthread.php?tid=1387)



Mqtt disconnects after some time - JohnTH - 09.05.2018

Hi,

I use this code as a resident script that runs every 30 second. It works great, but it is loosing connection after 4-4.5 hour. 
I then have to disable --> enable script again to get it started. I have tried both line 12 and 15 commented out with no difference.
This script is running fine on 2 other LM`s so I dont`t think the broker is the problem.

Code:
mqtt = require("mosquitto")
client = mqtt.new()

--------------------------------------------------

value = math.random(0, 1000)

--------------------------------------------------


client.ON_CONNECT = function()
               client:publish("test/1/LMdata", '{"site": "office", "heartbeat":"'..value..'"}')
            local qos = 1
            local retain = true
                --local mid = client:publish("test/1/LMdata", '{"site": "office", "heartbeat":"'..value..'"}', qos, retain)
end

client.ON_PUBLISH = function()
    client:disconnect()
end


broker = '10.10.10.18'
client:login_set("user","passwd")
client:connect(broker)
client:loop_forever()

Have anyone had the same problem, or any idea of what the problem can be ?


RE: Mqtt disconnects after some time - admin - 09.05.2018

Add this to the end of your script or switch to a scheduled script that runs every minute.
Code:
collectgarbage('collect')



RE: Mqtt disconnects after some time - JohnTH - 22.05.2018

Thanks, works great now.