Reconnect to MQTT if necessary (checked every 30 seconds)
This commit is contained in:
parent
379233327c
commit
1863ab15b9
5
main.lua
5
main.lua
@ -139,6 +139,11 @@ function normalOperation()
|
|||||||
if (startMqttClient ~= nil) then
|
if (startMqttClient ~= nil) then
|
||||||
if (not connectedMqtt()) then
|
if (not connectedMqtt()) then
|
||||||
rgbBuffer:set(103, 0, 64,0)
|
rgbBuffer:set(103, 0, 64,0)
|
||||||
|
-- check every thirty seconds, if reconnecting is necessary
|
||||||
|
if (((tmr.now() / 1000000) % 100) == 30) then
|
||||||
|
print("MQTT reconnecting... ")
|
||||||
|
reConnectMqtt()
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
ws2812.write(rgbBuffer)
|
ws2812.write(rgbBuffer)
|
||||||
|
50
mqtt.lua
50
mqtt.lua
@ -78,6 +78,33 @@ function readTemp()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Connect or reconnect to mqtt server
|
||||||
|
function reConnectMqtt()
|
||||||
|
if (not mqttConnected) then
|
||||||
|
m:connect(mqttServer, 1883, false, function(c)
|
||||||
|
print("MQTT is connected")
|
||||||
|
mqttConnected = true
|
||||||
|
-- subscribe topic with qos = 0
|
||||||
|
m:subscribe(mqttPrefix .. "/cmd/#", 0)
|
||||||
|
local tmr1 = tmr.create()
|
||||||
|
tmr1:register(1000, tmr.ALARM_SINGLE, function (t)
|
||||||
|
-- publish a message with data = hello, QoS = 0, retain = 0
|
||||||
|
m:publish(mqttPrefix .. "/ip", tostring(wifi.sta.getip()), 0, 0)
|
||||||
|
local red = string.byte(colorBg,2)
|
||||||
|
local green = string.byte(colorBg,1)
|
||||||
|
local blue = string.byte(colorBg,3)
|
||||||
|
m:publish(mqttPrefix .. "/background", tostring(red) .. "," .. tostring(green) .. "," .. tostring(blue), 0, 0)
|
||||||
|
tmr1:unregister()
|
||||||
|
end)
|
||||||
|
tmr1:start()
|
||||||
|
end,
|
||||||
|
function(client, reason)
|
||||||
|
print("failed reason: " .. reason)
|
||||||
|
mqttConnected = false
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
-- MQTT extension
|
-- MQTT extension
|
||||||
function registerMqtt()
|
function registerMqtt()
|
||||||
m = mqtt.Client("wordclock", 120)
|
m = mqtt.Client("wordclock", 120)
|
||||||
@ -141,27 +168,7 @@ function registerMqtt()
|
|||||||
mqttConnected = false
|
mqttConnected = false
|
||||||
end
|
end
|
||||||
)
|
)
|
||||||
m:connect(mqttServer, 1883, false, function(c)
|
reConnectMqtt()
|
||||||
print("MQTT is connected")
|
|
||||||
mqttConnected = true
|
|
||||||
-- subscribe topic with qos = 0
|
|
||||||
m:subscribe(mqttPrefix .. "/cmd/#", 0)
|
|
||||||
local tmr1 = tmr.create()
|
|
||||||
tmr1:register(1000, tmr.ALARM_SINGLE, function (t)
|
|
||||||
-- publish a message with data = hello, QoS = 0, retain = 0
|
|
||||||
m:publish(mqttPrefix .. "/ip", tostring(wifi.sta.getip()), 0, 0)
|
|
||||||
local red = string.byte(colorBg,2)
|
|
||||||
local green = string.byte(colorBg,1)
|
|
||||||
local blue = string.byte(colorBg,3)
|
|
||||||
m:publish(mqttPrefix .. "/background", tostring(red) .. "," .. tostring(green) .. "," .. tostring(blue), 0, 0)
|
|
||||||
tmr1:unregister()
|
|
||||||
end)
|
|
||||||
tmr1:start()
|
|
||||||
end,
|
|
||||||
function(client, reason)
|
|
||||||
print("failed reason: " .. reason)
|
|
||||||
mqttConnected = false
|
|
||||||
end)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function connectedMqtt()
|
function connectedMqtt()
|
||||||
@ -201,3 +208,4 @@ function startMqttClient()
|
|||||||
mqtttimer:start()
|
mqtttimer:start()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user