Wordclock/mqtt.lua

268 lines
8.6 KiB
Lua
Raw Normal View History

-- Global variable
2021-02-10 19:45:28 +01:00
local m=nil
local mqttConnected = false
-- Temp:
2021-02-10 19:45:28 +01:00
local t=nil
local dispTemp=10
function handleSingleCommand(client, topic, data)
if (data == "ON") then
2021-02-13 13:39:40 +01:00
briPer=100
m:publish(mqttPrefix .. "/clock", "ON", 0, 0)
elseif (data == "OFF") then
2021-02-13 13:39:40 +01:00
briPer=0
m: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-01-15 19:28:20 +01:00
m: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)
m: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
m: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
function readTemp()
if (t ~= nil) then
addrs=t.addrs()
-- Total DS18B20 numbers
sensors=table.getn(addrs)
local temp1=0
if (sensors >= 1) then
temp1=t.read(addrs[0])
end
return temp1
else
return nil
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
-- Logic to display Mqtt
function mqttDispTemp(dw, rgbBuffer, invertRows)
if (dispTemp ~= nil) then
-- Values: it, is, 5 minutes, 10 minutes, afer, before, three hour, quarter, dreiviertel, half, s
-- hours: one, one Long, two, three, four, five, six, seven, eight, nine, ten, eleven, twelve
-- Special ones: twenty, clock, minute 1 flag, minute 2 flag, minute 3 flag, minute 4 flag
local ret = { it=0, is=0, m5=0, m10=0, ha=0, hb=0, h3=0, hq=0, h3q=0, half=0, s=0,
h1=0, h1l=0, h2=0, h3=0, h4=0, h5=0, h6=0, h7=0, h8=0, h9=0, h10=0, h11=0, h12=0,
m20=0, cl=0, m1=0, m2=0, m3=0, m4=0 }
print("Mqtt Display of temperature: " .. tostring(dispTemp) )
if (dispTemp == 1) or (dispTemp == -1) then
ret.h1=1
elseif (dispTemp == 2) or (dispTemp == -2) then
ret.h2=1
elseif (dispTemp == 3) or (dispTemp == -3) then
ret.h3=1
elseif (dispTemp == 4) or (dispTemp == -4) then
ret.h4=1
elseif (dispTemp == 5) or (dispTemp == -5) then
ret.h5=1
elseif (dispTemp == 6) or (dispTemp == -6) then
ret.h6=1
elseif (dispTemp == 7) or (dispTemp == -7) then
ret.h7=1
elseif (dispTemp == 8) or (dispTemp == -8) then
ret.h8=1
elseif (dispTemp == 9) or (dispTemp == -9) then
ret.h9=1
elseif (dispTemp == 10) or (dispTemp == -10) then
ret.h10=1
elseif (dispTemp == 11) or (dispTemp == -11) then
ret.h11=1
elseif (dispTemp == 12) or (dispTemp == -12) then
ret.h12=1
else
-- over or under temperature
end
local col=string.char(128,0,0) -- red; positive degrees
if (dispTemp < 0) then
col=string.char(0,0,128) -- blue; negative degrees
end
return ret, col
else
return nil, nil
end
end
2020-12-07 21:26:02 +01:00
-- MQTT extension
2021-01-14 21:27:13 +01:00
function registerMqtt()
2020-12-07 21:26:02 +01:00
m = mqtt.Client("wordclock", 120)
-- on publish message receive event
m:on("message", function(client, topic, data)
print("MQTT " .. topic .. ":" )
2020-12-07 21:26:02 +01:00
if data ~= nil then
print(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)
elseif (topic == (mqttPrefix .. "/cmd/temp")) then
if ( data == "" ) then
dispTemp = nil
else
dispTemp = tonumber(data)
end
2021-01-15 18:33:49 +01:00
else
-- 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-01-15 18:33:49 +01:00
mqttConnected = 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" )
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
print("Updated row" .. tostring(i) )
return
end
end
end
2021-01-15 18:33:49 +01:00
end
2020-12-07 21:26:02 +01:00
end
end)
m:on("offline", function(client)
print("MQTT Disconnected")
mqttConnected = false
end
)
reConnectMqtt()
2020-12-07 21:26:02 +01:00
end
2021-03-21 18:00:07 +01:00
function connectedMqtt()
return mqttConnected
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-03-21 21:00:47 +01:00
if (file.open("ds18b20_diet.lc")) then
2021-03-21 20:52:27 +01:00
t=require("ds18b20_diet")
t.setup(2) -- GPIO4
readTemp() -- read once, to setup chip
print "Setup temperature"
end
2021-02-13 13:39:40 +01:00
local oldBrightness=0
oldTemp=0
2021-01-31 20:22:32 +01:00
local mqtttimer = tmr.create()
mqtttimer:register(5001, tmr.ALARM_AUTO, function (t)
if (mqttConnected) then
local temp = nil
if (t ~= nil) then
temp=readTemp()
end
2021-02-13 13:39:40 +01:00
if (oldBrightness ~= briPer) then
m:publish(mqttPrefix .. "/brightness", tostring(briPer), 0, 0)
elseif (temp ~= nil and temp ~= oldTemp) then
oldTemp = temp
m:publish(mqttPrefix .. "/temp", tostring(temp/100).."."..tostring(temp%100), 0, 0)
else
m:publish(mqttPrefix .. "/heap", tostring(node.heap()), 0, 0)
end
2021-02-13 13:39:40 +01:00
oldBrightness = briPer
2020-12-08 21:51:14 +01:00
end
end)
2021-01-31 20:22:32 +01:00
mqtttimer:start()
2020-12-08 21:51:14 +01:00
end
2020-12-10 21:11:35 +01:00
end