『瀚思彼岸』» 智能家居技术论坛

 找回密码
 立即注册
查看: 5268|回复: 4

NodeMCU V3控制WS2812灯带接入HA【2020-3-16更新全功能版本】

[复制链接]

12

主题

165

帖子

1613

积分

论坛技术达人

积分
1613
金钱
1433
HASS币
60
发表于 2020-2-25 14:17:56 | 显示全部楼层 |阅读模式
本帖最后由 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刷机都和原文一样。云编译固件时注意选择这些内容: QQ图片20200225140903.png
接下来就简单啦:
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
    


然后效果是这样的[更新了效果控制]:
截屏2020-02-2609.27.18.png

下拉切换灯光效果。




QQ截图20200225141527.png
QQ截图20200225141405.png
QQ截图20200225141222.png

评分

参与人数 1金钱 +20 收起 理由
+ 20 感谢楼主分享!

查看全部评分

回复

使用道具 举报

8

主题

382

帖子

1779

积分

金牌会员

Rank: 6Rank: 6

积分
1779
金钱
1397
HASS币
0
发表于 2020-2-25 17:56:28 | 显示全部楼层
本帖最后由 peter5858 于 2020-2-25 17:57 编辑

谢谢分享,这个是什么软件呢?哪里可以下载

                               
登录/注册后可看大图

回复

使用道具 举报

26

主题

2095

帖子

8503

积分

论坛元老

Rank: 8Rank: 8

积分
8503
金钱
6403
HASS币
30

论坛元老

发表于 2020-2-25 17:59:12 | 显示全部楼层
这个云编译有点意思呀,分享一下呗
回复

使用道具 举报

0

主题

204

帖子

1035

积分

金牌会员

Rank: 6Rank: 6

积分
1035
金钱
831
HASS币
0
发表于 2020-2-25 20:06:48 | 显示全部楼层
回复

使用道具 举报

12

主题

165

帖子

1613

积分

论坛技术达人

积分
1613
金钱
1433
HASS币
60
 楼主| 发表于 2020-2-26 09:19:10 | 显示全部楼层
zsyg 发表于 2020-2-25 20:06
谢谢分享
文中提到参考的文章https://bbs.hassbian.com/thread-739-1-11.html中找到
https://nodemcu-build ...

谢谢帮发
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-4-20 04:59 , Processed in 0.059590 second(s), 31 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表