2020-12-14 19:45:09 +01:00
|
|
|
-- Global variable
|
2021-01-14 21:27:13 +01:00
|
|
|
m=nil
|
2020-12-14 19:45:09 +01:00
|
|
|
mqttConnected = false
|
2021-01-23 18:10:53 +01:00
|
|
|
-- Temp:
|
|
|
|
t=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
|
|
|
|
briPercent=100
|
|
|
|
m:publish(mqttPrefix .. "/clock", "ON", 0, 0)
|
|
|
|
elseif (data == "OFF") then
|
|
|
|
briPercent=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-01-15 20:32:39 +01:00
|
|
|
colorBg=string.char(green * briPercent / 100, red * briPercent / 100, blue * briPercent / 100)
|
2021-01-15 18:08:56 +01:00
|
|
|
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)
|
2021-01-15 18:08:56 +01:00
|
|
|
if (displayTime~= nil) then
|
|
|
|
displayTime()
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if (tonumber(data) >= 0 and tonumber(data) <= 100) then
|
|
|
|
briPercent=tonumber(data)
|
|
|
|
m:publish(mqttPrefix .. "/clock", tostring(data), 0, 0)
|
|
|
|
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
|
|
|
|
function parseBgColor(data, row)
|
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-01-23 10:05:18 +01:00
|
|
|
m:publish(mqttPrefix .. "/"..row, tostring(red) .. "," .. tostring(green) .. "," .. tostring(blue), 0, 0)
|
2021-01-23 09:44:15 +01:00
|
|
|
return string.char(green * briPercent / 100, red * briPercent / 100, blue * briPercent / 100)
|
|
|
|
else
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-23 18:10:53 +01:00
|
|
|
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
|
|
|
|
|
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(topic .. ":" )
|
|
|
|
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)
|
|
|
|
else
|
|
|
|
-- Handle here the /cmd/# sublevel
|
|
|
|
if (string.match(topic, "telnet$")) then
|
|
|
|
client:publish(mqttPrefix .. "/telnet", tostring(wifi.sta.getip()), 0, 0)
|
|
|
|
print("Stop Mqtt")
|
|
|
|
m=nil
|
|
|
|
mqttConnected = false
|
|
|
|
stopWordclock()
|
|
|
|
collectgarbage()
|
|
|
|
mydofile("telnet")
|
2021-01-23 16:44:16 +01:00
|
|
|
if (startTelnetServer ~= nil) then
|
|
|
|
startTelnetServer()
|
|
|
|
end
|
2021-01-23 09:44:15 +01:00
|
|
|
elseif (string.match(topic, "row1$")) then
|
2021-01-23 10:05:18 +01:00
|
|
|
row1bgColor = parseBgColor(data, "row1")
|
2021-01-15 18:33:49 +01:00
|
|
|
end
|
|
|
|
end
|
2020-12-07 21:26:02 +01:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
m:connect(mqttServer, 1883, 0, function(client)
|
2020-12-07 21:37:04 +01:00
|
|
|
print("MQTT is connected")
|
2020-12-07 21:26:02 +01:00
|
|
|
mqttConnected = true
|
|
|
|
-- subscribe topic with qos = 0
|
2021-01-15 19:03:20 +01:00
|
|
|
client:subscribe(mqttPrefix .. "/cmd/#", 0)
|
|
|
|
tmr.alarm(3, 1000, 0, function()
|
2021-01-14 21:27:13 +01:00
|
|
|
-- publish a message with data = hello, QoS = 0, retain = 0
|
|
|
|
client:publish(mqttPrefix .. "/ip", tostring(wifi.sta.getip()), 0, 0)
|
2021-01-15 20:32:39 +01:00
|
|
|
local red = string.byte(colorBg,2)
|
|
|
|
local green = string.byte(colorBg,1)
|
2021-01-15 19:28:20 +01:00
|
|
|
local blue = string.byte(colorBg,3)
|
|
|
|
client:publish(mqttPrefix .. "/background", tostring(red) .. "," .. tostring(green) .. "," .. tostring(blue), 0, 0)
|
2021-01-14 21:27:13 +01:00
|
|
|
end)
|
2020-12-07 21:26:02 +01:00
|
|
|
end,
|
|
|
|
function(client, reason)
|
|
|
|
print("failed reason: " .. reason)
|
2021-01-14 21:32:12 +01:00
|
|
|
mqttConnected = false
|
2020-12-07 21:26:02 +01:00
|
|
|
end)
|
|
|
|
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-01-23 18:10:53 +01:00
|
|
|
if (file.open("ds18b20.lc")) then
|
|
|
|
t=require("ds18b20")
|
|
|
|
t.setup(2) -- GPIO4
|
|
|
|
readTemp() -- read once, to setup chip
|
|
|
|
print "Setup temperature"
|
|
|
|
end
|
2020-12-08 21:51:14 +01:00
|
|
|
oldBrightness=0
|
2020-12-14 19:45:09 +01:00
|
|
|
oldTemp=0
|
2020-12-10 21:11:35 +01:00
|
|
|
tmr.alarm(5, 5001, 1 ,function()
|
2020-12-14 19:45:09 +01:00
|
|
|
if (mqttConnected) then
|
|
|
|
local temp = nil
|
|
|
|
if (t ~= nil) then
|
|
|
|
temp=readTemp()
|
2021-01-23 18:10:53 +01:00
|
|
|
print(tostring(temp) .. "°C")
|
2020-12-14 19:45:09 +01:00
|
|
|
end
|
|
|
|
if (oldBrightness ~= briPercent) then
|
|
|
|
m:publish(mqttPrefix .. "/brightness", tostring(briPercent), 0, 0)
|
2021-01-23 18:10:53 +01:00
|
|
|
elseif (temp ~= nil and temp ~= oldTemp) then
|
|
|
|
oldTemp = temp
|
|
|
|
m:publish(mqttPrefix .. "/temp", tostring(temp/100).."."..tostring(temp%100), 0, 0)
|
2020-12-14 19:45:09 +01:00
|
|
|
else
|
|
|
|
m:publish(mqttPrefix .. "/heap", tostring(node.heap()), 0, 0)
|
|
|
|
end
|
|
|
|
oldBrightness = briPercent
|
2020-12-08 21:51:14 +01:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
2020-12-10 21:11:35 +01:00
|
|
|
end
|