本帖最后由 sorrypqa 于 2023-12-7 08:02 编辑
买了个佛山野鸡厂生产的伪日货风扇,遥控非常难用,按几次都没反应,想改ESP32来控制,以为是PWM调速,谁知测了一下1、2、3档控制频率分别是88Hz、180Hz、216Hz,也就是CLK调速,看了半天ESPHome的风扇组件,好像只有PWM没有CLK变频,有大佬知道如何写配置,从慢到快调速时让电机控制信号引脚输出50Hz-216Hz的变频信号吗?本来想弄个主控板直接换,增加光线传感器、温湿度传感器、LD2420雷达模块、433M电红外传感器接收模块,按键和指示灯的布局都定位好了,现在看来没戏唱.......
看着天书般的官方文档,并参考论坛里各位大神的风扇代码,折腾了2天,弄了个下面的配置,通过模板开关触发来输出不同频率,HA里可以10档调速,但这个风扇摇头电机需要H桥提供10KHz的正反交替变换电流驱动,如何写法才能让32、33这两个引脚提供这种信号?赐教一下,谢谢!
打板文件:
Gerber_PCB_无刷变频风扇控制板.zip
(44.42 KB, 下载次数: 3)
这块板子等得好辛苦:
substitutions:
device_name: esp32-fan-02 #设备名称
friendly_name: esp32_fan_02
button1_pin: GPIO0 #睡眠按键
button2_pin: GPIO5 #开关机按键
button3_pin: GPIO34 #变速按键
button4_pin: GPIO35 #摇头按键
led_state_pin: GPIO2 #状态指示灯
fan_pin: GPIO26 #风扇调速
osc1_pin: GPIO32 #同步电机1
osc2_pin: GPIO33 #同步电机2
speed1_led_pin: GPIO17 #速度1指示灯
speed2_led_pin: GPIO16 #速度2指示灯
speed3_led_pin: GPIO4 #速度3指示灯
power_led_pin: GPIO19 #风扇工作指示灯
time_led_pin: GPIO13 #定时启动指示灯
sleep_led_pin: GPIO15 #睡眠模式指示灯
osc_led_pin: GPIO12 #睡眠模式指示灯
i2c_sda1_pin: GPIO22 #aht10用i2c引脚
i2c_scl1_pin: GPIO23 #aht10用i2c引脚
i2c_sda2_pin: GPIO14 #bh1750用i2c引脚
i2c_scl2_pin: GPIO27 #bh1750用i2c引脚
pir_pin: GPIO21 #热释电红外传感器用引脚
Radar_pin: GPIO18 #雷达用引脚
esphome:
name: $device_name
comment: 变频风扇。ESP32主控、带温度、光照、红外传感器。
esp32:
board: esp32dev
framework:
type: arduino
preferences:
flash_write_interval: 5min #将数据刷新到闪存的频率。此设置有助于防止对组件的快速更改快速写入闪存并磨损它。默认值为1min
api:
encryption: #启用API层的传输加密
key: !secret api_key #用于加密的预共享密钥
reboot_timeout: 0s #与客户端(如HA)失去连接多久后重启设备,秒s/分钟min/小时h,不需要此功能设置为0s
ota:
password: !secret ota_password
captive_portal:
web_server:
port: 80
auth:
username: !secret web_name
password: !secret web_password
wifi:
networks:
- ssid: !secret wifi_ssid
password: !secret wifi_password
- ssid: !secret wifi_ssid2
password: !secret wifi_password2
- ssid: !secret wifi_ssid3
password: !secret wifi_password3
- ssid: !secret wifi_ssid4
password: !secret wifi_password4
# fast_connect: true #快速连接,如果SSID是隐藏的,必须设置为true
reboot_timeout: 0s #WiFi离线多久后重启设备,秒s/分钟min/小时h,不需要此功能设置为0s
# use_address: esp32-fan-02.local #更新设备名称时,需要指定设备旧地址。
# manual_ip:
# static_ip: 192.168.66.163
# gateway: 192.168.66.1
# subnet: 255.255.255.0
# dns1: 114.114.114.114
# dns2: 180.76.76.76
# ap:
# ssid: $friendly_name
# password: !secret ap_password
status_led:
pin:
number: $led_state_pin
inverted: true #反转属性
logger:
baud_rate: 0
logs:
text_sensor: ERROR
i2c:
- id: bh1750_i2c
sda: $i2c_sda2_pin
scl: $i2c_scl2_pin
scan: true #启动时搜索I²C地址空间
- id: aht10_i2c
sda: $i2c_sda1_pin
scl: $i2c_scl1_pin
scan: true #启动时搜索I²C地址空间
globals: #全局变量
- id: oscillation_state
type: bool
restore_value: no
initial_value: 'false'
- id: countdown_d_duration_value
type: int
- id: countdown_d_remaining_time
type: int
- id: sequence_script_running
type: bool
restore_value: no
initial_value: 'false'
fan: #速度风扇组件
- platform: speed
id: Variable_speed_fan
output: speed_fan_template
name: "${friendly_name}_fan"
speed_count: 10 #设置速度级别的数量,该值用于计算每个速度的百分比,同时允许输出中的增量为 1%
restore_mode: ALWAYS_OFF #启动时将风扇初始化为OFF。
oscillation_output: osc_output ##摇头电机
# direction_output: osc_template ##正反转开关可并在一起.在hk中显示在一起/out输出根据需要修改.本例子指向了同功
# on_speed_set: #每次更改风扇速度时激活此触发器
# - lambda: !lambda |-
# if (id(Variable_speed_fan).speed == 2) {
# }
on_turn_off: #关闭风扇时激活此触发器
- then:
- output.turn_off: power_led
- output.turn_off: speed1_led
- output.turn_off: speed2_led
- output.turn_off: speed3_led
- output.turn_off: time_led
- output.turn_off: sleep_led
- output.turn_off: osc_led
- lambda:
auto call = id(Variable_speed_fan).turn_off();
call.set_oscillating(false);
call.perform();
on_turn_on: #每次打开激活此触发器
then:
- output.turn_on: power_led
# - lambda: !lambda |-
output:
- platform: ledc
pin: $fan_pin
id: fan_out
# channel: 1
- platform: gpio #摇头电机输出
id: osc_output
pin: $osc2_pin
inverted: true #反转属性
- platform: gpio #速度指示灯1
id: speed1_led
pin: $speed1_led_pin
- platform: gpio #速度指示灯2
id: speed2_led
pin: $speed2_led_pin
- platform: gpio #速度指示灯3
id: speed3_led
pin: $speed3_led_pin
- platform: gpio #风扇工作指示灯
id: power_led
pin: $power_led_pin
- platform: gpio #定时启动指示灯
id: time_led
pin: $time_led_pin
- platform: gpio #睡眠模式指示灯
id: sleep_led
pin: $sleep_led_pin
- platform: gpio #摇头指示灯
id: osc_led
pin: $osc_led_pin
- platform: template #模板输出
id: speed_fan_template
type: float #输出的类型
write_action: #在更新输出状态时要执行的自动化。
- if:
condition:
lambda: return ((state == 0)); #调节位置比例
then:
- output.turn_off: fan_out
- output.turn_off: speed1_led
- output.turn_off: speed2_led
- output.turn_off: speed3_led
- if:
condition:
lambda: return ((state > 0) && (state < 0.1));
then:
- output.turn_on: fan_out
- output.turn_on: speed1_led
- output.turn_off: speed2_led
- output.turn_off: speed3_led
- output.ledc.set_frequency:
id: fan_out
frequency: "80Hz"
- output.set_level:
id: fan_out
level: "50%"
- if:
condition:
lambda: return ((state > 0.1) && (state < 0.2));
then:
- output.turn_on: fan_out
- output.turn_on: speed1_led
- output.turn_off: speed2_led
- output.turn_off: speed3_led
- output.ledc.set_frequency:
id: fan_out
frequency: "100Hz"
- output.set_level:
id: fan_out
level: "50%"
- if:
condition:
lambda: return ((state > 0.2) && (state < 0.3));
then:
- output.turn_on: fan_out
- output.turn_off: speed1_led
- output.turn_off: speed3_led
- output.turn_on: speed2_led
- output.ledc.set_frequency:
id: fan_out
frequency: "120Hz"
- output.set_level:
id: fan_out
level: "50%"
- if:
condition:
lambda: return ((state > 0.3) && (state < 0.4));
then:
- output.turn_on: fan_out
- output.turn_off: speed1_led
- output.turn_off: speed3_led
- output.turn_on: speed2_led
- output.ledc.set_frequency:
id: fan_out
frequency: "140Hz"
- output.set_level:
id: fan_out
level: "50%"
- if:
condition:
lambda: return ((state > 0.4) && (state < 0.5));
then:
- output.turn_on: fan_out
- output.turn_off: speed1_led
- output.turn_off: speed2_led
- output.turn_on: speed3_led
- output.ledc.set_frequency:
id: fan_out
frequency: "150Hz"
- output.set_level:
id: fan_out
level: "50%"
- if:
condition:
lambda: return ((state > 0.5) && (state < 0.6));
then:
- output.turn_on: fan_out
- output.turn_on: speed1_led
- output.turn_off: speed2_led
- output.turn_on: speed3_led
- output.ledc.set_frequency:
id: fan_out
frequency: "160Hz"
- output.set_level:
id: fan_out
level: "50%"
- if:
condition:
lambda: return ((state > 0.6) && (state < 0.7));
then:
- output.turn_on: fan_out
- output.turn_off: speed1_led
- output.turn_on: speed2_led
- output.turn_on: speed3_led
- output.ledc.set_frequency:
id: fan_out
frequency: "170Hz"
- output.set_level:
id: fan_out
level: "50%"
- if:
condition:
lambda: return ((state > 0.7) && (state < 0.8));
then:
- output.turn_on: fan_out
- output.turn_on: speed1_led
- output.turn_on: speed2_led
- output.turn_off: speed3_led
- output.ledc.set_frequency:
id: fan_out
frequency: "180Hz"
- output.set_level:
id: fan_out
level: "50%"
- if:
condition:
lambda: return ((state > 0.8) && (state < 0.9));
then:
- output.turn_on: fan_out
- output.turn_on: speed1_led
- output.turn_on: speed2_led
- output.turn_off: speed3_led
- output.ledc.set_frequency:
id: fan_out
frequency: "200Hz"
- output.set_level:
id: fan_out
level: "50%"
- if:
condition:
lambda: return ((state > 0.9));
then:
- output.turn_on: fan_out
- output.turn_on: speed1_led
- output.turn_on: speed2_led
- output.turn_on: speed3_led
- output.ledc.set_frequency:
id: fan_out
frequency: "220Hz"
- output.set_level:
id: fan_out
level: "50%"
switch:
- platform: restart #重启开关
name: "${friendly_name}_Restart"
icon: mdi:car-esp #自定义图标
- platform: safe_mode #安全模式
name: "${friendly_name}_SafeMode"
- platform: factory_reset #恢复出厂设置
name: ${friendly_name}_factory
#-------------------------计数器--------------------------
script:
- id: sequence_script
then:
- globals.set:
id: sequence_script_running
value: 'true'
- fan.turn_on:
id: Variable_speed_fan
speed: 3
- delay: 30min ###睡眠风换挡时间分钟
- fan.turn_on:
id: Variable_speed_fan
speed: 2
- delay: 30min ###睡眠风换挡时间分钟
- fan.turn_on:
id: Variable_speed_fan
speed: 1
- delay: 30min ###睡眠风换挡时间分钟
- fan.turn_off:
id: Variable_speed_fan
- globals.set:
id: sequence_script_running
value: 'false'
- id: countdown_timer_e_script
then:
- lambda: |-
id(countdown_d_remaining_time) = id(countdown_d_duration_value) * 60;
- fan.turn_on: Variable_speed_fan
- while:
condition:
lambda: 'return id(countdown_d_remaining_time) > 0;'
then:
- text_sensor.template.publish:
id: countdown_display_result
state: !lambda 'return to_string(id(countdown_d_remaining_time) / 60) + " 分钟 " + to_string(id(countdown_d_remaining_time) % 60) + " 秒后关闭风扇";'
- delay: 1s
- lambda: |-
id(countdown_d_remaining_time) -= 1;
- fan.turn_off: Variable_speed_fan
button:
#------------------------睡眠定时终止按钮--------------------------
- platform: template
name: "${friendly_name}_stop"
id: start_button
on_press:
then:
- script.stop: sequence_script ##停止睡眠模式
- script.stop: countdown_timer_e_script ###停止倒计时
- fan.turn_off: Variable_speed_fan ###关闭风扇
- output.turn_off: sleep_led
- globals.set:
id: sequence_script_running
value: 'false'
#------------------------睡眠模式启动按钮--------------------------
- platform: template
name: "睡眠风"
id: start_button3
icon: mdi:weather-night
on_press:
then:
- script.execute: sequence_script
- output.turn_on: sleep_led
#------------------------倒计时设置--------------------------
number:
- platform: template
name: "${friendly_name}_Set timing"
id: countdown_e_duration
min_value: 1 ##最小值
max_value: 120 ###最大值如超过步长的240倍将无法显示滑条
step: 1 ##步长
icon: "mdi:timer-outline" #自定义图标
lambda: |-
return id(countdown_d_duration_value);
set_action:
- script.stop: countdown_timer_e_script
- output.turn_on: time_led
- lambda: |-
id(countdown_d_duration_value) = x;
- script.execute: countdown_timer_e_script
#----------------------------------定时按键配置-------------------------------------------
binary_sensor:
- platform: gpio
pin:
number: $button1_pin
inverted: true #反转属性
mode:
input: true #将引脚配置为输入
pullup: true #激活引脚上的内部上拉电阻
name: "${friendly_name}_time"
id: "${friendly_name}_time"
internal: true #设为内部组件
on_press:
then:
- script.execute: sequence_script
- output.turn_on: sleep_led
#----------------------------------电源按键配置-------------------------------------------
- platform: gpio
pin:
number: $button2_pin
inverted: true #反转属性
mode:
input: true #将引脚配置为输入
pullup: true #激活引脚上的内部上拉电阻
name: "${friendly_name}_power"
id: "${friendly_name}_power"
internal: true #设为内部组件
on_press:
- fan.toggle: Variable_speed_fan #切换风扇开关状态
#----------------------------------调速按键配置-------------------------------------------
- platform: gpio
pin:
number: $button3_pin
inverted: true #反转属性
mode:
input: true #将引脚配置为输入
# pullup: true #激活引脚上的内部上拉电阻
name: "${friendly_name}_speed"
id: "${friendly_name}_speed"
internal: true #设为内部组件
on_press:
- fan.cycle_speed:
id: Variable_speed_fan #切换风扇速度
off_speed_cycle: false #确定风扇在循环达到最高速度后是否会循环关闭。可选true、false
#----------------------------------摇头按键配置-------------------------------------------
- platform: gpio
pin:
number: $button4_pin
inverted: true #反转属性
mode:
input: true #将引脚配置为输入
# pullup: true #激活引脚上的内部上拉电阻
name: "${friendly_name}_dsc" #摇头用按键
id: "${friendly_name}_dsc"
internal: true #设为内部组件
on_press:
- if:
condition:
- lambda: 'return id(Variable_speed_fan).state ;'
then:
- if:
condition:
- lambda: 'return id(Variable_speed_fan).oscillating ==0;'
then:
- output.turn_on: osc_led
- lambda:
auto call = id(Variable_speed_fan).turn_on();
call.set_oscillating(true);
call.perform();
else:
- output.turn_off: osc_led
- lambda:
auto call = id(Variable_speed_fan).turn_on();
call.set_oscillating(false);
call.perform();
#----------------------------------红外侦测模块用配置-------------------------------------------
- platform: gpio
pin:
number: $pir_pin
# inverted: true #反转属性
mode:
input: true #如果为 true,则将引脚配置为输入。
# pullup: true #激活引脚上的内部上拉电阻。
# pulldown: true #激活引脚上的内部下拉电阻。
name: "${friendly_name}_motion"
id: "${friendly_name}_motion"
device_class: motion
filters: #二进制传感器滤波器
- delayed_off: 5s #发布 OFF 值之前将等待10秒
# - delayed_on: 5s #触发值持续5秒才发布 On 值
#----------------------------------雷达模块用配置-------------------------------------------
- platform: gpio
pin:
number: $Radar_pin
# inverted: true #反转属性
mode:
input: true #如果为 true,则将引脚配置为输入。
# pullup: true #激活引脚上的内部上拉电阻。
pulldown: true #激活引脚上的内部下拉电阻。
name: "${friendly_name}_Radar"
id: "${friendly_name}_Radar"
device_class: motion
icon: mdi:radar #自定义图标
filters: #二进制传感器滤波器
- delayed_off: 5s #发布 OFF 值之前将等待时间
# - delayed_on: 5s #触发值持续5秒才发布 On 值
#time:
# - platform: homeassistant
# id: homeassistant_time
# Enable time component to reset energy at midnight
# - platform: sntp
# id: my_time
sensor:
- platform: wifi_signal #WiFi信号强度
name: "${friendly_name}_wifi_signal"
icon: mdi:signal #自定义图标
update_interval: 120s
- platform: uptime
id: ${friendly_name}_raw_up_time
update_interval: 15min
internal: true
on_raw_value:
then:
- text_sensor.template.publish:
id: ${friendly_name}_uptime_human
state: !lambda |-
int seconds = round(id(${friendly_name}_raw_up_time).raw_state);
int days = seconds / (24 * 3600);
seconds = seconds % (24 * 3600);
int hours = seconds / 3600;
seconds = seconds % 3600;
int minutes = seconds / 60;
seconds = seconds % 60;
return (
(days ? String(days) + "d " : "") +
(hours ? String(hours) + "h " : "") +
(minutes ? String(minutes) + "m " : "") +
(String(seconds) + "s")
).c_str();
- platform: bh1750
name: "${friendly_name}_Illuminance"
i2c_id: bh1750_i2c
address: 0x23
update_interval: 30s
- platform: aht10
i2c_id: aht10_i2c
temperature:
name: "${friendly_name}_Temperature"
humidity:
name: "${friendly_name}_Humidity"
update_interval: 60s
- platform: internal_temperature
name: "${friendly_name}_ESPTemp"
update_interval: 120s
text_sensor:
- platform: template
name: '${friendly_name}_Uptime'
id: ${friendly_name}_uptime_human
icon: mdi:clock-start #自定义图标
#------------------------倒计时状态----------------------------
- platform: template
name: "${friendly_name}_countdown" #倒计时模板传感器
id: countdown_display_result
icon: "mdi:timer-outline"
lambda: |-
if (id(countdown_display_result)) {
return { "未开启" }; // Replace this with your desired message when the script is running.
} else {
return { "停止" };
}
#------------------------睡眠模式状态--------------------------
- platform: template
id: script_status_display
name: "${friendly_name}_SLEEP"
icon: "mdi:weather-night"
update_interval: 30s
lambda: |-
if (id(sequence_script_running)) {
return { "启用" };
} else {
return { "未开启" };
}
- platform: wifi_info
ip_address:
name: ${friendly_name}_IP #IP地址
icon: mdi:ip-network #自定义图标
ssid:
name: ${friendly_name}_SSID
icon: mdi:wifi #自定义图标
mac_address:
name: ${friendly_name}_mac
icon: "mdi:wifi-marker" #自定义图标
|