From 0b6708ddbeb6950addb19ced6387798fd80b9ce9 Mon Sep 17 00:00:00 2001 From: oarcher Date: Tue, 13 Aug 2024 23:10:02 +0200 Subject: [PATCH] allow reconnect --- esphome/components/modem/modem_component.cpp | 19 +++++++++++++++++-- esphome/components/modem/modem_component.h | 3 +++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/esphome/components/modem/modem_component.cpp b/esphome/components/modem/modem_component.cpp index a6c3fcd2cf..3a85e74581 100644 --- a/esphome/components/modem/modem_component.cpp +++ b/esphome/components/modem/modem_component.cpp @@ -153,6 +153,17 @@ void ModemComponent::disable() { } } +void ModemComponent::reconnect() { + if (!this->internal_state_.reconnect) { + this->internal_state_.reconnect = true; + this->component_state_ = ModemComponentState::NOT_RESPONDING; + // if reconnect fail, let some time before retry + set_timeout(120000, [this]() { this->internal_state_.reconnect = false; }); + } else { + ESP_LOGD(TAG, "Reconnecting already in progress."); + } +} + network::IPAddresses ModemComponent::get_ip_addresses() { network::IPAddresses addresses; esp_netif_ip_info_t ip; @@ -233,7 +244,6 @@ void ModemComponent::loop() { static uint32_t last_health_check = millis(); static bool connecting = false; static uint8_t network_attach_retry = 10; - static uint8_t ip_lost_retries = 10; if ((millis() < next_loop_millis)) { // some commands need some delay @@ -380,7 +390,12 @@ void ModemComponent::loop() { if (!this->internal_state_.connected) { this->status_set_warning("Connection via Modem lost!"); this->component_state_ = ModemComponentState::DISCONNECTED; - } else if (this->cmux_ && (millis() - last_health_check) > 30000) { + break; + } + // clear flags if previously set by this->reconnect() + this->internal_state_.reconnect = false; + + if (this->cmux_ && (millis() - last_health_check) > 30000) { ESP_LOGD(TAG, "modem health check"); last_health_check = millis(); if (!this->get_imei()) { diff --git a/esphome/components/modem/modem_component.h b/esphome/components/modem/modem_component.h index d845b5132e..f2a67dec4d 100644 --- a/esphome/components/modem/modem_component.h +++ b/esphome/components/modem/modem_component.h @@ -77,6 +77,7 @@ class ModemComponent : public Component { bool modem_ready(bool force_check); void enable(); void disable(); + void reconnect(); network::IPAddresses get_ip_addresses(); std::string get_use_address() const; @@ -166,6 +167,8 @@ class ModemComponent : public Component { bool power_transition{false}; // states for triggering on/off signals ModemPowerState power_state{ModemPowerState::TOFFUART}; + // ask the modem to reconnect + bool reconnect{false}; }; InternalState internal_state_; };