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

 找回密码
 立即注册
查看: 2526|回复: 17

[经验分享] 某宝esp01串口继电器1路、2路、4路通用ESPhome固件分享

[复制链接]

13

主题

405

帖子

2453

积分

金牌会员

Rank: 6Rank: 6

积分
2453
金钱
2048
HASS币
0
发表于 2022-11-12 05:53:43 | 显示全部楼层 |阅读模式
本帖最后由 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,随你折腾了。

手机发帖,细节勿喷!



评分

参与人数 2金钱 +18 收起 理由
SilverWolf + 8 感谢楼主分享!
dscao + 10 感谢楼主分享!

查看全部评分

若然,一切若然~
回复

使用道具 举报

13

主题

405

帖子

2453

积分

金牌会员

Rank: 6Rank: 6

积分
2453
金钱
2048
HASS币
0
 楼主| 发表于 2022-11-12 05:57:55 | 显示全部楼层
我是拿来控制车库门的,加了温度和开启百分比
卡片如图
Screenshot_20221112-055433.jpg

评分

参与人数 1金钱 +5 收起 理由
book123 + 5 论坛有你更精彩!

查看全部评分

若然,一切若然~
回复

使用道具 举报

3

主题

148

帖子

1468

积分

金牌会员

Rank: 6Rank: 6

积分
1468
金钱
1320
HASS币
10
发表于 2022-11-12 06:08:14 | 显示全部楼层
我用esp-12f做的8路继电器也是用光耦驱动,偶尔会抖动下  整个继电器都快速动作下又恢复   不知道为啥,望大佬告知
回复

使用道具 举报

13

主题

405

帖子

2453

积分

金牌会员

Rank: 6Rank: 6

积分
2453
金钱
2048
HASS币
0
 楼主| 发表于 2022-11-12 06:19:48 | 显示全部楼层
yc0209 发表于 2022-11-12 06:08
我用esp-12f做的8路继电器也是用光耦驱动,偶尔会抖动下  整个继电器都快速动作下又恢复   不知道为啥,望 ...

你也是用了单片机?串口通讯?
若然,一切若然~
回复

使用道具 举报

37

主题

271

帖子

2161

积分

论坛DIY达人

积分
2161
金钱
1890
HASS币
20
发表于 2022-11-12 08:49:28 | 显示全部楼层
yc0209 发表于 2022-11-12 06:08
我用esp-12f做的8路继电器也是用光耦驱动,偶尔会抖动下  整个继电器都快速动作下又恢复   不知道为啥,望 ...

有几个gpio口就那样,避开就行了,上电瞬间高电平
回复

使用道具 举报

14

主题

250

帖子

2002

积分

金牌会员

Rank: 6Rank: 6

积分
2002
金钱
1747
HASS币
50
发表于 2022-11-12 10:39:54 | 显示全部楼层
谢谢分享!!
回复

使用道具 举报

0

主题

116

帖子

1833

积分

金牌会员

Rank: 6Rank: 6

积分
1833
金钱
1717
HASS币
0
发表于 2022-11-16 16:15:39 | 显示全部楼层

谢谢分享!!
回复

使用道具 举报

3

主题

46

帖子

270

积分

中级会员

Rank: 3Rank: 3

积分
270
金钱
224
HASS币
0
发表于 2023-1-9 16:18:33 来自手机 | 显示全部楼层
不是串口板,四个继电器接四根电平触发进来,也不是搞车库门,接四个电磁阀,没看懂太复杂了不会改,尤其是switch,不知道8266能用哪四个端子做高电平触发!控制逻辑要在ha里采集温度做,请帮忙敲代码,就是switch里定义吧?状态如何掌握?
回复

使用道具 举报

0

主题

562

帖子

2200

积分

金牌会员

Rank: 6Rank: 6

积分
2200
金钱
1638
HASS币
0
发表于 2023-1-10 13:19:34 | 显示全部楼层
楼主,车库门开启百分比是怎么设置的
回复

使用道具 举报

13

主题

405

帖子

2453

积分

金牌会员

Rank: 6Rank: 6

积分
2453
金钱
2048
HASS币
0
 楼主| 发表于 2023-1-20 17:33:31 | 显示全部楼层
Ari 发表于 2023-1-10 13:19
楼主,车库门开启百分比是怎么设置的

源码中 on_1  on_2 on_3就是按时间,开启百分比
若然,一切若然~
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-5-10 01:01 , Processed in 0.385535 second(s), 35 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表