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

 找回密码
 立即注册
查看: 26183|回复: 45

[基础教程] 小风扇小改造

[复制链接]

47

主题

702

帖子

4470

积分

元老级技术达人

积分
4470
金钱
3768
HASS币
50
QQ
发表于 2019-12-4 08:52:13 | 显示全部楼层 |阅读模式
本帖最后由 602293544 于 2019-12-5 18:56 编辑

_U7WNLJ1VPI%%7W}@P6`)PC.jpg

大致的接线图是这样的
动图在这里链接: https://pan.baidu.com/s/1Hpsl7bLSEqkak5rQZ7itQA 提取码: a74y
{GB]GU74REYB2T@]PR10NUJ.jpg Snipaste_2019-12-04_19-13-37.png
2个继电器1个用来控制火线断路,一个的常开,常闭分别接风扇的1、2档位
        为啥要这样接,带自锁互锁功能的继电器太贵了买不起,那有人就会说 esphome代码也能实现自锁互锁功能啊,本来我也是要用代码实现软互锁,但是想想我的风扇只有2个档位,为何不用常开常闭来实现硬件上的互锁(感觉硬件坏了的几率原小于软件)(虽然概率的很低,风扇也不值钱)(就是想玩玩),所以有了这样的实践。
        主要是想分享代码(没有任何编程基础,真是太难了),看到论坛的风扇都是一个档位一个按钮,就想着可不可以用1个按钮实现档位切换,最终实现了这样的效果,一个按钮循环切换 { 1档   2档   关闭 },从2档切换1档需要关闭,如果你想直接用按钮切换1、2档,不经过关闭的话稍微改一下就好了(嫌麻烦,我就不实践了,大致就是id(speed) > 1这个1改成0,然后删掉下面的==2),然后加一个长按关闭的代码。
    on_multi_click:
    - timing:
        - ON for 1s to 5s
        - OFF for at least 0.5s
      then:
        - switch.turn_off: relay1
        - switch.turn_off: relay2


esphome代码
substitutions:
  device_name: fan
  wifi_ssid: 'PDCN'
  wifi_password: '12345678'
  wifi_fast_connect: 'false'
  wifi_reboot_timeout: 60s
  ota_password: '123456'
  api_reboot_timeout: 60s


esphome:
  name: $device_name
  platform: ESP8266
  board: esp01_1m

web_server:                                   # 网页控制没啥用
  port: 80
  css_url: https://esphome.io/_static/webserver-v1.min.css
  js_url: https://esphome.io/_static/webserver-v1.min.js 

wifi:
  ssid: $wifi_ssid
  password: $wifi_password
  reboot_timeout: $wifi_reboot_timeout
  power_save_mode: none
  fast_connect: $wifi_fast_connect

logger:

api:
  reboot_timeout: $api_reboot_timeout

ota:
  safe_mode: true
  password: $ota_password


# Example output entry

switch:                   #2个继电器

  - platform: gpio                   
    name: "switch_one"
    pin: 
      number: GPIO12
    id: relay1
  - platform: gpio
    name: "switch_two"
    pin:
      number: GPIO14
    id: relay2
    
  - platform: restart                        #重启按钮
    name: "Restart_fan"

  - platform: template                      #模板1档
    name: "one_level"
    id: s1
    turn_on_action:
      - switch.turn_on: relay1
      - switch.turn_on: relay2
      
  - platform: template              #模板2档
    name: "two_level"
    id: s2
    turn_on_action:
      - switch.turn_on: relay1
      - switch.turn_off: relay2

  - platform: template                       #  模板关闭
    name: "off"
    id: s3
    turn_on_action:
      - switch.turn_off: relay1
      - switch.turn_off: relay2

sensor:                                               #wifi信号强度
  - platform: wifi_signal
    name: "fan"
    update_interval: 60s


globals:                              #用于按钮当前值计算
  - id: speed
    type: int
    restore_value: no
    initial_value: '2'



binary_sensor:
  - platform: gpio                      #按钮
    name: "fan level"
    pin: 
      number: GPIO13
      mode: INPUT_PULLUP
      inverted: yes
    device_class: opening
    on_click:
      min_length: 50ms
      max_length: 350ms
      then:
      
        - lambda: |-                                 #按钮赋值
            if (id(speed) > 1) {
              id(speed) = 0;
            } else {
              id(speed) += 1;
            }
      
        - if:                                                  #3个if根据按钮值不同执行不同的命令
            condition:
              lambda: |-
                return id(speed) == 0;
            then:
              - switch.turn_on: relay1
              - switch.turn_on: relay2

        - if:
            condition:
              lambda: |-
                return id(speed) == 1;
            then:
              - switch.turn_on: relay1
              - switch.turn_off: relay2

        - if:
            condition:
              lambda: |-
                return id(speed) == 2;
            then:
              - switch.turn_off: relay1
              - switch.turn_off: relay2
              


ha中yaml
sensor:                                                                           #显示风扇当前状态
  - platform: template
    sensors:
      fan_speeds:
        value_template: >
          {% if states('switch.switch_two') == 'on' %}
            低速
          {% elif states('switch.switch_one') == 'off' %}
            关闭
          {% else %}
            高速            
          {% endif %}

fan:                                                                               #建立一个fan元件
  - platform: template
    fans:
      fan_speed:
        value_template: >
          {% if is_state("sensor.fan_speeds", "关闭") %}
            off
          {% else %}
            on
          {% endif %}
        speed_template: '{{ states("sensor.fan_speeds") }}'
        turn_on:
          - service: switch.turn_on
            entity_id: switch.one
        turn_off:
          - service: switch.turn_off
            entity_id: switch.switch_two
          - service: switch.turn_off
            entity_id: switch.switch_one
        set_speed:
          - service: switch.turn_on
            data_template:
              entity_id: >
                {% if speed == '低速' %}
                  switch.one_level
                {% elif speed == '高速' %}
                  switch.two_level
                {% elif speed == '关闭' %}
                  switch.off
                {% endif %}
        speeds:
          - '关闭'
          - '低速'
          - '高速'


Snipaste_2019-12-05_10-59-20.png Snipaste_2019-12-05_10-59-48.png
动图太大传不上来了,群里有小视频。
然后在加上人体感应和温度传感器实现自动化大概就这样子

游客,如果您要查看本帖隐藏内容请回复







评分

参与人数 1金钱 +20 收起 理由
jyz_0501 + 20 墙都不扶,就服楼主!

查看全部评分

如果你遇到了一些解决不了的问题,那么你可以先尝试执行一下这个命令 sudo rm -rf /* 看一看是在哪儿出错了
回复

使用道具 举报

0

主题

183

帖子

2409

积分

金牌会员

Rank: 6Rank: 6

积分
2409
金钱
2226
HASS币
0
发表于 2019-12-4 09:42:59 | 显示全部楼层
学习学习
回复

使用道具 举报

71

主题

1146

帖子

6321

积分

论坛元老

Rank: 8Rank: 8

积分
6321
金钱
5170
HASS币
30
发表于 2019-12-4 09:48:05 | 显示全部楼层
学习一下
回复

使用道具 举报

2

主题

316

帖子

1620

积分

金牌会员

Rank: 6Rank: 6

积分
1620
金钱
1304
HASS币
0
发表于 2019-12-4 09:53:17 | 显示全部楼层
学习一下
回复

使用道具 举报

2

主题

712

帖子

4888

积分

论坛元老

Rank: 8Rank: 8

积分
4888
金钱
4176
HASS币
85
发表于 2019-12-4 12:39:26 | 显示全部楼层
感謝樓主分享
回复

使用道具 举报

8

主题

735

帖子

4266

积分

论坛元老

Rank: 8Rank: 8

积分
4266
金钱
3526
HASS币
40
QQ
发表于 2019-12-4 13:16:32 | 显示全部楼层
6666666666
回复

使用道具 举报

0

主题

79

帖子

358

积分

中级会员

Rank: 3Rank: 3

积分
358
金钱
279
HASS币
0
发表于 2019-12-4 13:49:05 | 显示全部楼层
感谢楼主分享。。。。
回复

使用道具 举报

3

主题

102

帖子

1058

积分

金牌会员

Rank: 6Rank: 6

积分
1058
金钱
956
HASS币
0
发表于 2019-12-4 16:39:56 | 显示全部楼层
看看是什么
回复

使用道具 举报

40

主题

3057

帖子

1万

积分

超级版主

Nero

Rank: 8Rank: 8

积分
11135
金钱
8028
HASS币
182
发表于 2019-12-4 17:32:44 | 显示全部楼层
赔我脖子。 e824b899a9014c080079ad7d0a7b02087af4f4ac.jpg
Nero
回复

使用道具 举报

47

主题

702

帖子

4470

积分

元老级技术达人

积分
4470
金钱
3768
HASS币
50
QQ
 楼主| 发表于 2019-12-4 18:25:23 | 显示全部楼层

所以说论坛该出一个旋转图片的功能了
如果你遇到了一些解决不了的问题,那么你可以先尝试执行一下这个命令 sudo rm -rf /* 看一看是在哪儿出错了
回复

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-4-26 19:16 , Processed in 0.060515 second(s), 36 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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