#' ============================================================================== #' ESP32-C3 SUPER MINI - CT RELAY CONTROLLER WITH OLED & ROTARY ENCODERS #' ============================================================================== #' UPDATED GPIO ASSIGNMENTS OVERVIEW: #' ------------------------------------------------------------------------------ #' GPIO 0 : Rotary Encoder 1 - Phase A (Current Threshold) #' GPIO 1 : Rotary Encoder 1 - Phase B (Current Threshold) #' GPIO 2 : Rotary Encoder 1 - Push Button (Toggle Display On/Off) #' GPIO 3 : Rotary Encoder 2 - Phase A (Off-Delay) #' GPIO 4 : CT Current Sensor (Analog Input ADC1_CH4) [MOVED FROM GPIO7] #' GPIO 7 : Rotary Encoder 2 - Phase B (Off-Delay) [MOVED FROM GPIO4] #' GPIO 5 : I2C - SCL (SH1106 Display) #' GPIO 6 : I2C - SDA (SH1106 Display) #' GPIO 8 : Relay Output (Active High) #' ============================================================================== esphome: name: afzuiginglageweg esp32: board: esp32-c3-devkitm-1 variant: esp32c3 preferences: flash_write_interval: 1min logger: api: ota: - platform: esphome wifi: ssid: !secret wifi_ssid password: !secret wifi_password manual_ip: static_ip: 192.168.1.173 gateway: 192.168.1.1 subnet: 255.255.255.0 dns1: 192.168.1.1 ap: ssid: "CT-Relay Fallback AP" password: !secret ap_password i2c: sda: GPIO6 scl: GPIO5 scan: true font: - file: "gfonts://Roboto" id: font_main size: 10 globals: - id: current_threshold type: float restore_value: true initial_value: '0.5' - id: off_delay_seconds type: int restore_value: true initial_value: '5' - id: display_timeout_timer type: int restore_value: false initial_value: '0' binary_sensor: - platform: gpio pin: number: GPIO2 mode: INPUT_PULLUP id: encoder_1_button name: "Display Toggle Button" on_press: then: - lambda: |- id(display_timeout_timer) = 10; id(main_display).turn_on(); - id: load_detected platform: template name: "Load Detected" lambda: |- return id(ct_current).state >= id(current_threshold); on_state: then: - if: condition: binary_sensor.is_on: load_detected then: - switch.turn_on: relay_output - script.stop: delay_off_script else: - script.execute: delay_off_script sensor: # Current Transformer Reading on valid ADC Pin (GPIO4) - platform: ct_clamp sensor: adc_sensor name: "CT Measured Current" id: ct_current update_interval: 500ms sample_duration: 200ms accuracy_decimals: 0 force_update: true filters: - calibrate_linear: - 0.01 -> 0 - 1 -> 1 - platform: adc pin: GPIO4 id: adc_sensor attenuation: 12db # Rotary Encoder 1: Threshold Control - platform: rotary_encoder name: "Threshold Setting" pin_a: GPIO0 pin_b: GPIO1 id: encoder_threshold on_value: then: - lambda: |- static int last_val = 0; int diff = x - last_val; last_val = x; id(current_threshold) += (diff * 0.1); if (id(current_threshold) < 0.0) id(current_threshold) = 0.0; id(display_timeout_timer) = 10; id(main_display).turn_on(); # Rotary Encoder 2: Delay Control - platform: rotary_encoder name: "Delay Setting" pin_a: GPIO3 pin_b: GPIO7 id: encoder_delay on_value: then: - lambda: |- static int last_val = 0; int diff = x - last_val; last_val = x; id(off_delay_seconds) += diff; if (id(off_delay_seconds) < 0) id(off_delay_seconds) = 0; id(display_timeout_timer) = 10; id(main_display).turn_on(); switch: - platform: gpio pin: GPIO8 inverted: true name: "Relay Output" id: relay_output script: - id: delay_off_script mode: restart then: - delay: !lambda "return id(off_delay_seconds) * 1000;" - switch.turn_off: relay_output display: - platform: ssd1306_i2c model: "SH1106 128x64" address: 0x3C id: main_display update_interval: 0.1s lambda: |- it.printf(0, 0, id(font_main), "Current: %.2f A", id(ct_current).state); it.printf(0, 11, id(font_main), "Threshold: %.1f A", id(current_threshold)); it.printf(0, 22, id(font_main), "Delay Set: %d s", id(off_delay_seconds)); it.printf(0, 33, id(font_main), "Relay State: %s", id(relay_output).state ? "ON" : "OFF"); it.printf(0, 44, id(font_main), "Delay: %d s", id(display_timeout_timer)); interval: - interval: 1s then: - lambda: |- if (id(display_timeout_timer) > 0) { id(display_timeout_timer)--; if (id(display_timeout_timer) == 0) { id(main_display).turn_off(); } }