Added MQTT module
This commit is contained in:
parent
a2021fcb48
commit
8858fa19bd
4
main.lua
4
main.lua
@ -42,6 +42,7 @@ function syncTimeFromInternet()
|
|||||||
end
|
end
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
briPercent = 50
|
briPercent = 50
|
||||||
function displayTime()
|
function displayTime()
|
||||||
local sec, usec = rtctime.get()
|
local sec, usec = rtctime.get()
|
||||||
@ -146,7 +147,7 @@ function normalOperation()
|
|||||||
print('IP: ',wifi.sta.getip())
|
print('IP: ',wifi.sta.getip())
|
||||||
-- Here the WLAN is found, and something is done
|
-- Here the WLAN is found, and something is done
|
||||||
print("Solving dependencies")
|
print("Solving dependencies")
|
||||||
local dependModules = { "timecore" , "wordclock", "telnet" }
|
local dependModules = { "timecore" , "wordclock", "telnet", "mqtt" }
|
||||||
for _,mod in pairs(dependModules) do
|
for _,mod in pairs(dependModules) do
|
||||||
print("Loading " .. mod)
|
print("Loading " .. mod)
|
||||||
mydofile(mod)
|
mydofile(mod)
|
||||||
@ -154,7 +155,6 @@ function normalOperation()
|
|||||||
|
|
||||||
tmr.alarm(2, 500, 0 ,function()
|
tmr.alarm(2, 500, 0 ,function()
|
||||||
syncTimeFromInternet()
|
syncTimeFromInternet()
|
||||||
displayTime()
|
|
||||||
end)
|
end)
|
||||||
tmr.alarm(3, 2000, 0 ,function()
|
tmr.alarm(3, 2000, 0 ,function()
|
||||||
if (startTelnetServer ~= nil) then
|
if (startTelnetServer ~= nil) then
|
||||||
|
44
mqtt.lua
Normal file
44
mqtt.lua
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
-- MQTT extension
|
||||||
|
function startMqtt()
|
||||||
|
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
|
||||||
|
mqttBrightness=100
|
||||||
|
m:publish(mqttPrefix .. "/clock", "ON", 0, 0)
|
||||||
|
elseif (data == "OFF") then
|
||||||
|
mqttBrightness=0
|
||||||
|
m:publish(mqttPrefix .. "/clock", "OFF", 0, 0)
|
||||||
|
else
|
||||||
|
if (tonumber(data) >= 0 and tonumber(data) <= 100) then
|
||||||
|
mqttBrightness=tonumber(data)
|
||||||
|
m:publish(mqttPrefix .. "/clock", tostring(data), 0, 0)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
|
m:connect(mqttServer, 1883, 0, function(client)
|
||||||
|
print("[MQTT] connected")
|
||||||
|
mqttConnected = true
|
||||||
|
-- subscribe topic with qos = 0
|
||||||
|
client:subscribe(mqttPrefix .. "/command", 0)
|
||||||
|
-- publish a message with data = hello, QoS = 0, retain = 0
|
||||||
|
client:publish(mqttPrefix .. "/ip", tostring(wifi.sta.getip()), 0, 0)
|
||||||
|
end,
|
||||||
|
function(client, reason)
|
||||||
|
print("failed reason: " .. reason)
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
if (mqttServer ~= nil and mqttPrefix ~= nil) then
|
||||||
|
startMqtt()
|
||||||
|
print "Started MQTT client"
|
||||||
|
|
||||||
|
tmr.alarm(5, 60000, 1 ,function()
|
||||||
|
m:publish(mqttPrefix .. "/brightness", tostring(briPercent), 0, 0)
|
||||||
|
end)
|
||||||
|
end
|
Loading…
Reference in New Issue
Block a user