From 2aef41a35038c36009bb19eccbc9d3740dc865c0 Mon Sep 17 00:00:00 2001 From: Aiden Date: Fri, 19 Dec 2025 13:35:31 +0800 Subject: [PATCH] Add Bluetooth proxy --- athom-1gang-switch.yaml | 42 +++++++++++++++++++++++++++++++++++++---- athom-2gang-switch.yaml | 42 +++++++++++++++++++++++++++++++++++++---- athom-3gang-switch.yaml | 42 +++++++++++++++++++++++++++++++++++++---- athom-4gang-switch.yaml | 42 +++++++++++++++++++++++++++++++++++++---- 4 files changed, 152 insertions(+), 16 deletions(-) diff --git a/athom-1gang-switch.yaml b/athom-1gang-switch.yaml index f03c0ea..f321b95 100644 --- a/athom-1gang-switch.yaml +++ b/athom-1gang-switch.yaml @@ -10,7 +10,7 @@ substitutions: # Project Name project_name: "China Athom Technology.1Gang Switch" # Projection version denotes the release version of the yaml file, allowing checking of deployed vs latest version - project_version: "v3.0.0" + project_version: "v3.0.1" # Restore the relay (GPO switch) upon reboot to state: light_restore_mode: RESTORE_DEFAULT_OFF # Define a domain for this device to use. i.e. iot.home.lan (so device will appear as athom-smart-plug-v2.iot.home.lan in DNS/DHCP logs) @@ -68,7 +68,6 @@ esphome: } } - esp32: board: esp32-c3-devkitm-1 flash_size: 4MB @@ -76,18 +75,40 @@ esp32: framework: type: esp-idf version: recommended + sdkconfig_options: + # @grigi found in testing that these options resulted in better responsiveness. + # BLE 4.2 is supported by ALL ESP32 boards that have bluetooth, the original and derivatives. + CONFIG_BT_BLE_42_FEATURES_SUPPORTED: y + # Also enable this on any derivative boards (S2, C3 etc) but not the original ESP32. + CONFIG_BT_BLE_50_FEATURES_SUPPORTED: y + # Extend the watchdog timeout, so the device reboots if the device appears locked up for over 10 seconds. + CONFIG_ESP_TASK_WDT_TIMEOUT_S: "10" preferences: - flash_write_interval: 5min + flash_write_interval: 1min api: + # reboot_timeout: 0s + # Only enable BLE tracking when wifi is up and api is connected + # Gives single-core ESP32-C3 devices time to manage wifi and authenticate with api + on_client_connected: + - esp32_ble_tracker.start_scan: + continuous: true + # Disable BLE tracking when there are no api connections live + on_client_disconnected: + if: + condition: + not: + api.connected: + then: + - esp32_ble_tracker.stop_scan: ota: - platform: esphome logger: + baud_rate: 0 # 0 Enables logging, but disables serial-port logging to free CPU and memory level: ${log_level} - baud_rate: 115200 mdns: disabled: false @@ -112,6 +133,19 @@ captive_portal: esp32_improv: authorizer: none +esp32_ble_tracker: + scan_parameters: + # Don't auto start BLE scanning, we control it in the `api` block's automation. + continuous: false + active: true # send scan-request packets to gather more info, like device name for some devices. + interval: 320ms # default 320ms - how long to spend on each advert channel + window: 300ms # default 30ms - how long to actually "listen" in each interval. Reduce this if device is unstable. + # If the device cannot keep up or becomes unstable, reduce the "window" setting. This may be + # required if your device is controlling other sensors or doing PWM for lights etc. + +bluetooth_proxy: + active: true + dashboard_import: package_import_url: github://athom-tech/esp32-configs/athom-1gang-switch.yaml diff --git a/athom-2gang-switch.yaml b/athom-2gang-switch.yaml index 407b145..459fc9d 100644 --- a/athom-2gang-switch.yaml +++ b/athom-2gang-switch.yaml @@ -10,7 +10,7 @@ substitutions: # Project Name project_name: "China Athom Technology.2Gang Switch" # Projection version denotes the release version of the yaml file, allowing checking of deployed vs latest version - project_version: "v3.0.0" + project_version: "v3.0.1" # Restore the relay (GPO switch) upon reboot to state: light1_restore_mode: RESTORE_DEFAULT_OFF light2_restore_mode: RESTORE_DEFAULT_OFF @@ -71,7 +71,6 @@ esphome: } } - esp32: board: esp32-c3-devkitm-1 flash_size: 4MB @@ -79,18 +78,40 @@ esp32: framework: type: esp-idf version: recommended + sdkconfig_options: + # @grigi found in testing that these options resulted in better responsiveness. + # BLE 4.2 is supported by ALL ESP32 boards that have bluetooth, the original and derivatives. + CONFIG_BT_BLE_42_FEATURES_SUPPORTED: y + # Also enable this on any derivative boards (S2, C3 etc) but not the original ESP32. + CONFIG_BT_BLE_50_FEATURES_SUPPORTED: y + # Extend the watchdog timeout, so the device reboots if the device appears locked up for over 10 seconds. + CONFIG_ESP_TASK_WDT_TIMEOUT_S: "10" preferences: - flash_write_interval: 5min + flash_write_interval: 1min api: + # reboot_timeout: 0s + # Only enable BLE tracking when wifi is up and api is connected + # Gives single-core ESP32-C3 devices time to manage wifi and authenticate with api + on_client_connected: + - esp32_ble_tracker.start_scan: + continuous: true + # Disable BLE tracking when there are no api connections live + on_client_disconnected: + if: + condition: + not: + api.connected: + then: + - esp32_ble_tracker.stop_scan: ota: - platform: esphome logger: + baud_rate: 0 # 0 Enables logging, but disables serial-port logging to free CPU and memory level: ${log_level} - baud_rate: 115200 mdns: disabled: false @@ -115,6 +136,19 @@ captive_portal: esp32_improv: authorizer: none +esp32_ble_tracker: + scan_parameters: + # Don't auto start BLE scanning, we control it in the `api` block's automation. + continuous: false + active: true # send scan-request packets to gather more info, like device name for some devices. + interval: 320ms # default 320ms - how long to spend on each advert channel + window: 300ms # default 30ms - how long to actually "listen" in each interval. Reduce this if device is unstable. + # If the device cannot keep up or becomes unstable, reduce the "window" setting. This may be + # required if your device is controlling other sensors or doing PWM for lights etc. + +bluetooth_proxy: + active: true + dashboard_import: package_import_url: github://athom-tech/esp32-configs/athom-2gang-switch.yaml diff --git a/athom-3gang-switch.yaml b/athom-3gang-switch.yaml index bdd60fb..bc9b9a7 100644 --- a/athom-3gang-switch.yaml +++ b/athom-3gang-switch.yaml @@ -10,7 +10,7 @@ substitutions: # Project Name project_name: "China Athom Technology.3Gang Switch" # Projection version denotes the release version of the yaml file, allowing checking of deployed vs latest version - project_version: "v3.0.0" + project_version: "v3.0.1" # Restore the relay (GPO switch) upon reboot to state: light1_restore_mode: RESTORE_DEFAULT_OFF light2_restore_mode: RESTORE_DEFAULT_OFF @@ -74,7 +74,6 @@ esphome: } } - esp32: board: esp32-c3-devkitm-1 flash_size: 4MB @@ -82,18 +81,40 @@ esp32: framework: type: esp-idf version: recommended + sdkconfig_options: + # @grigi found in testing that these options resulted in better responsiveness. + # BLE 4.2 is supported by ALL ESP32 boards that have bluetooth, the original and derivatives. + CONFIG_BT_BLE_42_FEATURES_SUPPORTED: y + # Also enable this on any derivative boards (S2, C3 etc) but not the original ESP32. + CONFIG_BT_BLE_50_FEATURES_SUPPORTED: y + # Extend the watchdog timeout, so the device reboots if the device appears locked up for over 10 seconds. + CONFIG_ESP_TASK_WDT_TIMEOUT_S: "10" preferences: - flash_write_interval: 5min + flash_write_interval: 1min api: + # reboot_timeout: 0s + # Only enable BLE tracking when wifi is up and api is connected + # Gives single-core ESP32-C3 devices time to manage wifi and authenticate with api + on_client_connected: + - esp32_ble_tracker.start_scan: + continuous: true + # Disable BLE tracking when there are no api connections live + on_client_disconnected: + if: + condition: + not: + api.connected: + then: + - esp32_ble_tracker.stop_scan: ota: - platform: esphome logger: + baud_rate: 0 # 0 Enables logging, but disables serial-port logging to free CPU and memory level: ${log_level} - baud_rate: 115200 mdns: disabled: false @@ -118,6 +139,19 @@ captive_portal: esp32_improv: authorizer: none +esp32_ble_tracker: + scan_parameters: + # Don't auto start BLE scanning, we control it in the `api` block's automation. + continuous: false + active: true # send scan-request packets to gather more info, like device name for some devices. + interval: 320ms # default 320ms - how long to spend on each advert channel + window: 300ms # default 30ms - how long to actually "listen" in each interval. Reduce this if device is unstable. + # If the device cannot keep up or becomes unstable, reduce the "window" setting. This may be + # required if your device is controlling other sensors or doing PWM for lights etc. + +bluetooth_proxy: + active: true + dashboard_import: package_import_url: github://athom-tech/esp32-configs/athom-3gang-switch.yaml diff --git a/athom-4gang-switch.yaml b/athom-4gang-switch.yaml index 6f67475..e992a2c 100644 --- a/athom-4gang-switch.yaml +++ b/athom-4gang-switch.yaml @@ -10,7 +10,7 @@ substitutions: # Project Name project_name: "China Athom Technology.4Gang Switch" # Projection version denotes the release version of the yaml file, allowing checking of deployed vs latest version - project_version: "v3.0.0" + project_version: "v3.0.1" # Restore the relay (GPO switch) upon reboot to state: light1_restore_mode: RESTORE_DEFAULT_OFF light2_restore_mode: RESTORE_DEFAULT_OFF @@ -77,7 +77,6 @@ esphome: } } - esp32: board: esp32-c3-devkitm-1 flash_size: 4MB @@ -85,18 +84,40 @@ esp32: framework: type: esp-idf version: recommended + sdkconfig_options: + # @grigi found in testing that these options resulted in better responsiveness. + # BLE 4.2 is supported by ALL ESP32 boards that have bluetooth, the original and derivatives. + CONFIG_BT_BLE_42_FEATURES_SUPPORTED: y + # Also enable this on any derivative boards (S2, C3 etc) but not the original ESP32. + CONFIG_BT_BLE_50_FEATURES_SUPPORTED: y + # Extend the watchdog timeout, so the device reboots if the device appears locked up for over 10 seconds. + CONFIG_ESP_TASK_WDT_TIMEOUT_S: "10" preferences: - flash_write_interval: 5min + flash_write_interval: 1min api: + # reboot_timeout: 0s + # Only enable BLE tracking when wifi is up and api is connected + # Gives single-core ESP32-C3 devices time to manage wifi and authenticate with api + on_client_connected: + - esp32_ble_tracker.start_scan: + continuous: true + # Disable BLE tracking when there are no api connections live + on_client_disconnected: + if: + condition: + not: + api.connected: + then: + - esp32_ble_tracker.stop_scan: ota: - platform: esphome logger: + baud_rate: 0 # 0 Enables logging, but disables serial-port logging to free CPU and memory level: ${log_level} - baud_rate: 115200 mdns: disabled: false @@ -121,6 +142,19 @@ captive_portal: esp32_improv: authorizer: none +esp32_ble_tracker: + scan_parameters: + # Don't auto start BLE scanning, we control it in the `api` block's automation. + continuous: false + active: true # send scan-request packets to gather more info, like device name for some devices. + interval: 320ms # default 320ms - how long to spend on each advert channel + window: 300ms # default 30ms - how long to actually "listen" in each interval. Reduce this if device is unstable. + # If the device cannot keep up or becomes unstable, reduce the "window" setting. This may be + # required if your device is controlling other sensors or doing PWM for lights etc. + +bluetooth_proxy: + active: true + dashboard_import: package_import_url: github://athom-tech/esp32-configs/athom-4gang-switch.yaml