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

标题: esphome屏幕挂灯 [打印本页]

作者: hugh_jie    时间: 2023-6-27 12:26
标题: esphome屏幕挂灯
本帖最后由 hugh_jie 于 2023-12-15 22:39 编辑

帖子中编码器部分代码参考了:
https://bbs.hassbian.com/forum.p ... tid=7200&pid=327693
切换灯光部分代码参考了:
小风扇小改造
https://bbs.hassbian.com/thread-8776-1-1.html
非常感谢


配件





组装


唯一需要注意的就是灯带贴底边槽,放在它设计的槽的话灯光范围会很大



[attach]49089[/attach][attach]49079[/attach][attach]49080[/attach][attach]49081[/attach][attach]49082[/attach][attach]49083[/attach][attach]49084[/attach][attach]49085[/attach][attach]49086[/attach][attach]49087[/attach][attach]49104[/attach]
[attach]49105[/attach]

功能


实体按钮,按一下打开灯光,继续按切换自己设的静态灯光效果,我自己只弄了4种,可以随便加

旋钮用于调节亮度

长按按钮关闭灯光

对HA中的功能无影响,预设的动态灯光效果只能在HA中调用



代码


使用的esphome


  1. globals:                        #全局变量,用于按钮调颜色
  2.   - id: option
  3.     type: int
  4.     restore_value: no
  5.     initial_value: '1'          #打开灯光后的初始值


  6. light:
  7.   - platform: neopixelbus
  8.     type: GRB
  9.     variant: WS2812
  10.     pin: D7
  11.     num_leds: 26
  12.     id: light_1
  13.     name: "PC-screenlight"
  14.     effects:                     #预设的动态灯光效果,基本用不上,只在HA里面用
  15.       - addressable_rainbow:
  16.       - addressable_color_wipe:
  17.       - addressable_scan:
  18.       - addressable_twinkle:
  19.       - addressable_random_twinkle:
  20.       - addressable_fireworks:
  21.       - addressable_flicker:
  22.     default_transition_length: 1.5s

  23. # 编码器旋钮
  24. sensor:
  25.   - platform: rotary_encoder
  26.     pin_a: D5
  27.     pin_b: D6
  28.     id: ec11
  29.     internal: true
  30.     min_value: 1
  31.     max_value: 100
  32.     resolution: 4
  33.     icon: mdi:speedometer
  34.     on_value:
  35.       then:
  36.         - light.control:
  37.             id: light_1
  38.             state: on
  39.             brightness: !lambda |-
  40.               return id(ec11).state/100.0;
  41.             transition_length: 0s


  42. # 编码器按钮
  43. binary_sensor:
  44.   - platform: gpio
  45.     id: button
  46.     pin:
  47.       number: D3
  48.       inverted: true              #上拉后反向
  49.       mode:
  50.         input: true
  51.         pullup: true              #上拉,否则疯狂输出
  52.     internal: true
  53.     on_click:
  54.       - min_length: 50ms          #短按,打开和切换静态灯光
  55.         max_length: 350ms
  56.         then:
  57.           - lambda: |-
  58.               if (id(option) > 2) {
  59.                     id(option) = 0;
  60.                   } else {
  61.                     id(option) += 1;
  62.                   }
  63.           - if:
  64.               condition:
  65.                 lambda: |-
  66.                   return id(option) == 0;
  67.               then:
  68.                 - switch.turn_on: pure_white
  69.           - if:
  70.               condition:
  71.                 lambda: |-
  72.                   return id(option) == 1;
  73.               then:
  74.                 - switch.turn_on: warm_white1
  75.           - if:
  76.               condition:
  77.                 lambda: |-
  78.                   return id(option) == 2;
  79.               then:
  80.                 - switch.turn_on: warm_white2
  81.           - if:
  82.               condition:
  83.                 lambda: |-
  84.                   return id(option) == 3;
  85.               then:
  86.                  - switch.turn_on: warm_yellow
  87.       - min_length: 500ms
  88.         max_length: 2000ms
  89.         then:
  90.           - light.toggle: light_1

  91. # 预设静态灯光
  92. switch:
  93.   - platform: gpio
  94.     name: "screen_cool_fan"
  95.     pin: D1
  96.     id:  screen_cool
  97.    

  98.   - platform: template
  99.     id: pure_white          #近纯白
  100.     turn_on_action:
  101.       then:
  102.         - switch.turn_off: warm_white1
  103.         - switch.turn_off: warm_white2
  104.         - switch.turn_off: warm_yellow
  105.         - light.turn_on:
  106.             id: light_1
  107.             brightness: 100%
  108.             red: 100%
  109.             green: 96.8%
  110.             blue: 86.7%
  111.   - platform: template
  112.     id: warm_white1        #暖白1
  113.     turn_on_action:
  114.       then:
  115.         - switch.turn_off: pure_white
  116.         - switch.turn_off: warm_white2
  117.         - switch.turn_off: warm_yellow
  118.         - light.turn_on:
  119.             id: light_1
  120.             brightness: 100%
  121.             red: 100%
  122.             green: 93.7%
  123.             blue: 74.9%
  124.   - platform: template
  125.     id: warm_white2        #暖白2
  126.     turn_on_action:
  127.       then:
  128.         - switch.turn_off: warm_white1
  129.         - switch.turn_off: pure_white
  130.         - switch.turn_off: warm_yellow
  131.         - light.turn_on:
  132.             id: light_1
  133.             brightness: 100%
  134.             red: 100%
  135.             green: 90.98%
  136.             blue: 63.14%
  137.   - platform: template
  138.     id: warm_yellow        #暖黄
  139.     turn_on_action:
  140.       then:
  141.         - switch.turn_off: warm_white1
  142.         - switch.turn_off: warm_white2
  143.         - switch.turn_off: pure_white
  144.         - light.turn_on:
  145.             id: light_1
  146.             brightness: 100%
  147.             red: 100%
  148.             green: 87%
  149.             blue: 50.2%
