Added cmd MQTT sublevel and refactored function

This commit is contained in:
Ollo 2021-01-15 18:08:56 +01:00
parent 34e47c9a39
commit 9534368b01

View File

@ -2,14 +2,7 @@
m=nil m=nil
mqttConnected = false mqttConnected = false
-- MQTT extension function handleSingleCommand(client, topic, data)
function registerMqtt()
m = mqtt.Client("wordclock", 120)
-- on publish message receive event
m:on("message", function(client, topic, data)
print(topic .. ":" )
if data ~= nil then
print(data)
if (data == "ON") then if (data == "ON") then
briPercent=100 briPercent=100
m:publish(mqttPrefix .. "/clock", "ON", 0, 0) m:publish(mqttPrefix .. "/clock", "ON", 0, 0)
@ -33,6 +26,18 @@ function registerMqtt()
print "Unknown MQTT command" print "Unknown MQTT command"
end end
end end
end
-- MQTT extension
function registerMqtt()
m = mqtt.Client("wordclock", 120)
-- on publish message receive event
m:on("message", function(client, topic, data)
print(topic .. ":" )
if data ~= nil then
print(data)
handleSingleCommand(client, topic, data)
end end
end) end)
@ -44,6 +49,7 @@ function registerMqtt()
tmr.alarm(3, 500, 0, function() tmr.alarm(3, 500, 0, function()
-- publish a message with data = hello, QoS = 0, retain = 0 -- publish a message with data = hello, QoS = 0, retain = 0
client:publish(mqttPrefix .. "/ip", tostring(wifi.sta.getip()), 0, 0) client:publish(mqttPrefix .. "/ip", tostring(wifi.sta.getip()), 0, 0)
client:subscribe(mqttPrefix .. "/cmd/#", 0)
end) end)
end, end,
function(client, reason) function(client, reason)