From 9763f4b5739ce7051c21ddd3cf97f835e1169f7a Mon Sep 17 00:00:00 2001 From: Ollo Date: Fri, 15 Jan 2021 19:28:20 +0100 Subject: [PATCH] Publish current background color --- mqtt.lua | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/mqtt.lua b/mqtt.lua index 8b6ce5c..350551f 100644 --- a/mqtt.lua +++ b/mqtt.lua @@ -10,6 +10,9 @@ function handleSingleCommand(client, topic, data) briPercent=0 m:publish(mqttPrefix .. "/clock", "OFF", 0, 0) elseif ((data:sub(1,1) == "#" and data:len() == 7) or (string.match(data, "%d+,%d+,%d+"))) then + local red=0 + local green=0 + local blue=0 if (data:sub(1,1) == "#") then red = tonumber(data:sub(2,3), 16) green = tonumber(data:sub(4,5), 16) @@ -17,8 +20,9 @@ function handleSingleCommand(client, topic, data) else red, green, blue = string.match(data, "(%d+),(%d+),(%d+)") end - colorBg=string.char(green* briPercent / 100, red * briPercent / 100, blue * briPercent / 100) + colorBg=string.char(red* briPercent / 100, green * briPercent / 100, blue * briPercent / 100) print("Updated BG: " .. tostring(red) .. "," .. tostring(green) .. "," .. tostring(blue) ) + m:publish(mqttPrefix .. "/background", tostring(red) .. "," .. tostring(green) .. "," .. tostring(blue), 0, 0) if (displayTime~= nil) then displayTime() end @@ -67,6 +71,10 @@ function registerMqtt() tmr.alarm(3, 1000, 0, function() -- publish a message with data = hello, QoS = 0, retain = 0 client:publish(mqttPrefix .. "/ip", tostring(wifi.sta.getip()), 0, 0) + local red = string.byte(colorBg,1) + local green = string.byte(colorBg,2) + local blue = string.byte(colorBg,3) + client:publish(mqttPrefix .. "/background", tostring(red) .. "," .. tostring(green) .. "," .. tostring(blue), 0, 0) end) end, function(client, reason)