我要是发到博客或者专栏就当个学习笔记吧,如果有时间再整理成教程的格式。

Mac(类Unix)上 ESPHome安装

ESPHome --user 安装位置:/Users/gakki/Library/Python/3.9/bin

使用时需要添加环境变量

export PATH="$PATH:/Users/gakki/Library/Python/3.9/bin"

硬件输出PWM控制舵机

案例:接入ESPHome用ESP8266控制舵机喂狗:https://bbs.hassbian.com/thread-8741-1-1.html

esphome:
  name: servos
  platform: ESP8266
  board: nodemcuv2

wifi:
  ssid: "blue_sky"
  password: "720805zzy"
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  #ap:
    #ssid: "Servos Fallback Hotspot"
    #password: "VYTOwxqxxlOT"

#captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:
    password: "123"
  services:
    - service: control_servo
      variables:
        level: float
      then:
        - servo.write:
            id: my_servo
            level: !lambda 'return level / 100.0;'

ota:
  password: "123"
binary_sensor:
  - platform: gpio
    pin: D4
    name: "手动喂狗"
    on_state:
      then:
      - servo.write:
          id: my_servo
          level: 100.0%
      - delay: 2s
      - servo.write:
          id: my_servo
          level: 0%      
      
servo:
  - id: my_servo
    output: pwm_output


# Example output platform
# On ESP32, use ledc output
output:
  - platform: esp8266_pwm
    id: pwm_output
    pin: D5
    frequency: 50 Hz

软件模拟PWM

案例:ESP8266/ESP01 软件输出PWM二极管调光(Arduino):https://www.cnblogs.com/cuianbing/p/14409053.html

LED 测试项目:

esphome:
  name: node_mcu
  platform: ESP8266
  board: nodemcuv2

wifi:
  ssid: "blue_sky"
  password: "720805zzy"

  # Enable fallback hotspot (captive portal) in case wifi connection fails




# Enable logging
logger:

# Enable Home Assistant API
api:
  password: "123"

ota:
  password: "123"
  
output:
  - platform: esp8266_pwm
    pin: GPIO2
    frequency: 200Hz
    id: led_onboard
    inverted: True
    
light:
  - platform: monochromatic
    name: "LED On Board"
    output: led_onboard

ESPHOME 舵机

# Example configuration entry
servo:
  - id: my_servo
    output: pwm_output

# Example output platform
# On ESP32, use ledc output
output:
  - platform: esp8266_pwm
    id: pwm_output
    pin: D1
    frequency: 50 Hz

用舵机控制墙壁开关

最终烧录的yaml:

esphome:
  name: node_mcu
  platform: ESP8266
  board: nodemcuv2

wifi:
  ssid: "blue_sky"
  password: "720805zzy"
  power_save_mode: light
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  #ap:
  #ssid: "Servos Fallback Hotspot"
  #password: "VYTOwxqxxlOT"

#captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:
  services:
    - service: control_servo
      variables:
        level: float
      then:
        - servo.write:
            id: my_servo
            level: !lambda 'return level / 100.0;'

ota:
  password: "123"

servo:
  - id: my_servo
    output: pwm_output

# Example output platform
# On ESP32, use ledc output
output:
  - platform: esp8266_pwm
    id: pwm_output
    pin: D4
    frequency: 50 Hz
    
light:
  - platform: monochromatic
    name: "LED On Board"
    output: pwm_output

继电器加普通开关

通过ESP01S控制继电器,并通过普通开关的输入控制通断。

要写判断语句,分为有标识模式无标识双控模式

B站的实例1

将普通开关接到GPIO2Vcc之间。

定义了几个系统变量:

  • 继电器状态标识YYXBC_HIGHYYXBC_LOW (以后用LED_ONLED_OFF代替)
  • 外部IO位置,物理开关与继电器
  • 物理开关工作模式
/*1,继电器高电平触发时,YYXBC_HIGH = 1,YYXBC_LOW  = 0
继电器低电平触发时,YYXBC_HIGH = 0,YYXBC_LOW  = 1 */
const int YYXBC_HIGH = 0 ;    //继电器是低电平触发
const int YYXBC_LOW  = 1 ;
//2,用esp-01时,物理开关接在vcc 和gpio2上,继电器接在gpio0上
//NodeMCU 继电器接D3 GPIO0,物理开关接D4 GPIO2
#define LED_BUILTIN_LIGHT 0
#define LED_BUILTIN_K2 2

/*3,物理开关点动模式1,自锁模式0*/
const int YYXBC_BUTTON_TYPE = 1;

心跳回调:

没用,只是用于更新点灯科技上的按钮状态。

void heartbeat()
{
   BLINKER_LOG("heartbeat,state: ", digitalRead(LED_BUILTIN));

    //较正app的按钮状态
    if(YYXBC_HIGH == digitalRead(LED_BUILTIN_LIGHT) ){
        Button1.print("on");
    }else{
       Button1.print("off");
    }
}

