Mqtt disconnects after some time
#1
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 ?
Reply
#2
Add this to the end of your script or switch to a scheduled script that runs every minute.
Code:
collectgarbage('collect')
Reply
#3
Thanks, works great now.
Reply


Forum Jump: