├── LICENSE ├── README.md └── n1-fan.yml /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Chan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # esphome-pwm-fan 2 | 8266通过ESPHOME使用PWM控制无级调速风扇 3 | 4 | ![yaofan](https://user-images.githubusercontent.com/6293952/196037499-17ef6aec-9fe4-4fc2-a4ac-811a12bfb380.jpg) 5 | 6 | ## 硬件 7 | - 12V电源(5.5mm或线头式的随意,取决于你的降压模块和是否焊接) 8 | - 12V转5V降压模块 9 | - PWM MOS管 10 | - 12V风扇 11 | - 8266模块 12 | 13 | ## 连接 14 | 1. 首先将12V单元焊接到降压模块的正负极输入上,如果你的降压模块带5.5mm插头的也可以直接用电源插入。注意此时电源先不接入220V. 15 | 2. 降压板的12V输出接MOS管的正负极输入,MOS管的正负极输出接风扇的正负极。注意风扇的正负极,反接风扇不转。 16 | 3. 12V电源的USB输出口,插入Micro USB给8266供电。 17 | 4. 8266的D4接口接到MOS管的PWM针脚,G接到MOS管的GND. 18 | 5. 8266的5V输出和G接到SHTC3的正负极,D5、D6分别接到SDA和SCL。 19 | 20 | ## 烧录 21 | 1. 8266的USB接入电脑或HA主机,用esphome刷入仓库内的yaml文件。 22 | 2. 烧录成功后,12V接上电源,USB接回降压板上。(8266亦可单独供电) 23 | 3. 此时通电后,风扇过5秒左右会自动启动。 24 | 25 | ## 接入 26 | 1. 直接在HA的设备里,新增集成。选择esphome 27 | 2. 通过路由器找到风扇的IP 28 | 3. 集成添加8266的IP即可 29 | 30 | ## 交流 31 | - QQ群:198841186 32 | 33 | - 微信群:(添加该机器人,发送“进群”会自动发送邀请链接) 34 | 35 | ![xiaomi miot weixin group](https://user-images.githubusercontent.com/4549099/161735971-0540ce1c-eb49-4aff-8cb3-3bdad15e22f7.png) 36 | -------------------------------------------------------------------------------- /n1-fan.yml: -------------------------------------------------------------------------------- 1 | substitutions: 2 | device_name: "N1 Fan" 3 | device_name_low: "n1-fan" 4 | comment: "N1 Fan" 5 | version: "1.0.0" 6 | speed_low: "50" 7 | speed_med: "75" 8 | speed_full: "100" 9 | 10 | esphome: 11 | name: "${device_name_low}" 12 | project: 13 | name: "n1.fan_controller" 14 | version: "${version}" 15 | on_boot: 16 | then: 17 | - delay: 10s 18 | - fan.turn_on: 19 | id: fan_speed 20 | speed: "${speed_low}" 21 | - delay: 5s 22 | - fan.turn_on: 23 | id: fan_speed 24 | speed: "${speed_full}" 25 | 26 | esp8266: 27 | board: nodemcuv2 28 | 29 | # Enable logging 30 | logger: 31 | 32 | # Enable Home Assistant API 33 | api: 34 | 35 | ota: 36 | password: "12345678" 37 | 38 | wifi: 39 | ssid: !secret wifi_ssid #WiFi SSID 40 | password: !secret wifi_password #WiFi密码 41 | 42 | # Enable fallback hotspot (captive portal) in case wifi connection fails 43 | ap: 44 | ssid: "${device_name_low}" 45 | password: "12345678" 46 | 47 | captive_portal: 48 | 49 | web_server: 50 | port: 80 51 | 52 | #定义i2c pin口 53 | i2c: 54 | sda: D6 55 | scl: D5 56 | scan: True 57 | 58 | output: 59 | - platform: esp8266_pwm 60 | pin: D4 61 | frequency: 15000 Hz 62 | id: pwm_output 63 | 64 | fan: 65 | - platform: speed 66 | id: fan_speed 67 | output: pwm_output 68 | speed_count: 100 69 | name: "${device_name} 开关" 70 | 71 | number: 72 | - platform: template 73 | icon: "mdi:fan" 74 | id: speed_input 75 | name: "${device_name} 风速" 76 | lambda: |- 77 | return id(fan_speed).speed; 78 | step: 1 79 | min_value: 1 80 | max_value: 100 81 | unit_of_measurement: "%" 82 | update_interval: 1s 83 | set_action: 84 | then: 85 | - fan.turn_on: 86 | id: fan_speed 87 | speed: !lambda "return x;" 88 | - component.update: fanmode 89 | - component.update: sensor_fanspeed 90 | 91 | select: 92 | - platform: template 93 | icon: "mdi:fan" 94 | name: "${device_name} 模式" 95 | id: fanmode 96 | update_interval: 1s 97 | lambda: |- 98 | if(id(fan_speed).speed <= ${speed_low}) 99 | return std::string("Low"); 100 | else if(id(fan_speed).speed == ${speed_med}) 101 | return std::string("Medium"); 102 | else if(id(fan_speed).speed >= ${speed_full}) 103 | return std::string("Full"); 104 | else 105 | return std::string("Custom"); 106 | set_action: 107 | then: 108 | - lambda: |- 109 | auto call = id(fan_speed).turn_on(); 110 | 111 | if(x == "Low") 112 | call.set_speed(${speed_low}); 113 | else if(x == "Medium") 114 | call.set_speed(${speed_med}); 115 | else if(x == "Full") 116 | call.set_speed(${speed_full}); 117 | else 118 | call.set_speed(id(speed_input).state); 119 | 120 | call.perform(); 121 | - component.update: speed_input 122 | - component.update: sensor_fanspeed 123 | options: 124 | - Low 125 | - Medium 126 | - Full 127 | - Custom 128 | 129 | button: 130 | - platform: template 131 | icon: "mdi:fan-speed-1" 132 | name: "${device_name} Low" 133 | id: fanlow 134 | on_press: 135 | - fan.turn_on: 136 | id: fan_speed 137 | speed: "${speed_low}" 138 | - platform: template 139 | icon: "mdi:fan-speed-2" 140 | name: "${device_name} Med" 141 | on_press: 142 | - fan.turn_on: 143 | id: fan_speed 144 | speed: "${speed_med}" 145 | - platform: template 146 | icon: "mdi:fan-speed-3" 147 | name: "${device_name} Full" 148 | on_press: 149 | - fan.turn_on: 150 | id: fan_speed 151 | speed: "${speed_full}" 152 | - platform: restart 153 | name: "${device_name} 初始化" 154 | 155 | sensor: 156 | - platform: template 157 | id: sensor_fanspeed 158 | icon: "mdi:fan" 159 | name: "风扇速度" 160 | lambda: |- 161 | return id(fan_speed).speed; 162 | update_interval: 1s 163 | accuracy_decimals: 0 164 | unit_of_measurement: "%" 165 | - platform: shtcx 166 | temperature: 167 | name: "${device_name} Temperature" 168 | humidity: 169 | name: "${device_name} Humidity" 170 | address: 0x70 171 | update_interval: 5s #获取频率 172 | --------------------------------------------------------------------------------