本帖最后由 polisher 于 2023-8-3 10:22 编辑
本人小白水平,未经严格测试,供有兴趣的友友把玩。如有任何建议和问题请一定劳烦告知。(参考trz0332大神的帖子)改造原因:
主要为光猫、路由器、HA服务器等设计的智能化本地执行的断开与恢复设备电源的功能。旨在当设备出现网络问题时,尝试自行断电恢复。
改造内容:
1.新增按钮:延时重启电源。该功能可远程控制电源断开,在间隔设定的时间后自动恢复供电。
2.新增选项:延时时间。可自行设定断电后的间隔时间。
3.新增选项:检测到断网重启电源。选择该项后,每间隔1分钟对互联网进行检测,如发现断网,将自动重启电源。
5.新增选项:启用故障灯。选择该项后,火零线反接与缺地线将触发红色故障灯。
4.新增显示:故障检测(互联网连接)。当“检测到断网重启电源”启用后,显示网络检测情况。
5.调整安全锁功能,当该项启用后,插线板上的电源开关将失效,并不能长按恢复功能。
6.调整插座启动模式,当本插座停电恢复后,尝试恢复上次开关状态,并默认为 开。
主要疑问:
ESPHOME自带的“Safe Mode Switch”似乎并没有什么卵用,不知道是不是我理解有误。
刷机方式:
本固件大小有660KB(
wifis3.bin
(660.22 KB, 下载次数: 36)
),本插线板用的ESP8266的FLASH实在有些捉襟见肘,所以刷机只能线刷,或先用小固件OTA后再用本固件()。
esphome:
name: wifis3
friendly_name: WIFI智能插线板S3
comment: 智能插线板。可检测缺地线、火零接反、联网。主要功能:延时重启电源与断网重启电源。
esp8266:
board: esp01_1m
restore_from_flash: true
# Enable logging
logger:
level: DEBUG #WARN
# Enable Home Assistant API
api:
encryption:
key: "tG/74gVnhFgdPM8Pm8fTWtr/P+smTsONRX/zlcHtx4g="
ota:
reboot_timeout: 8min
password: "fe26521716b51c2cbb161336ac8eb0eb"
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Wifis3 Fallback Hotspot"
password: "1prrZACu5khe"
captive_portal:
substitutions:
device_name: S3_1
button_pin: GPIO13 #按钮。插线板上的电源按键
relay_pin: GPIO5 #继电器。控制电源输出
led_r_pin: GPIO4 #故障指示灯(红色)
led_w_pin: GPIO16 #插线板状态灯。缓慢闪烁:WIFI连接中断或传感器读取失败;快速闪烁:ESPHome在设置时发现错误
alarm_pin1: GPIO12 #零火接反检测。零线地线之间是否有电压
alarm_pin2: GPIO14 #缺地线检测。地线和火线之间是否有电压
hlw8012_sel_pin: GPIO2 #HLW8012传感器sel引脚
hlw8012_sf_pin: GPIO9 #HLW8012传感器sf引脚
hlw8012_sf1_pin: GPIO10 #HLW8012传感器sf1引脚
globals:
- id: Security_lock_enable #安全锁启用。1为启用,0为禁用
type: int
restore_value: yes
initial_value: "0"
- id: Disconnection_reboot_enable #检测到断网(互联网)重启电源;1为启用,0为禁用
restore_value: yes
type: int
initial_value: "0"
- id: Disconnection_rebooted_mark #断网重启标志,1为已重启,0为未重启。断网后只重启一次,如果网络恢复将重置该标志
type: int
initial_value: "0"
- id: Warning_light_enable #故障指示灯启用。1为启用,0为禁用
type: int
restore_value: yes
initial_value: "1"
status_led:
pin:
number: $led_w_pin
inverted: true
output:
- id: light_output
platform: gpio
pin:
number: $led_r_pin
inverted: true
# 故障指示灯
light:
- platform: binary
output: light_output
name: "${device_name}_Warning_light"
internal: True # 不在前端显示
id: "r_led"
# 二元传感器组件
binary_sensor:
# 插线板电源按键
- platform: gpio
pin:
number: $button_pin
inverted: true
mode:
input: true
pullup: true
name: "${device_name}_Outlet button"
internal: True # 不在前端显示
on_click:
- min_length: 50ms
max_length: 1000ms
then:
- if:
condition:
lambda: 'return id(Security_lock_enable)==0 ;'
then:
- switch.toggle: "relay1"
# 故障检测(零火反接)
- platform: gpio
device_class: PROBLEM
entity_category: DIAGNOSTIC
pin:
number: $alarm_pin1
inverted: true
mode:
input: true
pullup: true
name: "${device_name}_LNInversion"
id: lostGnd
# 故障检测(缺地线)
- platform: gpio
device_class: PROBLEM
entity_category: DIAGNOSTIC
pin:
number: $alarm_pin2
inverted: true
mode:
input: true
pullup: true
name: "${device_name}_lostGnd"
filters:
- delayed_on: 100ms
- delayed_off: 100ms
- delayed_on_off: 100ms #延迟开关:仅当二进制传感器至少在指定的时间段内保持在相同状态时,才发送打开或关闭值。用于解除二进制开关的抖动
id: LNInversion
# hlw8012组件
sensor:
- platform: hlw8012
model: BL0937
sel_pin:
number: $hlw8012_sel_pin
inverted: true
cf_pin: $hlw8012_sf_pin
cf1_pin: $hlw8012_sf1_pin
#电流
current:
name: "${device_name}_current"
entity_category: diagnostic
#电压
voltage:
name: "${device_name}_voltage"
entity_category: diagnostic
#功率
power:
name: "${device_name}_power"
entity_category: diagnostic
#电量
energy:
name: "${device_name}_energy"
entity_category: diagnostic
initial_mode: CURRENT
update_interval: 30s
voltage_divider: 1066
current_resistor: 0.001
button:
# - platform: factory_reset
# name: "${device_name}_Reset"
# - platform: safe_mode
# name: "${device_name}_SafeMode"
# - platform: restart
# name: "Living Room Restart"
# 延时重启电源
- platform: template
name: "${device_name}_Delay reboot power"
icon: mdi:gesture-tap-button
on_press:
then:
- switch.turn_off: relay1
- delay: !lambda "if (id(delay_time).state) return id(delay_time).state*1000*60; else return 1*1000960;"
- switch.turn_on: relay1
# 延时设置
number:
- platform: template
icon: mdi:timer-settings
name: "${device_name}_Delay time"
mode: box
entity_category: config
unit_of_measurement: "分钟"
optimistic: true
min_value: 1
max_value: 60
restore_value: True
initial_value: 1
step: 1
id: delay_time
# 开关组件
switch:
# 继电器
- platform: gpio
pin: $relay_pin
name: "${device_name}_relay"
id: "relay1"
device_class: OUTLET
restore_mode: RESTORE_DEFAULT_ON # 尝试恢复状态并默认为 ON。
# 故障灯启用开关
- platform: template
name: "${device_name}_Warning light enable"
icon: mdi:lightbulb
entity_category: config
lambda: |-
if (id(Warning_light_enable)==1) {
return true;
} else {
return false;
}
id: "Warning_light_enable_switch"
turn_on_action:
- lambda: |-
id(Warning_light_enable)=1;
turn_off_action:
- lambda: |-
id(Warning_light_enable)=0;
# 检测到断网重启电源
- platform: template
name: "${device_name}_Disconnection reboot"
icon: mdi:network-off-outline
entity_category: config
lambda: |-
if (id(Disconnection_reboot_enable)==1) {
return true;
} else {
return false;
}
id: "Disconnection_reboot_enable_switch"
turn_on_action:
- lambda: |-
id(Disconnection_reboot_enable)=1;
id(network_status_text).publish_state("准备检测");
turn_off_action:
- lambda: |-
id(Disconnection_reboot_enable)=0;
id(network_status_text).publish_state("检测关闭");
# 安全锁开关,开启时,插排上的开关失效
lock:
- platform: template
name: "${device_name}_Security lock"
entity_category: CONFIG
id: "lock1"
lock_action:
then:
- lambda: |-
id(Security_lock_enable)=1;
unlock_action:
then:
- lambda: |-
id(Security_lock_enable)=0;
lambda: |-
if (id(Security_lock_enable)==1) {
return LOCK_STATE_LOCKED;
} else {
return LOCK_STATE_UNLOCKED;
}
http_request:
useragent: esphome/device
timeout: 10s
# 故障检测(互联网连接)
text_sensor:
- platform: template
name: "${device_name}_Disconnection"
update_interval: never
id: network_status_text
entity_category: diagnostic
interval:
# 每秒检测一次故障,有则亮红灯
- interval: 1s
then:
- lambda: |-
if(id(Warning_light_enable)==1){
if ( id(LNInversion).state or id(lostGnd).state) {
auto call =id(r_led).turn_on();
call.perform();
} else{
auto call =id(r_led).turn_off();
call.perform();
}
}
# 每分钟检测一次网络,无则关闭电源输出5分钟后恢复
- interval: 1min
then:
- if:
condition:
lambda: 'return (id(Disconnection_reboot_enable)==1)and(id(Disconnection_rebooted_mark)==0) ;'
then:
- lambda: id(network_status_text).publish_state("检测网络");
- http_request.get:
url: https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su
headers:
Content-Type: application/json
verify_ssl: false
on_response:
then:
- if:
condition:
lambda: 'return (status_code>399) ;'
then:
#lambda: 'id(network_status_text).publish_state("联网失败");'
- switch.turn_off: relay1
- delay: !lambda "if (id(delay_time).state) return id(delay_time).state*1000*60; else return 1*1000960;"
- switch.turn_off: relay1
else:
lambda: 'id(network_status_text).publish_state("联网正常");'
|