硬件
卸下底座胶垫下的三颗螺丝即可拆开,按照 Tasmota 文档 [存档] 给 ESP 模块焊上需要的引线:
- 黑色:GND,连接至 PIN3、5、6、7、15、16、19、20 均可
- 白色:Vcc(+3.3 V),连接至 PIN8
- 灰色:UART RxD,连接至 PIN17
- 紫色:UART TxD,连接至 PIN18
另外短接 PIN4(GPIO0)和 PIN3(GND),再用镊子短接 PIN9(RST)和 PIN7(GND)后释放,让 ESP 重置后进入烧写模式。
软件
ESPHome 配置示例:
来源:https://github.com/syssi/esphome-mi-desk-lamp
substitutions:
default_color_temperature: '250.0' # 默认色温,按需调整(单位为 mireds,1000000 / 4000 K = 250 mireds)
esphome:
name: lamp
comment: '小米米家台灯'
esp8266:
board: esp01_1m # 1 MB 闪存
restore_from_flash: true
logger:
level: WARN
baud_rate: 115200
wifi:
fast_connect: true
networks:
- ssid: !secret wifi_ssid
bssid: !secret wifi_bssid
password: !secret wifi_password
domain: !secret domain_name
api:
ota:
password: !secret ota_password
button:
# 硬重启
- platform: restart
id: restart_button
name: '重启台灯'
entity_category: diagnostic
globals:
- id: color_temperature
type: float
restore_value: no
initial_value: ${default_color_temperature}
sensor:
- platform: rotary_encoder
id: rotation
pin_a: 13
pin_b: 12
resolution: 2
on_value:
then:
- if:
condition:
# 检查旋转旋钮时其是否被按下
lambda: 'return id(rotary_encoder_button).state;'
then:
# 如果被按下,则调整色温
- lambda: |-
auto min_temp = id(light_).get_traits().get_min_mireds();
auto max_temp = id(light_).get_traits().get_max_mireds();
auto cur_temp = id(light_).current_values.get_color_temperature();
id(color_temperature) = max(min_temp, min(max_temp, cur_temp + (x * 10)));
auto call = id(light_).turn_on();
call.set_color_temperature(id(color_temperature));
call.perform();
else:
# 如果没被按下,则调整亮度
- light.dim_relative:
id: light_
relative_brightness: !lambda 'return x / 25.0;'
# 重置旋转值
- sensor.rotary_encoder.set_value:
id: rotation
value: 0
internal: true
binary_sensor:
- platform: gpio
id: rotary_encoder_button
pin:
number: 2
inverted: true
on_multi_click:
# 双击则重置色温至默认
- timing:
- ON for at most 1s
- OFF for at most 1s
- ON for at most 1s
- OFF for at least 0.2s
then:
- lambda: |-
id(color_temperature) = ${default_color_temperature};
auto call = id(light_).turn_on();
call.set_color_temperature(id(color_temperature));
call.set_transition_length(500); // 按需调整过渡时长(毫秒)
call.perform();
# 单击则开关灯
- timing:
- ON for at most 1s
- OFF for at least 0.5s
then:
- light.toggle:
id: light_
transition_length: 0.5s # 按需调整过渡时长
internal: true
# 将自带重置按钮用作硬重启按钮
- platform: gpio
id: reset_button
pin:
number: 14
inverted: true
internal: true
on_release:
then:
- button.press: restart_button
internal: true
output:
# 冷白色通道
- platform: esp8266_pwm
id: output_cold
pin: 4
# 暖白色通道
- platform: esp8266_pwm
id: output_warm
pin: 5
light:
- platform: cwww
id: light_
name: '台灯'
icon: mdi:desk-lamp
cold_white: output_cold
warm_white: output_warm
cold_white_color_temperature: 6500 K
warm_white_color_temperature: 2700 K
default_transition_length: 0s
constant_brightness: true
gamma_correct: 0
on_turn_on:
- light.control:
id: light_
state: on
color_temperature: !lambda 'return id(color_temperature);'
on_turn_off:
- lambda: 'id(color_temperature) = id(light_).current_values.get_color_temperature();'
restore_mode: RESTORE_DEFAULT_OFF
重置 ESP 模块后稍等,Home Assistant 会通知已自动发现新 ESPHome 设备,直接选择接入即可。之后即可添加原生带亮度与色温调整的灯具卡片至 lovelace。
关于扩展功能
As the lamp has external power supply, and the LEDs are in the upper part of the lamp, the temperature of the base is the same as of the ambient, and by its nature the lamp is situated on your desk, so it's a perfect place for temp+humidity measurements.
在 GPIO0 接了一个 DHT22 温湿度传感器,但实际使用一段时间后发现效果并不好,台灯一点亮就会让温度读数马上升高 5 ℃ 左右。
0 Comments