本帖最后由 ylilike 于 2022-11-12 06:15 编辑
早期入坑的一款继电器,看有人需要,分享一下固件
继电器开关命令都相同,
除了一路继电器是波特率9600,
其余都是波特率115200。
单路
双路
四路
命令
#################################################################################
substitutions: { desc: "车库门控制", device_name: esp01_relay4 }
#################################################################################
esphome:
name: $device_name
platform: ESP8266
board: esp01_1m
on_boot:
priority: 600
then:
- lambda: |-
id(ctrl_text).publish_state("若然,一切若然~");
wifi:
ssid: !secret wifi_4 # wifi_aifm # wifi_ssid
password: !secret wifi_password_4
reboot_timeout: 0s
manual_ip:
static_ip: 192.168.3.42
gateway: 192.168.3.254
subnet: 255.255.255.0
dns1: 192.168.3.254
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "${device_name}_hotspot"
password: !secret fallback_hotspot_pass
captive_portal:
logger:
level: error # VERBOSE #makes uart stream available in esphome logstream
baud_rate: 0 #disable logging over uart
# api:
# password: !secret api_password
mqtt:
broker: !secret mqtt_broker # 'bemfa.com'
port: !secret mqtt_port
username: !secret mqtt_username
password: !secret mqtt_password
discovery: true
reboot_timeout: 0s
on_message:
- topic: esp01_relay4
then:
- lambda: |-
if (x == "door_up"){
id(open_the_door).execute();
id(ctrl_text).publish_state("the door is Up...");
}
else if (x == "door_pause"){
id(door_pause).turn_on();
id(ctrl_text).publish_state("the door is Pause!");
}
else if (x == "door_down"){
id(door_down).turn_on();
id(ctrl_text).publish_state("the door is Down...");
}
else if (x == "on_1"){
id(open_the_door).execute();
id(my_count)=0;
id(count_flag) = 1;
id(ctrl_text).publish_state("on %10 ...");
}
else if (x == "on_2"){
id(open_the_door).execute();
id(my_count)=0;
id(count_flag) = 2;
id(ctrl_text).publish_state("on 30% ...");
}
else if (x == "on_3"){
id(open_the_door).execute();
id(my_count)=0;
id(count_flag) = 3;
id(ctrl_text).publish_state("on 60% ...");
}
ota:
password: !secret ota_password
web_server:
port: 80
script:
- id: open_the_door
mode: single
then:
- switch.turn_on: door_pause
- delay: 333ms
- switch.turn_on: door_up
text_sensor:
- platform: template
name: "Tag"
id: ctrl_text
update_interval: never
- platform: wifi_info
ip_address:
name: ${device_name}_IP
mac_address:
name: ${device_name}_MAC
globals:
- id: my_count #
type: int
restore_value: no
initial_value: '0'
- id: count_flag #
type: int
restore_value: no
initial_value: '0'
interval:
- interval: 1s #定时检查my_count
then:
- lambda: |-
if(id(count_flag) > 0 ){
if(id(my_count)> 99 ){
id(my_count)=0;
}
id(my_count)+=1;
if(id(count_flag) == 1 ){
if(id(my_count)>3 ){
id(count_flag)=0;
id(my_count)=0;
id(door_pause).turn_on();
}
}
if(id(count_flag) == 2 ){
if(id(my_count)>9 ){
id(count_flag)=0;
id(my_count)=0;
id(door_pause).turn_on();
}
}
if(id(count_flag) == 3 ){
if(id(my_count)>15 ){
id(count_flag)=0;
id(my_count)=0;
id(door_pause).turn_on();
}
}
}
uart:
tx_pin: GPIO01
baud_rate: 115200
dallas:
- pin: 0
sensor:
- platform: uptime
id: uptime_s
update_interval: 60s
- platform: wifi_signal
name: "${device_name}_WiFi_Signal"
update_interval: 120s
- platform: dallas
address: 0xC53C01B556BD8E28
name: "${device_name}_temperature"
binary_sensor:
- platform: gpio
pin:
number: 3
mode: INPUT_PULLUP
inverted: True
id: door_status
name: "${device_name}_door"
device_class: garage_door
filters:
- delayed_on_off: 20 ms
on_release:
- light.turn_off: cr_light
output:
- platform: template
type: binary
id: out_1
write_action:
if:
condition:
light.is_on: cr_light
then:
- switch.turn_on: relay_1
else:
- switch.turn_off: relay_1
light:
- platform: binary
name: "${device_name}_light"
id: cr_light
output: out_1
switch:
- platform: restart
name: "${device_name}_Restart"
- platform: template
id: relay_1
turn_on_action:
- uart.write: [0xA0, 0x01, 0x01, 0xA2]
turn_off_action:
- uart.write: [0xA0, 0x01, 0x00, 0xA1]
optimistic: true
- platform: template
# name: door_down # 不显示名称,仅内部调用。因为是复位 脉冲信号
id: door_down
turn_on_action:
- uart.write: [0xA0, 0x02, 0x01, 0xA3]
- delay: 166 ms
- uart.write: [0xA0, 0x02, 0x00, 0xA2]
# optimistic: true
- platform: template
# name: door_pause # 不显示名称,仅内部调用。因为是复位 脉冲信号
id: door_pause
turn_on_action:
- uart.write: [0xA0, 0x03, 0x01, 0xA4]
- delay: 166 ms
- uart.write: [0xA0, 0x03, 0x00, 0xA3]
# optimistic: true
- platform: template
# name: door_up # 不显示名称,仅内部调用。因为是复位 脉冲信号
id: door_up
turn_on_action:
- uart.write: [0xA0, 0x04, 0x01, 0xA5]
- delay: 166 ms
- uart.write: [0xA0, 0x04, 0x00, 0xA4]
# optimistic: true
# cover:
# - platform: template
# id: crd_st
# name: "车库门"
# lambda: |-
# if (id(door_status).state) {
# return COVER_OPEN;
# } else {
# return COVER_CLOSED;
# }
# open_action:
# - switch.turn_on: door_up
# close_action:
# - switch.turn_on: door_down
# stop_action:
# - switch.turn_on: door_pause
# optimistic: true
# #assumed_state: true
订阅主题esp01_relay4,命令door_up………详见程序
当然你需要在ha中显示开关,把name注释删掉,不想mqtt就改回api,随你折腾了。
手机发帖,细节勿喷!
|