Wordclock/mqtt.lua

247 lines
8.0 KiB
Lua
Raw Permalink Normal View History

2021-12-04 13:04:12 +01:00
-- Module Variables
-- Mqtt variable
local mMqttClient=nil
local mMqttConnected = false
function handleSingleCommand(client, topic, data)
if (data == "ON") then
2021-02-13 13:39:40 +01:00
briPer=100
2021-12-04 13:04:12 +01:00
mMqttClient:publish(mqttPrefix .. "/clock", "ON", 0, 0)
elseif (data == "OFF") then
2021-02-13 13:39:40 +01:00
briPer=0
2021-12-04 13:04:12 +01:00
mMqttClient:publish(mqttPrefix .. "/clock", "OFF", 0, 0)
2021-01-15 18:14:39 +01:00
elseif ((data:sub(1,1) == "#" and data:len() == 7) or (string.match(data, "%d+,%d+,%d+"))) then
2021-01-15 19:28:20 +01:00
local red=0
local green=0
local blue=0
2021-01-15 18:33:49 +01:00
if (data:sub(1,1) == "#") then
red = tonumber(data:sub(2,3), 16)
green = tonumber(data:sub(4,5), 16)
blue = tonumber(data:sub(6,7), 16)
else
red, green, blue = string.match(data, "(%d+),(%d+),(%d+)")
end
2021-02-13 13:39:40 +01:00
colorBg=string.char(green * briPer / 100, red * briPer / 100, blue * briPer / 100)
print("Updated BG: " .. tostring(red) .. "," .. tostring(green) .. "," .. tostring(blue) )
2021-12-04 13:04:12 +01:00
mMqttClient:publish(mqttPrefix .. "/background", tostring(red) .. "," .. tostring(green) .. "," .. tostring(blue), 0, 0)
else
if (tonumber(data) >= 0 and tonumber(data) <= 100) then
2021-02-13 13:39:40 +01:00
briPer=tonumber(data)
2021-12-04 13:04:12 +01:00
mMqttClient:publish(mqttPrefix .. "/clock", tostring(data), 0, 0)
else
print "Unknown MQTT command"
end
end
end
-- Parse MQTT data and extract color
-- @param data MQTT information
-- @param row string of the row e.g. "row1" used to publish current state
2021-04-03 13:15:37 +02:00
-- @param per percent the color should be dimmed
function parseBgColor(data, row, per)
2021-01-23 09:44:15 +01:00
local red=nil
local green=nil
local blue=nil
if (data:sub(1,1) == "#") then
red = tonumber(data:sub(2,3), 16)
green = tonumber(data:sub(4,5), 16)
blue = tonumber(data:sub(6,7), 16)
else
red, green, blue = string.match(data, "(%d+),(%d+),(%d+)")
end
if ((red ~= nil) and (green ~= nil) and (blue ~= nil) ) then
2021-12-04 13:04:12 +01:00
mMqttClient:publish(mqttPrefix .. "/"..row, tostring(red) .. "," .. tostring(green) .. "," .. tostring(blue), 0, 0)
2021-04-03 13:15:37 +02:00
if (per ~= nil) then
return string.char(green * per / 100, red * per / 100, blue * per / 100)
else
return string.char(green , red , blue )
end
2021-01-23 09:44:15 +01:00
else
return nil
end
end
2021-11-28 12:19:19 +01:00
function readTemp(ds18b20)
if (ds18b20 ~= nil) then
2021-12-04 13:04:12 +01:00
local addrs=ds18b20.addrs()
-- Total DS18B20 numbers
2021-12-04 13:04:12 +01:00
local sensors=table.getn(addrs)
local temp1=0
if (sensors >= 1) then
2021-11-28 12:19:19 +01:00
temp1=ds18b20.read(addrs[0])
else
print("No sensor DS18B20 found")
end
return temp1
else
print("No DS18B20 lib")
return nil
end
end
-- Connect or reconnect to mqtt server
function reConnectMqtt()
2021-12-04 13:04:12 +01:00
if (not mMqttConnected) then
mMqttClient:connect(mqttServer, 1883, false, function(c)
print("MQTT is connected")
2021-12-04 13:04:12 +01:00
mMqttConnected = true
-- subscribe topic with qos = 0
2021-12-04 13:04:12 +01:00
mMqttClient:subscribe(mqttPrefix .. "/cmd/#", 0)
local tmr1 = tmr.create()
2021-11-28 12:19:19 +01:00
tmr1:register(1000, tmr.ALARM_SINGLE, function (dummyTemp)
-- publish a message with data = hello, QoS = 0, retain = 0
2021-12-04 13:04:12 +01:00
mMqttClient: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)
2021-12-04 13:04:12 +01:00
mMqttClient:publish(mqttPrefix .. "/background", tostring(red) .. "," .. tostring(green) .. "," .. tostring(blue), 0, 0)
tmr1:unregister()
2021-12-04 13:04:12 +01:00
tmr1=nil
end)
tmr1:start()
end,
function(client, reason)
print("failed reason: " .. reason)
2021-12-04 13:04:12 +01:00
mMqttConnected = false
end)
end
end
2020-12-07 21:26:02 +01:00
-- MQTT extension
2021-01-14 21:27:13 +01:00
function registerMqtt()
2021-12-04 13:04:12 +01:00
mMqttClient = mqtt.Client("wordclock", 120)
2020-12-07 21:26:02 +01:00
-- on publish message receive event
2021-12-04 13:04:12 +01:00
mMqttClient:on("message", function(client, topic, data)
collectgarbage()
2020-12-07 21:26:02 +01:00
if data ~= nil then
2021-12-04 13:04:12 +01:00
local heapusage = node.heap()
2022-01-06 16:01:27 +01:00
if (heapusage < 11000) then
2021-12-04 13:04:12 +01:00
print("Skip " .. topic .. ":" .. data .. "; heap:" .. heapusage)
heapusage=nil
return
end
heapusage=nil
print("MQTT " .. topic .. ":" .. data)
2021-01-15 19:03:20 +01:00
if (topic == (mqttPrefix .. "/cmd/single")) then
2021-01-15 18:33:49 +01:00
handleSingleCommand(client, topic, data)
else
2021-01-15 18:33:49 +01:00
-- Handle here the /cmd/# sublevel
if (string.match(topic, "telnet$")) then
client:publish(mqttPrefix .. "/telnet", tostring(wifi.sta.getip()), 0, 0)
2021-01-23 19:17:30 +01:00
ws2812.write(string.char(0,0,0):rep(114))
print("Stop Mqtt and Temp")
2021-01-15 18:33:49 +01:00
m=nil
2021-01-23 19:17:30 +01:00
t=nil
2021-12-04 13:04:12 +01:00
mMqttConnected = false
2021-02-13 14:17:58 +01:00
if (mlt ~= nil) then
mlt:unregister()
else
print("main loop unstoppable")
2021-01-31 20:22:32 +01:00
end
2021-01-15 18:33:49 +01:00
collectgarbage()
mydofile("telnet")
if (startTelnetServer ~= nil) then
startTelnetServer()
2021-03-21 20:52:27 +01:00
else
print("Cannost start Telnet Server!")
end
2021-04-03 13:15:37 +02:00
elseif (string.match(topic, "color$")) then
color = parseBgColor(data, "color")
2021-04-03 13:28:10 +02:00
print("Updated color" )
2021-04-03 13:15:37 +02:00
elseif (string.match(topic, "color1$")) then
color1 = parseBgColor(data, "color1")
2021-04-03 13:28:10 +02:00
print("Updated color1" )
2021-04-03 13:15:37 +02:00
elseif (string.match(topic, "color2$")) then
2021-04-03 14:57:39 +02:00
color2 = parseBgColor(data, "color2")
2021-04-03 13:28:10 +02:00
print("Updated color2" )
2021-04-03 13:15:37 +02:00
elseif (string.match(topic, "color3$")) then
2021-04-03 14:57:39 +02:00
color3 = parseBgColor(data, "color3")
2021-04-03 13:28:10 +02:00
print("Updated color3" )
2021-04-03 13:15:37 +02:00
elseif (string.match(topic, "color4$")) then
2021-04-03 14:57:39 +02:00
color4 = parseBgColor(data, "color4")
2021-04-03 13:28:10 +02:00
print("Updated color4" )
--FIXME load here the mqtt2 file
2021-04-03 13:15:37 +02:00
else
2021-01-23 19:17:30 +01:00
for i=1,10,1 do
if (string.match(topic, "row".. tostring(i) .."$")) then
2021-04-03 13:15:37 +02:00
rowbgColor[i] = parseBgColor(data, "row" .. tostring(i), briPer)
2021-01-23 19:17:30 +01:00
return
end
end
end
2021-01-15 18:33:49 +01:00
end
2020-12-07 21:26:02 +01:00
end
end)
2021-12-04 13:04:12 +01:00
-- do something
mMqttClient:on("offline", function(client)
print("MQTT Disconnected")
2021-12-04 13:04:12 +01:00
mMqttConnected = false
collectgarbage()
end)
reConnectMqtt()
2020-12-07 21:26:02 +01:00
end
2021-03-21 18:00:07 +01:00
function connectedMqtt()
2021-12-04 13:04:12 +01:00
return mMqttConnected
2021-03-21 18:00:07 +01:00
end
2021-01-14 21:51:29 +01:00
function startMqttClient()
2020-12-08 21:51:14 +01:00
if (mqttServer ~= nil and mqttPrefix ~= nil) then
2021-01-14 21:27:13 +01:00
registerMqtt()
2020-12-08 21:51:14 +01:00
print "Started MQTT client"
2021-12-04 13:04:12 +01:00
local lSetupTimer = tmr.create()
lSetupTimer:register(123, tmr.ALARM_SINGLE, function (kTemp)
if (file.open("ds18b20_diet.lc")) then
2021-11-28 12:19:19 +01:00
t=true
2021-12-02 22:53:38 +01:00
print "Setup temperatur"
end
end)
2021-12-04 13:04:12 +01:00
lSetupTimer:start()
local loldBrightness=0
local lMqttTimer = tmr.create()
2022-01-20 21:12:54 +01:00
local tempCounter=0
2021-12-04 13:04:12 +01:00
-- Check every 12 seconds
2022-01-20 21:58:10 +01:00
lMqttTimer:register(6321, tmr.ALARM_AUTO, function (kTemp)
2021-12-04 13:04:12 +01:00
if (mMqttConnected) then
-- Cleanup the initialization stuff
lSetupTimer=nil
-- Do the action
local heapusage = node.heap()
local temperatur = nil
2021-12-04 13:04:12 +01:00
if (loldBrightness ~= briPer) then
mMqttClient:publish(mqttPrefix .. "/brightness", tostring(briPer), 0, 0)
loldBrightness = briPer
else
2022-01-20 21:12:54 +01:00
if ((t ~= nil) and (heapusage > 11900) and (tempCounter > 10)) then
2021-11-28 12:19:19 +01:00
local ds18b20=require("ds18b20_diet")
ds18b20.setup(2) -- GPIO4
2021-12-02 22:53:38 +01:00
readTemp(ds18b20) -- read once, to setup chip
2021-11-28 12:19:19 +01:00
temperatur=readTemp(ds18b20)
2021-12-02 22:53:38 +01:00
if (temperatur == 8500) then
2021-11-28 12:19:19 +01:00
temperatur=nil
end
ds18b20=nil
ds18b20_diet=nil
package.loaded["ds18b20_diet"]=nil
2022-01-20 21:12:54 +01:00
tempCounter = 0
else
tempCounter = tempCounter + 1
end
collectgarbage()
2022-01-20 22:06:03 +01:00
if (temperatur ~= nil) then
2021-12-04 13:04:12 +01:00
mMqttClient:publish(mqttPrefix .. "/temp", tostring(temperatur/100)..".".. tostring(temperatur%100), 0, 0)
2021-11-28 12:19:19 +01:00
else
2021-12-04 13:04:12 +01:00
mMqttClient:publish(mqttPrefix .. "/heap", tostring(heapusage), 0, 0)
2022-01-20 21:12:54 +01:00
collectgarbage()
2021-11-28 12:19:19 +01:00
end
end
2021-12-04 13:04:12 +01:00
heapusage=nil
temperatur=nil
2020-12-08 21:51:14 +01:00
end
end)
2021-12-04 13:04:12 +01:00
lMqttTimer:start()
2020-12-08 21:51:14 +01:00
end
2020-12-10 21:11:35 +01:00
end