Emergency safe mode on ESPHome and ESP32 (re-using EN button on GPIO0)

 The GPIO0 'enable' button is usually only used at boot to put the device into flash mode.

After it has booted, we have a spare physical button on most dev boards.

We can use this to flag that ESPHome needs to go into safe mode (ie, not run any components other than wifi and ota).


We need to add an ID to the ota component:

ota:
  id: theota
  password: "(readcted)"


Then add the following YAML

globals:
  - id: press_ms
    type: int

binary_sensor:
  - platform: gpio
    id: failsafe
    pin:
      number: 0
      mode:
        input: true
        pullup: true
      inverted: true
    on_press:
      then:
        lambda: !lambda |-
          id(press_ms) = millis();
    on_release:
      then:
        lambda: !lambda |-
          int press_duration = millis() - id(press_ms);
          if (press_duration < 3000)
          {
            ESP_LOGD("FAILSAFE", "******* <3s -  Enter OTA Mode set.");
            id(theota)->set_safe_mode_pending(true);          
          }
          else
          {
            ESP_LOGD("FAILSAFE", "******* >3s - Factory Reset.");
            global_preferences->reset();
          }

This will set the flag to go into safe mode on next reboot (it wont reboot else it will go into flash mode).

So if you have a component misbehaving, you can press EN then RESET, and it will go into safe mode.

Comments

Popular posts from this blog

SSD Upgrade on Lenovo Legion Go - Part 1 - Backup the original drive using Veeam

SSD Upgrade on Lenovo Legion Go - Part 2 - Restore backup