|
本帖最后由 frx 于 2020-3-16 09:51 编辑
2020-3-16更新,所有功能全部实现版本,请移步:https://bbs.hassbian.com/thread-9498-1-1.html
2020-2-26更新,之前的效果控制部分没找到办法是放在一个开关上控制的,现在OK了,能在灯带属性里面下拉控制了。唯一一点不足就是效果速度的控制还没解决。
对了,把参考文章的两个链接放进来
云编译:https://nodemcu-build.com/index.php
如果要自己定义效果,可以看NodeMCU的文档:https://nodemcu.readthedocs.io/en/latest/
闲在家里没事做,折腾了下灯带,参考了https://bbs.hassbian.com/thread-739-1-11.html这篇文章,但这篇是早在17年的文了,我运行不了,所以做了些调整,便有了这篇。接线和mcu刷机都和原文一样。云编译固件时注意选择这些内容:
接下来就简单啦:
NodeMCU代码在这里(要用的话注意修改wifi ssid和密码,mqtt服务器信息):
print('Wait link to wifi')
wifi.setmode(wifi.STATION)
wifi.sta.sethostname("Node-RGB")
station_cfg={}
station_cfg.ssid="XXXXXXX"
station_cfg.pwd="XXXXXXX"
wifi.sta.config(station_cfg)
ws2812.init()
local color,speed,brightness,mode = "255,165,0",200,100,"static"
strip_buffer = ws2812.newBuffer(215, 3)
strip_buffer:fill(255,165,0)
ws2812.write(strip_buffer)
ws2812_effects.init(strip_buffer)
mytimer = tmr.create()
mytimer:alarm(1000, 1, function()
if wifi.sta.getip() == nil then
print('Waiting for IP ...')
else
print('IP is ' .. wifi.sta.getip())
print('WIFI LINK OK')
effectsInit()
mqttInit()
mytimer:stop()
end
end)
--效果切换
function effectsInit()
print('MODE IS' .. mode)
local aa = lua_string_split(color,",")
r = aa[1]
g = aa[2]
b = aa[3]
ws2812_effects.set_speed(speed)
ws2812_effects.set_brightness(brightness)
if mode == "static" or mode == "blink" or mode == "color_wipe" then
ws2812_effects.set_color(g,r,b)
ws2812_effects.set_mode(mode)
elseif mode == "flicker" then
ws2812_effects.set_color(g,r,b)
ws2812_effects.set_mode(mode,50)
else
ws2812_effects.set_mode(mode)
end
ws2812_effects.start()
end
--连接mqtt服务,监听消息
function mqttInit()
m = mqtt.Client("NodeRGB", 60, "mqtt", "mqtt")
m:lwt("/lwt", "offline", 0, 0)
m:on("connect", function(client) print ("connected") end)
m:on("offline", function(client) print ("offline") end)
m:on("message", function(client, topic, data)
print(topic .. ":" )
if data ~= nil then
print(data)
if topic == "ws2812/setcolor" then
--由HA发送的颜色数据
color = data
--在不支持调色模式时收到调色信息将模式设置为static
if mode ~= "static" and mode ~= "blink" and mode ~= "color_wipe" and mode ~= "flicker" then
mode = "static"
end
effectsInit()
elseif topic == "ws2812/seteffect" then
mode = data
effectsInit()
client:publish("ws2812/switchpub", "ON", 0, 0, function(client) print("sent") end)
elseif topic == "ws2812/setspeed" then
--设置效果的动态时间
speed = data
effectsInit()
elseif topic == "ws2812/setbrightness" then
brightness = data
effectsInit()
elseif topic == "ws2812/switch" then
if data == "OFF" then
ws2812_effects.stop()
strip_buffer:fill(0,0,0)
ws2812.write(strip_buffer)
client:publish("ws2812/switchpub", "OFF", 0, 0, function(client) print("sent") end)
elseif data == "ON" then
effectsInit()
client:publish("ws2812/switchpub", "ON", 0, 0, function(client) print("sent") end)
end
end
end
end)
m:connect("192.168.1.10", 1883, 0, function(client)
print("connected")
client:subscribe("ws2812/switch",0, function(conn) print("subscribe success") end)
client:subscribe("ws2812/setcolor",0, function(conn) print("subscribe success") end)
client:subscribe("ws2812/setbrightness",0, function(conn) print("subscribe success") end)
client:subscribe("ws2812/setspeed",0, function(conn) print("subscribe success") end)
client:subscribe("ws2812/seteffect",0, function(conn) print("subscribe success") end)
end,
function(client, reason)
print("failed reason: " .. reason)
end)
--m:close();
end
--字符串分割函数
function lua_string_split(str, split_char)
local sub_str_tab = {};
while (true) do
local pos = string.find(str, split_char);
if (not pos) then
sub_str_tab[#sub_str_tab + 1] = str;
break;
end
local sub_str = string.sub(str, 1, pos - 1);
sub_str_tab[#sub_str_tab + 1] = sub_str;
str = string.sub(str, pos + 1, #str);
end
return sub_str_tab;
end
HA里面是这样:
mqtt:
broker: 192.168.1.10
port: 1883
light:
- platform: mqtt
name: "灯带"
command_topic: "ws2812/switch"
state_topic: "ws2812/switchpub"
rgb_state_topic: "ws2812/setcolorpub"
rgb_command_topic: "ws2812/setcolor"
brightness_state_topic: "ws2812/setbrightnesspub"
brightness_command_topic: "ws2812/setbrightness"
effect_list: [static, blink, random_color, rainbow, rainbow_cycle, flicker, fire, fire_soft, halloween, circus_combustus, larson_scanner, color_wipe, random_dot]
effect_command_topic: "ws2812/seteffect"
optimistic: true
然后效果是这样的[更新了效果控制]:
下拉切换灯光效果。
|
评分
-
查看全部评分
|