复制代码

2023.12.15更新ws2812亮度还是不爽,且显色太差了,重新买了根据说98显指的双色温灯带,24v的,壳子还是用原来的,现在这个就需要外接电源了,使用了2个mos管
长这样
[attach]54374[/attach]

接线图
[attach]54375[/attach]

换了esp32c3是因为8266的pwm貌似只能输出1000Hz,加个pwm模块又不太方便在esphome里面控制,所以改了这个,而且它是真的便宜
新的yaml:
  1. output:
  2.   - platform: ledc
  3.     pin: GPIO6
  4.     id: output1
  5.     frequency: 4882Hz
  6.     channel: 0
  7.     min_power: 0.025575
  8.     max_power: 0.744524556
  9.     zero_means_zero: true
  10.   - platform: ledc
  11.     pin: GPIO10
  12.     id: output2
  13.     frequency: 4882Hz
  14.     channel: 4
  15.     min_power: 0.034351
  16.     max_power: 0.69912
  17.     zero_means_zero: true


  18. light:
  19.   - platform: cwww
  20.     name: "light"
  21.     id: light1
  22.     icon: mdi:lightbulb-fluorescent-tube-outline
  23.     cold_white: output1
  24.     warm_white: output2
  25.     cold_white_color_temperature: 6000 K
  26.     warm_white_color_temperature: 3000 K
  27.     constant_brightness: true
  28.     default_transition_length: 0.2s


  29. switch:
  30.   - platform: gpio
  31.     name: "cool_fan"
  32.     pin: GPIO3

  33. sensor:
  34.   - platform: rotary_encoder
  35.     internal: true
  36.     pin_a: GPIO18
  37.     pin_b: GPIO19
  38.     id: ec11
  39.     min_value: 0
  40.     max_value: 100
  41.     resolution: 1
  42.     on_clockwise:
  43.       then:
  44.         light.dim_relative:
  45.           id: light1
  46.           transition_length: 0.2s
  47.           relative_brightness: -5%

  48.     on_anticlockwise:
  49.       then:
  50.         light.dim_relative:
  51.           id: light1
  52.           transition_length: 0.2s
  53.           relative_brightness: 5%

  54.          
  55. binary_sensor:
  56.   - platform: gpio
  57.     id: button
  58.     pin:
  59.       number: GPIO7
  60.       inverted: true
  61.       mode:
  62.         input: true
  63.         pullup: true
  64.     internal: true
  65.     on_click:
  66.       then:
  67.         - light.toggle: light1
