esphome 如何为带有集成 4 继电器的模块 esp01 编写配置

esphome how write config for module esp01 with integrated 4 relay

我从 aliexpress 得到了 4 relay module with esp01,但它不通过引脚切换继电器。

如何用esphome控制这个继电器模块?

在家庭助理社区找到答案 通过 uart 进行继电器控制,带有一些数据并需要禁用记录器: 这是 esphome 4 中继 esp01 模块的 yaml 格式的配置

# Enable logging
    logger:
      baud_rate: 0 #need this to free up UART pins
    
    uart:
      baud_rate: 115200 # speed to STC15L101EW
      tx_pin: GPIO1
      rx_pin: GPIO3
    
    switch:
      - platform: uart
        name: "A1on"
        data: [0xA0, 0x01, 0x01, 0xA2]
    
      - platform: uart
        name: "A1off"
        data: [0xA0, 0x01, 0x00, 0xA1]
    
      - platform: uart
        name: "A2on"
        data: [0xA0, 0x02, 0x01, 0xA3]
    
      - platform: uart
        name: "A2off"
        data: [0xA0, 0x02, 0x00, 0xA2]
    
      - platform: uart
        name: "A3on"
        data: [0xA0, 0x03, 0x01, 0xA4]
    
      - platform: uart
        name: "A3off"
        data: [0xA0, 0x03, 0x00, 0xA3]
    
      - platform: uart
        name: "A4on"
        data: [0xA0, 0x04, 0x01, 0xA5]
    
      - platform: uart
        name: "A4off"
        data: [0xA0, 0x04, 0x00, 0xA4]

之后我们得到了 8 个开关,分别打开和关闭每个继电器(每个继电器 2 个)

# Enable logging
logger:
  baud_rate: 0 #need this to free up UART pins
uart:
  baud_rate: 9600 # speed to STC15L101EW
  tx_pin: 1
  rx_pin: 3

globals:
   - id: on_flag_1
     type: int
     restore_value: no
     initial_value: '0'
   - id: on_flag_2
     type: int
     restore_value: no
     initial_value: '0'
   - id: on_flag_3
     type: int
     restore_value: no
     initial_value: '0'
   - id: on_flag_4
     type: int
     restore_value: no
     initial_value: '0'

switch:
  - platform: template 
    id: relay1
    name: "Relay #1"
    lambda: 
      return id(on_flag_1);
    turn_on_action:
      - uart.write: [0xA0, 0x01, 0x01, 0xA2]
      - globals.set:
          id: on_flag_1
          value: '1'
    turn_off_action:
      - uart.write: [0xA0, 0x01, 0x00, 0xA1]
      - globals.set:
          id: on_flag_1
          value: '0'
  - platform: template 
    id: relay2
    name: "Relay #2"
    lambda: 
      return id(on_flag_2);
    turn_on_action:
      - uart.write: [0xA0, 0x02, 0x01, 0xA3]
      - globals.set:
          id: on_flag_2
          value: '1'
    turn_off_action:
      - uart.write: [0xA0, 0x02, 0x00, 0xA2]
      - globals.set:
          id: on_flag_2
          value: '0'
  - platform: template 
    id: relay3
    name: "Relay #3"
    lambda: 
      return id(on_flag_3);
    turn_on_action:
      - uart.write: [0xA0, 0x03, 0x01, 0xA4]
      - globals.set:
          id: on_flag_3
          value: '1'
    turn_off_action:
      - uart.write: [0xA0, 0x03, 0x00, 0xA3]
      - globals.set:
          id: on_flag_3
          value: '0'
  - platform: template 
    id: relay4
    name: "Relay #4"
    lambda: 
      return id(on_flag_4);
    turn_on_action:
      - uart.write: [0xA0, 0x04, 0x01, 0xA5]
      - globals.set:
          id: on_flag_4
          value: '1'
    turn_off_action:
      - uart.write: [0xA0, 0x04, 0x00, 0xA4]
      - globals.set:
          id: on_flag_4
          value: '0'

我有一个单曲,但根据我的单曲的工作原理对其进行了扩展,这将产生 4 个可以切换的开关 on/off。