开关响应:

//点动模式按钮,监听按钮状态,执行相应处理
void btnHandler1()
{
  static bool oButtonState = false;
  int state1 =  digitalRead(LED_BUILTIN_K2); //按钮状态
  int state2 =  digitalRead(LED_BUILTIN_LIGHT); //灯的状态
  if(state1 == HIGH )
  {
    if(oButtonState ){
      if(state2 == YYXBC_HIGH )
      { 
        button1_callback(BLINKER_CMD_OFF);
        Serial.println("按钮对灯已执行关闭");
      }else{
        button1_callback(BLINKER_CMD_ON);
        Serial.println("按钮对灯已执行打开");
      }
      oButtonState = false;
    }
  }else{
    oButtonState = true;
  }
}



//自锁模式按钮,监听按钮状态,执行相应处理
void btnHandler2()
{
 static bool is_btn = false;//按钮的标志位,用来逻辑处理对比,判断按钮有没有改变状态
  bool is = digitalRead(LED_BUILTIN_K2);   //按钮状态
  if ( is != is_btn)
  {
    bool is_led = digitalRead(LED_BUILTIN_LIGHT);
    digitalWrite(LED_BUILTIN_LIGHT, !is_led);
    if (is_led == YYXBC_HIGH)
    {
      button1_callback(BLINKER_CMD_OFF);
      Serial.println("按钮对灯已执行关闭");
    }
    else
    {
      button1_callback(BLINKER_CMD_ON);
      Serial.println("按钮对灯已执行打开");
    }
    is_btn = digitalRead(LED_BUILTIN_K2);  //更新按钮状态
  }

CSDN实例2

通过ESPHome接入HA,实现与之前相近的功能,即使用Wi-Fi和实体开关控制继电器。

大体的原理是:一个gpio虚拟成二进制传感器,一个gpio来控制继电器。当这个二进制传感器触发的时候,开关的开与闭合也发生变化。

YAML文件:

esphome:
  name: node_mcu
  platform: ESP8266
  board: nodemcuv2
 
wifi:
  networks:
    ssid: "blue_sky"  #改成你的wifi名称
    password: "720805zzy" #改成你的wifi密码
  #manual_ip:
    #static_ip: 192.168.50.188
    #gateway: 192.168.50.1
    #subnet: 255.255.255.0
 
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "WS05 Fallback Hotspot" #当esp01s连不上网的时候,它会自动发出热点。
    password: "12345678"
 
captive_portal:
 
# Enable logging
logger:
 
# Enable Home Assistant API
api:
#  password: "1"
 
ota:
  password: "123"
  
web_server:
  port: 80
  
switch:
  - platform: gpio
    pin: GPIO0
    name: "wireless05_switch"
    id: relay2
    
binary_sensor:
  - platform: gpio
    pin: GPIO2
    name: "press_switch05"
    device_class: opening
    filters:
      - delayed_on_off: 100ms #这里一定要加这个这个起到滤波的作用。
    on_press:  #当这个二进制传感器触发的时候,触发gpio0
      then:
        - switch.toggle: relay2

我烧录的YAML

esphome:
  name: node_mcu
  platform: ESP8266
  board: nodemcuv2
 
wifi:
  networks:
    ssid: "blue_sky"  #改成你的wifi名称
    password: "720805zzy" #改成你的wifi密码
  power_save_mode: light
  #manual_ip:
    #static_ip: 192.168.50.188
    #gateway: 192.168.50.1
    #subnet: 255.255.255.0
 
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Library_Light_Hotspot" #当esp01s连不上网的时候,它会自动发出热点。
    password: "12345678"
 
captive_portal:
 
# Enable logging
logger:
 
# Enable Home Assistant API
api:
#  password: "1"
 
ota:
  password: "123"
  
web_server:
  port: 80
  
switch:
  - platform: gpio
    pin: GPIO0
    name: "书房灯"
    id: relay2
    inverted: True
    
binary_sensor:
  - platform: gpio
    pin: GPIO2
    name: "press_switch_lib"
    device_class: opening
    filters:
      - delayed_on_off: 100ms #这里一定要加这个这个起到滤波的作用。
    on_state:  #当这个二进制传感器状态改变的时候,触发gpio0
      then:
        - switch.toggle: relay2

附录

ESPHome Wi-Fi 使用省电模式:

wifi:
  # ...
  power_save_mode: none

参考资料


  1. ESP-01+继电器,自己动手制作智能开关,添加物理开关控制.https://www.bilibili.com/video/av501928405/
  2. 树莓派智能家居homeassistant之用esphome制作零火智能开关篇. https://blog.csdn.net/zuobianfy/article/details/118432775