复制代码
说明:
1.min_power和max_power用于调节pwm的控制范围,可以避免HA里面的调节虚位,需要根据实际情况调节,先不加,根据自己的灯带最大和最小亮度确定亮度百分比,比如最小亮度是10%,那么min_power就是0.1^2.8,max_power同理
2.ec11的按钮可以添加一个按下旋转调节色温的功能,B站有个大佬有发,但是我觉得没必要所以没有借鉴,有需要的自己去找额
3.这个mos管我买的店没有pwm范围,找同款有说20k以下,我拿到了就直接写了20kHz,结果亮度调节范围很小,而且低亮度闪烁严重,1000Hz基本可以覆盖0~100%,但是网上说3125以上才能完全不伤眼,所以改了个esphome推荐的几个频率中最接近的4882Hz,然后就需要使用min和max_power来修改一下

作者: sorrypqa    时间: 2023-6-27 12:52
不错吗,我还没玩过rgb
作者: hugh_jie    时间: 2023-6-27 13:15
sorrypqa 发表于 2023-6-27 12:52
不错吗,我还没玩过rgb

家里rgb确实使用场景不多,我这之前也没用rgb,屏幕挂灯确实用不上,只是那个灯带用久了感觉有点闪才换的
作者: DDDear    时间: 2023-6-27 14:25
没有效果图呀

作者: fung1006    时间: 2023-6-27 15:53
实际效果图?亮度可以吗?
作者: flyice    时间: 2023-6-27 19:41
秀儿!
作者: hugh_jie    时间: 2023-6-27 20:09
DDDear 发表于 2023-6-27 14:25
没有效果图呀

之前是白天,没拍
作者: hugh_jie    时间: 2023-6-27 20:13
fung1006 发表于 2023-6-27 15:53
实际效果图?亮度可以吗?

有了,亮度我感觉略低了点,主要是壳子是我之前的,长度受限了,重头做的话可以长点,或者有单独电源的话选灯密一点的,我这是usb供电,只有5v2A
作者: fbll    时间: 2023-6-28 17:07
这东西玩玩可以的,不过真的用,屏幕挂灯技术还是很高的,否者一个灯也不会一千多了
作者: 花落花空    时间: 2023-7-3 09:57
想法不错,白光最好还是用一个wwcw的等待来做,2812混出来的白光偏冷
作者: ChristianSwift    时间: 2023-7-3 10:56
2812.....这真是突击步枪打蚊子
作者: xinghevss    时间: 2023-7-3 22:28
大佬太强了 666
作者: 山岚    时间: 2023-7-20 17:13
NB牛逼实数牛逼
作者: henry2022    时间: 2023-7-23 10:15
WS2812这种灯珠的发光显指很低,建议你买高显指的最好是RA>98那种的,对眼睛好,蓝光低,尤其是屏幕挂灯这种的,对眼睛好点,WS28**系列适合做装饰灯光,不适合照明。
作者: hugh_jie    时间: 2023-7-28 13:25
henry2022 发表于 2023-7-23 10:15
WS2812这种灯珠的发光显指很低,建议你买高显指的最好是RA>98那种的,对眼睛好,蓝光低,尤其是屏幕挂灯这 ...

感谢提醒




欢迎光临 『瀚思彼岸』» 智能家居技术论坛 (https://bbs.hassbian.com/) Powered by Discuz! X3.5