本帖最后由 szlww 于 2023-12-17 22:55 编辑
闲鱼搜 铁通插排
提供esphome两个配置文件,一个通用版,一个自用版,按需选择
1.通用版本
基本通断功能
substitutions:
node_name: cmcc-powerboard
node_id: cmcc_powerboard
node_name_friendly: ${node_name}
esphome:
name: ${node_name}
min_version: 2023.11.4
esp8266:
board: esp_wroom_02
restore_from_flash: false
wifi:
ap:
ssid: "${node_id} hotspot"
password: !secret ap_password
captive_portal:
logger:
level: DEBUG
api:
encryption:
key: !secret api_key
reboot_timeout: 0s
ota:
password: !secret ota_password
web_server:
port: 80
include_internal: true
version: 1
#插排电源按钮
binary_sensor:
- platform: gpio
name: button4
pin:
number: 4
inverted: true
mode:
input: true
pullup: true
on_press:
then:
- switch.toggle: switch_button
#通电指示灯
light:
- platform: binary
name: "light_power_status_output_gpio5"
id: light_power_status_output_gpio5
output: poweron_output_gpio5
output:
- platform: gpio
id: output_gpio15
pin:
number: 15
inverted: true
- id: poweron_output_gpio5
platform: gpio
pin:
number: 5
inverted: true
button:
- platform: output
name: "button_output_gpio15"
id: button_output_gpio15
output: output_gpio15
duration: 50ms
internal: true
switch:
#虚拟开关
- platform: template
name: switch_button
id: switch_button
lambda: |-
return id(switch_gpio0).state ? true: false;
turn_on_action:
- switch.turn_on: switch_gpio0
- switch.turn_on: switch_gpio12
- light.turn_on: light_power_status_output_gpio5
turn_off_action:
- switch.turn_off: switch_gpio12
- switch.turn_off: switch_gpio0
- light.turn_off: light_power_status_output_gpio5
#干路继电器
- platform: gpio
name: switch_gpio0
id: switch_gpio0
pin:
number: 0
inverted: true
on_turn_on:
then:
- button.press: button_output_gpio15
on_turn_off:
- button.press: button_output_gpio15
#分路继电器
- platform: gpio
name: switch_gpio12
id: switch_gpio12
pin:
number: 12
inverted: true
on_turn_on:
then:
- button.press: button_output_gpio15
on_turn_off:
- button.press: button_output_gpio15
#重启
- platform: restart
name: "${node_name_friendly}-Restart"
id: ${node_id}_restart
#重置
- platform: factory_reset
name: ${node_name}-factory-reset
id: ${node_id}_factory_reset
2.自用版本
支持断电记忆、按钮功能智能与普通模式选择、重新配网,指示灯状态等功能(无计量功能)
###########################################################################################
#注意事项:
# 1.固件的编译环境为esphome-v2023.11.4,可正常编译运行;不想升级esphome版本,则需要修改min_version的值,尝试编译固件。
# 2.OTA升级固件之后,请长按电源键5秒,等待蓝灯闪烁,重置设备并重新配网,避免出现奇怪的问题。
#固件功能:
#一.插排按钮:
# 1.实现智能模式与普通模式。
# 功能可配置,为normal或者smart模式,web界面或者homeassistant中下拉框选择。
# normal: 普通模式,插排按钮按下之后全开或者全关
# smart: 智能模式(此模式为默认模式),插排按钮按下之后只操作[主继电器]通断,不改变[从继电器]之前的状态
# 2.长按、双击、单击操作
# 长按:按住电源按钮5秒以上,插排会重置flash上的信息(包括插座记忆状态),并进入配网状态,
# 双击:双击电源键,从继电器接通或断开
# 单击:单击电源键,按下实现第[1]条功能配置的相应模式(智能模式/普通模式)
#二.指示灯
# 蓝灯常亮:wifi已连接
# 蓝灯熄灭:wifi未连接
# 蓝灯闪烁:ap配网模式
# 红灯闪烁/熄灭:esphome status_led light
# 白灯常亮:从继电器 真正通电
# 白灯熄灭:从继电器 真正断电
#三.断电记忆
# 记忆状态写入间隔1分钟,也就是说开关等组件状态发生变化,1分钟后才会被记录到flash,下次通电之后可恢复这个状态(可以配置成0s,所有状态变化立刻写入flash,但频繁的状态变化会缩短flash寿命。)
#四.计量功能 - todo
############################################################################################
substitutions:
button_pin: GPIO4
relay_master_pin: GPIO0
relay_slave_pin: GPIO12
relay_enable_pin: GPIO15
led_blue_pin: GPIO16
led_red_pin: GPIO14
led_white_pin: GPIO5
node_name: cmcc-powerboard
node_id: cmcc_powerboard
node_name_friendly: ${node_name}
esphome:
name: ${node_name}
project:
name: "CMCC.CMPOWER W1"
version: "1.0.0"
min_version: 2023.11.4
on_boot:
- priority: 0
then:
- lambda: |-
if(nullptr != esphome::captive_portal::global_captive_portal)
{
//配网模式,蓝色led闪烁
if(esphome::captive_portal::global_captive_portal->is_active() && !id(${node_id}_wifi).is_connected())
{
id(led_blue).turn_off().perform();
id(led_blue).turn_on().set_effect("fast").perform();
}
}
esp8266:
board: esp_wroom_02
restore_from_flash: true
preferences:
flash_write_interval: 1min
wifi:
id: ${node_id}_wifi
ap:
ssid: "${node_id} hotspot"
password: !secret ap_password
on_connect:
- light.turn_on:
id: led_blue
effect: "None"
on_disconnect:
- light.turn_off: led_blue
captive_portal:
logger:
level: INFO
api:
encryption:
key: !secret api_key
#避免未接入homeassistant时,模块一直重启(默认15分钟重启一次)
reboot_timeout: 0s
ota:
password: !secret ota_password
web_server:
port: 80
include_internal: true
version: 2
output:
#主、从继电器触发引脚
- platform: gpio
id: pin_trigger_relay_enable
pin:
number: ${relay_enable_pin}
inverted: true
#主继电器
- platform: gpio
id: pin_relay_master
pin:
number: ${relay_master_pin}
inverted: true
#从继电器
- platform: gpio
id: pin_relay_slave
pin:
number: ${relay_slave_pin}
inverted: true
#白色led
- platform: esp8266_pwm
id: pin_led_white
pin:
number: ${led_white_pin}
inverted: true
#蓝色led
- platform: esp8266_pwm
id: pin_led_blue
pin:
number: ${led_blue_pin}
inverted: true
#红色led
- platform: esp8266_pwm
id: pin_led_red
pin:
number: ${led_red_pin}
inverted: true
light:
#白色led(分路继电器的状态)
- platform: monochromatic
name: ${node_name_friendly} Led White
id: led_white
output: pin_led_white
restore_mode: ALWAYS_OFF
default_transition_length: 0s
#蓝灯常亮-wifi已连接
#蓝灯闪烁-配网模式
#红灯闪烁,蓝灯熄灭-wifi未连接
#蓝色led(wifi状态)
- platform: monochromatic
name: ${node_name_friendly} Led Blue
id: led_blue
output: pin_led_blue
restore_mode: ALWAYS_OFF
default_transition_length: 0s
effects:
#配网模式
- pulse:
name: "fast"
transition_length: 0ms
update_interval: 100ms
#预留(未使用,可用作指示其他状态)
- pulse:
name: "slow"
transition_length: 200ms
update_interval: 500ms
#红色led(wifi状态与配网状态)
- platform: status_led
name: ${node_name_friendly} Led Red
id: led_red
output: pin_led_red
# 红蓝灯合并(后期实现彩色status_led)
# - platform: cwww
# name: "Status Lights"
# cold_white: pin_led_blue
# warm_white: pin_led_red
# cold_white_color_temperature: 6536 K
# warm_white_color_temperature: 2000 K
# constant_brightness: true
# restore_mode: ALWAYS_OFF
# default_transition_length: 0s
# effects:
# - pulse:
# name: "fast"
# transition_length: 0ms
# update_interval: 100ms
button:
#实现一个电平反转,触发继电器动作
- platform: output
#name: ${node_name_friendly} Relay Trigger
id: trigger_relay_enable
output: pin_trigger_relay_enable
duration: 10ms
internal: true
- platform: template
name: ${node_name_friendly} One-Key-All-Turn-On
id: button_one_key_all_turn_on
on_press:
then:
- switch.turn_on: relay_master_logic
- switch.turn_on: relay_slave_logic
- platform: template
name: ${node_name_friendly} One-Key-All-Turn-Off
id: button_one_key_all_turn_off
on_press:
then:
- switch.turn_off: relay_slave_logic
- switch.turn_off: relay_master_logic
switch:
#插排按钮-逻辑开关
- platform: template
name: ${node_name_friendly} Power Key
id: power_key_logic
lambda: |-
return id(relay_master_real).state;
restore_mode: DISABLED
turn_on_action:
- lambda: |-
auto strMode = id(select_key_button_mode).state;
id(relay_master_logic).turn_on();
//此模式下使用继电器默认记忆状态
if("smart" == strMode)
{
return;
}
//普通插排的逻辑,电源键全开或全关
if("normal" == strMode)
{
id(relay_slave_logic).turn_on();
return;
}
turn_off_action:
- lambda: |-
auto strMode = id(select_key_button_mode).state;
id(relay_master_logic).turn_off();
//此模式下使用继电器的记忆状态
if("smart" == strMode)
{
return;
}
//普通插排的逻辑,电源键全开或全关
if("normal" == strMode)
{
id(relay_slave_logic).turn_off();
return;
}
#由于两个继电器可以由esp芯片独立控制,可能会出现这种场景:[主继电器]断开,[从继电器]打开,homeassistant显示[从继电器]是开启状态,但是此时[从继电器]的线路是没有电的;
#为了避免出现这种令人疑惑的场景,增加逻辑开关。
# 使用逻辑开关,控制[从继电器]的实际通电状态:
# 从继电器接通且主继电器接通,白色指示灯点亮
# 从继电器断开或者主继电器断开,白色指示灯熄灭
#主继电器-逻辑开关
- platform: template
name: ${node_name_friendly} Relay Master
id: relay_master_logic
lambda: |-
return id(relay_master_real).state;
restore_mode: DISABLED
turn_on_action:
- switch.turn_on: relay_master_real
turn_off_action:
- switch.turn_off: relay_master_real
#从继电器-逻辑开关
- platform: template
name: ${node_name_friendly} Relay Slave
id: relay_slave_logic
lambda: |-
return id(relay_master_real).state && id(relay_slave_real).state;
restore_mode: DISABLED
turn_on_action:
- switch.turn_on: relay_slave_real
- switch.turn_on: relay_master_real
turn_off_action:
- switch.turn_off: relay_slave_real
#主继电器-真实开关
- platform: output
#name: ${node_name_friendly} Relay Master Real
id: relay_master_real
output: pin_relay_master
restore_mode: RESTORE_DEFAULT_OFF
on_turn_on:
- button.press: trigger_relay_enable
- lambda: |-
if(id(relay_slave_real).state) id(led_white).turn_on().perform();
on_turn_off:
- button.press: trigger_relay_enable
- lambda: |-
id(led_white).turn_off().perform();
internal: true
#从继电器-真实开关
#internal-true 对前端隐藏,使用relay_slave_logic 代替直接控制 relay_slave_real
- platform: output
#name: ${node_name_friendly} Relay slave Real
id: relay_slave_real
output: pin_relay_slave
restore_mode: RESTORE_DEFAULT_OFF
on_turn_on:
- button.press: trigger_relay_enable
- lambda: |-
if(id(relay_master_real).state) id(led_white).turn_on().perform();
on_turn_off:
- button.press: trigger_relay_enable
- lambda: |-
id(led_white).turn_off().perform();
internal: true
#重启
- platform: restart
name: "${node_name_friendly} Restart"
id: ${node_id}_restart
#重置
- platform: factory_reset
name: ${node_name_friendly} FactoryReset
id: ${node_id}_factory_reset
#插排电源按钮
binary_sensor:
- platform: gpio
#name: ${node_name_friendly} Power Key Real
id: power_key
internal: true
pin:
number: ${button_pin}
inverted: true
mode:
input: true
pullup: true
#插排电源按钮单击、长按功能
on_multi_click:
#长按5s以上,重置为出厂模式,进入配网状态
- timing:
- ON for at least 5s
then:
- logger.log: "Long press."
- switch.turn_on: ${node_id}_factory_reset
invalid_cooldown: 0s
#双击
- timing:
- ON for at most 0.5s
- OFF for at most 0.5s
- ON for at most 0.5s
- OFF for at least 0.2s
then:
- logger.log: "double click."
- switch.toggle: relay_slave_logic
invalid_cooldown: 0s
#单击
- timing:
- ON for at most 0.3s
- OFF for at least 0.2s
then:
- logger.log: "single click."
- switch.toggle: power_key_logic
invalid_cooldown: 0s
#物理按键:智能模式与普通模式
select:
- platform: template
name: ${node_name_friendly} Key Button Mode
id: select_key_button_mode
options:
- "normal" #普通模式,一键强制全开或全关
- "smart" #智能模式,只操作主继电器,不改变[从继电器]以前的状态
initial_option: "smart"
optimistic: true
restore_value: true
|