Skip to content

Instantly share code, notes, and snippets.

@jamesshannon
Created February 5, 2025 04:45
Show Gist options
  • Save jamesshannon/9f6769292e0af15a7ec64c7156238982 to your computer and use it in GitHub Desktop.
Save jamesshannon/9f6769292e0af15a7ec64c7156238982 to your computer and use it in GitHub Desktop.
ESPHome internet watchdog configuration - WIP
esphome:
name: plug-pm-1
friendly_name: Plug (PM) 1
platformio_options:
board_build.mcu: esp32c3
board_build.variant: esp32c3
esp32:
variant: ESP32C3
board: esp32-c3-devkitm-1
framework:
type: esp-idf
sdkconfig_options:
CONFIG_BT_BLE_50_FEATURES_SUPPORTED: y
CONFIG_BT_BLE_42_FEATURES_SUPPORTED: y
CONFIG_ESP_TASK_WDT_TIMEOUT_S: "10"
safe_mode:
logger:
level: DEBUG
logs:
component: NONE
esp-idf: WARN
api:
actions:
- action: power_cycle
then:
script.execute: power_cycle
ota:
platform: esphome
password: "PASSWORD"
captive_portal:
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
ap:
ssid: "Plug-Pm-1 Fallback Hotspot"
password: "tpHfmEHr135L"
button:
- platform: safe_mode
name: Safe Mode
http_request:
interval:
- interval: 1min
then:
- http_request.get:
url: https://www.google.com/generate_204
on_response:
then:
- logger.log:
level: DEBUG
format: 'Response status: %d, Duration: %u ms'
args:
- response->status_code
- response->duration_ms
- if:
condition:
lambda: 'return id(failed_requests).state > 0;'
then:
- logger.log:
level: INFO
format: "Successful request; resetting failed request counter to 0"
- number.set:
id: failed_requests
value: 0
on_error:
then:
- logger.log: "Request failed!"
- number.increment: failed_requests
number:
- platform: template
id: failed_requests
name: Failed Requests
optimistic: True
restore_value: True
min_value: 0
max_value: 10000
step: 1
on_value:
then:
- lambda: |-
int val = static_cast<int>(x);
if (val == 1024) {
ESP_LOGW("main", "resetting %d", val);
auto call = id(failed_requests).make_call();
call.set_value(val >> 1);
call.perform();
return;
}
bool should_reset = (val >= 2) & (( val & (val - 1)) == 0);
if (should_reset) {
ESP_LOGW("main", "POWER CYCLING %d", should_reset);
id(power_cycle).execute();
ESP_LOGW("main", "NOT DONE POWER CYCLING %d", should_reset);
}
switch:
- platform: gpio
name: Relay
id: relay
device_class: outlet
pin: GPIO6
restore_mode: RESTORE_DEFAULT_OFF
on_turn_on:
then:
- light.turn_on: white_led
on_turn_off:
then:
- light.turn_off: white_led
binary_sensor:
- platform: gpio
internal: true
pin:
number: GPIO2
mode: INPUT_PULLUP
inverted: true
name: Button
filters:
- delayed_on: 10ms
on_press:
then:
- switch.toggle: relay
light:
- platform: binary
internal: true
name: White LED
id: white_led
output: white_output
restore_mode: RESTORE_DEFAULT_OFF
output:
- id: white_output
platform: gpio
pin: GPIO7
inverted: true
status_led:
pin:
number: GPIO8
inverted: true
script:
- id: power_cycle
then:
- switch.turn_off: relay
- delay: 15s
- switch.turn_on: relay
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment