2021-12-04 13:04:12 +01:00
|
|
|
-- Global Variables
|
|
|
|
-- Display Temp
|
|
|
|
tw=nil
|
|
|
|
tcol=nil
|
|
|
|
-- Module Variables
|
|
|
|
-- Mqtt variable
|
|
|
|
local mMqttClient=nil
|
|
|
|
local mMqttConnected = false
|
|
|
|
-- Read Temp
|
|
|
|
local mOldTemp=nil
|
2020-12-14 19:45:09 +01:00
|
|
|
|
2021-01-15 18:08:56 +01:00
|
|
|
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)
|
2021-01-15 18:08:56 +01:00
|
|
|
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)
|
2021-01-15 18:08:56 +01:00
|
|
|
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)
|
2021-01-15 18:08:56 +01:00
|
|
|
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)
|
2021-01-15 18:08:56 +01:00
|
|
|
else
|
|
|
|
print "Unknown MQTT command"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2021-01-23 10:05:18 +01:00
|
|
|
-- 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()
|
2021-01-23 18:10:53 +01:00
|
|
|
-- Total DS18B20 numbers
|
2021-12-04 13:04:12 +01:00
|
|
|
local sensors=table.getn(addrs)
|
2021-01-23 18:10:53 +01:00
|
|
|
local temp1=0
|
|
|
|
if (sensors >= 1) then
|
2021-11-28 12:19:19 +01:00
|
|
|
temp1=ds18b20.read(addrs[0])
|
2021-11-28 09:49:12 +01:00
|
|
|
else
|
|
|
|
print("No sensor DS18B20 found")
|
2021-01-23 18:10:53 +01:00
|
|
|
end
|
|
|
|
return temp1
|
|
|
|
else
|
2021-11-28 09:49:12 +01:00
|
|
|
print("No DS18B20 lib")
|
2021-01-23 18:10:53 +01:00
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-03 14:03:59 +02:00
|
|
|
-- 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)
|
2021-04-03 14:03:59 +02:00
|
|
|
print("MQTT is connected")
|
2021-12-04 13:04:12 +01:00
|
|
|
mMqttConnected = true
|
2021-04-03 14:03:59 +02:00
|
|
|
-- subscribe topic with qos = 0
|
2021-12-04 13:04:12 +01:00
|
|
|
mMqttClient:subscribe(mqttPrefix .. "/cmd/#", 0)
|
2021-04-03 14:03:59 +02:00
|
|
|
local tmr1 = tmr.create()
|
2021-11-28 12:19:19 +01:00
|
|
|
tmr1:register(1000, tmr.ALARM_SINGLE, function (dummyTemp)
|
2021-04-03 14:03:59 +02:00
|
|
|
-- 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)
|
2021-04-03 14:03:59 +02:00
|
|
|
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)
|
2021-04-03 14:03:59 +02:00
|
|
|
tmr1:unregister()
|
2021-12-04 13:04:12 +01:00
|
|
|
tmr1=nil
|
2021-04-03 14:03:59 +02:00
|
|
|
end)
|
|
|
|
tmr1:start()
|
|
|
|
end,
|
|
|
|
function(client, reason)
|
|
|
|
print("failed reason: " .. reason)
|
2021-12-04 13:04:12 +01:00
|
|
|
mMqttConnected = false
|
2021-04-03 14:03:59 +02:00
|
|
|
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)
|
2021-11-01 19:49:56 +01:00
|
|
|
elseif (topic == (mqttPrefix .. "/cmd/temp")) then
|
|
|
|
if (( data == "" ) or (data == nil)) then
|
|
|
|
tw=nil
|
|
|
|
tcol=nil
|
|
|
|
print("MQTT | wordclock failed")
|
2021-11-01 13:01:40 +01:00
|
|
|
else
|
2021-11-01 19:02:12 +01:00
|
|
|
-- generate the temperatur to display, once as it will not change
|
|
|
|
local dispTemp = tonumber(data)
|
|
|
|
collectgarbage()
|
|
|
|
mydofile("wordclock")
|
|
|
|
if (wc ~= nil) then
|
|
|
|
tw, tcol = wc.temp(dw, rgbBuffer, invertRows)
|
|
|
|
wc = nil
|
2021-11-01 19:49:56 +01:00
|
|
|
print("MQTT | generated words for: " + tostring(dispTemp))
|
2021-11-01 19:02:12 +01:00
|
|
|
else
|
|
|
|
print("MQTT | wordclock failed")
|
|
|
|
end
|
2021-11-01 13:01:40 +01:00
|
|
|
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-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")
|
2021-01-23 16:44:16 +01:00
|
|
|
if (startTelnetServer ~= nil) then
|
|
|
|
startTelnetServer()
|
2021-03-21 20:52:27 +01:00
|
|
|
else
|
|
|
|
print("Cannost start Telnet Server!")
|
2021-01-23 16:44:16 +01:00
|
|
|
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
|
|
|
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)
|
2021-03-14 13:44:54 +01:00
|
|
|
print("MQTT Disconnected")
|
2021-12-04 13:04:12 +01:00
|
|
|
mMqttConnected = false
|
|
|
|
collectgarbage()
|
|
|
|
end)
|
2021-04-03 14:03:59 +02:00
|
|
|
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)
|
2021-11-01 13:38:22 +01:00
|
|
|
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"
|
2021-11-01 13:38:22 +01:00
|
|
|
end
|
|
|
|
end)
|
2021-12-04 13:04:12 +01:00
|
|
|
lSetupTimer:start()
|
|
|
|
local loldBrightness=0
|
|
|
|
mOldTemp=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
|
2021-11-28 19:02:29 +01:00
|
|
|
local heapusage = node.heap()
|
2021-11-28 09:49:12 +01:00
|
|
|
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
|
2020-12-14 19:45:09 +01:00
|
|
|
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
|
2022-01-20 21:49:35 +01:00
|
|
|
ds18b20_diet=nil
|
|
|
|
package.loaded["ds18b20_diet"]=nil
|
2022-01-20 21:12:54 +01:00
|
|
|
tempCounter = 0
|
|
|
|
else
|
|
|
|
tempCounter = tempCounter + 1
|
|
|
|
end
|
|
|
|
collectgarbage()
|
2021-12-04 13:04:12 +01:00
|
|
|
if (temperatur ~= nil and temperatur ~= mOldTemp) then
|
|
|
|
mOldTemp = temperatur
|
|
|
|
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
|
2020-12-14 19:45:09 +01:00
|
|
|
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
|
2021-04-03 14:03:59 +02:00
|
|
|
|