techposts

Rotary Encoder Smart Switch

Aug 24th, 2024 (edited)
950
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.20 KB | Software | 0 0
  1. esphome:
  2.   name: smart-rotary-switch
  3.   friendly_name: Smart-Rotary-Switch
  4.   platformio_options:
  5.     upload_speed: 921600
  6.     board_build.flash_mode: dio
  7.  
  8. esp32:
  9.   board: seeed_xiao_esp32c3
  10.   variant: esp32c3
  11.  
  12. logger:
  13.   level: WARN
  14.   baud_rate: 0
  15.  
  16. # Enable Home Assistant API
  17. api:
  18.   encryption:
  19.     key: "xxxxxxxxxxxxxxxxx"
  20.  
  21. ota:
  22.   - platform: esphome
  23.     password: "xxxxxxxx"
  24.  
  25. wifi:
  26.   ssid: !secret wifi_ssid
  27.   password: !secret wifi_password
  28.  
  29.   # Enable fallback hotspot (captive portal) in case wifi connection fails
  30.   ap:
  31.     ssid: "Smart-Rotary-Switch"
  32.     password: "xxxxxxxx"
  33.  
  34. captive_portal:
  35.  
  36. esp32_ble_tracker:
  37.  
  38. bluetooth_proxy:
  39.   active: true
  40.  
  41. esp32_ble_server:
  42.  
  43. globals:
  44.   - id: encoder_value
  45.     type: int
  46.     initial_value: '0'
  47.     restore_value: true
  48.  
  49. sensor:
  50.   - platform: rotary_encoder
  51.     name: "Rotary Encoder Brightness"
  52.     id: brightness_encoder
  53.     pin_a: GPIO2
  54.     pin_b: GPIO3
  55.     min_value: 0
  56.     max_value: 255
  57.     resolution: 2
  58.     on_clockwise:
  59.       then:
  60.         - lambda: |-
  61.             if (id(encoder_value) < 25) {
  62.               id(encoder_value) += 1;
  63.               id(my_custom_encoder_script).execute();
  64.             }
  65.     on_anticlockwise:
  66.       then:
  67.         - lambda: |-
  68.             if (id(encoder_value) > 0) {
  69.               id(encoder_value) -= 1;
  70.               id(my_custom_encoder_script).execute();
  71.             }
  72.     filters:
  73.       - debounce: 25ms
  74.  
  75. binary_sensor:
  76.   - platform: gpio
  77.     pin:
  78.       number: GPIO4
  79.       inverted: true
  80.       mode:
  81.         input: true
  82.         pullup: true
  83.     name: "Rotary Encoder Button"
  84.     on_click:
  85.       - homeassistant.service:
  86.           service: script.re_button_press
  87.     filters:
  88.       - delayed_on: 10ms
  89.  
  90. script:
  91.   - id: my_custom_encoder_script
  92.     then:
  93.       - lambda: |-
  94.           // Ensure the encoder value does not go below 0 or above 25
  95.           if (id(encoder_value) < 0) {
  96.             id(encoder_value) = 0;
  97.           } else if (id(encoder_value) > 25) {
  98.             id(encoder_value) = 25;
  99.           }
  100.       - homeassistant.service:
  101.           service: script.handle_encoder_value
  102.           data:
  103.             value: !lambda 'return id(encoder_value);'
Tags: smart Home
Advertisement
Add Comment
Please, Sign In to add comment