Over-The-Air Updates

Example Usage

// Setup basic OTA
App.init_ota();
// Enable safe mode.
App.init_ota()->start_safe_mode();
// OTA password
auto *ota = App.init_ota();
ota->set_auth_plaintext_password("VERY_SECURE");
ota->start_safe_mode();
// OTA MD5 password
auto *ota = App.init_ota();
ota->start_safe_mode();

API Reference

OTAComponent

class OTAComponent : public Component

OTAComponent provides a simple way to integrate Over-the-Air updates into your app using ArduinoOTA.

Public Functions

OTAComponent(uint16_t port = OTA_DEFAULT_PORT)

Construct an OTAComponent.

Defaults to no authentication.

Parameters
  • port: The port ArduinoOTA will listen on.

void set_auth_password(const std::string &password)

Set a plaintext password that OTA will use for authentication.

Warning: This password will be stored in plaintext in the ROM and can be read by intruders. It is however secured somewhat secure again MITM attacks (attackers could still modify the binary while you’re sending it, but they can’t start OTA processes themselves.)

Parameters
  • password: The plaintext password.

void set_auth_plaintext_password(const std::string &password)

Set a plaintext password for legacy OTA.

Parameters
  • password: The password

void set_auth_password_hash(const std::string &hash)

Set a hashed password for legacy OTA.

Parameters
  • password: The MD5 password hash.

void set_port(uint16_t port)

Manually set the port OTA should listen on.

void start_safe_mode(uint8_t num_attempts = 10, uint32_t enable_time = 120000)

Start OTA safe mode.

When called at startup, this method will automatically detect boot loops.

If a boot loop is detected (by num_attempts boots which each lasted less than enable_time), this method will block startup and enable just WiFi and OTA so that users can flash a new image.

When in boot loop safe mode, if no OTA attempt is made within enable_time milliseconds, the device is restarted. If the device has stayed on for more than enable_time milliseconds, the boot is considered successful and the num_attempts counter is reset.

Parameters
  • num_attempts: The number of failed boot attempts until which time safe mode should be enabled.
  • enable_time: The time in ms safe mode should be on before restarting.

void setup()

Where the component’s initialization should happen.

Analogous to Arduino’s setup(). This method is guaranteed to only be called once. Defaults to doing nothing.

void dump_config()
float get_setup_priority() const

priority of setup().

higher -> executed earlier

Defaults to 0.

Return
The setup priority of this component

void loop()

This method will be called repeatedly.

Analogous to Arduino’s loop(). setup() is guaranteed to be called before this. Defaults to doing nothing.

uint16_t get_port() const
void clean_rtc()

Protected Types

enum [anonymous]

Values:

OPEN
PLAINTEXT
HASH

Protected Functions

void write_rtc_(uint8_t val)
uint8_t read_rtc_()
void handle_()
size_t wait_receive_(uint8_t *buf, size_t bytes, bool check_disconnected = true)

Protected Attributes

OTAComponent::[anonymous] OPEN
std::string password_
uint16_t port_
WiFiServer *server_ = {nullptr}
WiFiClient client_ = {}
bool ota_triggered_ = {false}
bool has_safe_mode_ = {false}

stores whether safe mode can be enabled.

uint32_t safe_mode_start_time_

stores when safe mode was enabled.

uint32_t safe_mode_enable_time_ = {60000}

The time safe mode should be on for.

uint8_t safe_mode_rtc_value_
uint8_t safe_mode_num_attempts_
uint8_t at_ota_progress_message_ = {0}

store OTA progress message index so that we don’t spam logs

ESPPreferenceObject rtc_