diff --git a/esphome/components/modem/__init__.py b/esphome/components/modem/__init__.py index 5c17effc32..0f4a48edd6 100644 --- a/esphome/components/modem/__init__.py +++ b/esphome/components/modem/__init__.py @@ -100,8 +100,8 @@ async def to_code(config): if CORE.using_esp_idf: add_idf_sdkconfig_option("CONFIG_LWIP_PPP_SUPPORT", True) add_idf_sdkconfig_option("CONFIG_TASK_WDT_TIMEOUT_S", 60) - add_idf_sdkconfig_option("CONFIG_LOG_MAXIMUM_EQUALS_DEFAULT", False) - add_idf_sdkconfig_option("CONFIG_LOG_MAXIMUM_LEVEL_VERBOSE", True) + # add_idf_sdkconfig_option("CONFIG_LOG_MAXIMUM_EQUALS_DEFAULT", False) + # add_idf_sdkconfig_option("CONFIG_LOG_MAXIMUM_LEVEL_VERBOSE", True) add_idf_component( name="esp_modem", repo="https://github.com/espressif/esp-protocols.git", diff --git a/esphome/components/modem/modem_component.cpp b/esphome/components/modem/modem_component.cpp index e6add5bdc3..e969a6d327 100644 --- a/esphome/components/modem/modem_component.cpp +++ b/esphome/components/modem/modem_component.cpp @@ -105,13 +105,32 @@ void ModemComponent::loop() { } switch (this->state_) { case ModemComponentState::STOPPED: - this->turn_on_modem(); + this->set_state(ModemComponentState::TURNING_ON_POWER); break; case ModemComponentState::TURNING_ON_POWER: // time_check_pwrkey - this->turn_on_pwrkey(); + if (power_pin_) { + this->power_pin_->digital_write(true); + time_turn_on_modem = millis(); + ESP_LOGD(TAG, "Modem turn on"); + } else { + ESP_LOGD(TAG, "Can't turn on modem power pin because it is not configured, go to turn on pwrkey"); + this->turn_on_pwrkey(); + } + this->set_state(ModemComponentState::TURNING_ON_PWRKEY); this->dce_init(); break; case ModemComponentState::TURNING_ON_PWRKEY: + if (pwrkey_pin_) { + this->pwrkey_pin_->digital_write(false); + ESP_LOGD(TAG, "pwrkey turn on"); + } else { + ESP_LOGD(TAG, "Can't turn on pwrkey pin because it is not configured, go to reset modem"); + this->turn_on_reset(); // how do if reset dont work + break; + } + this->set_state(ModemComponentState::TURNING_OFF_PWRKEY); + break; + case ModemComponentState::TURNING_OFF_PWRKEY: this->turn_off_pwrkey(); break; case ModemComponentState::SYNC: @@ -150,7 +169,14 @@ void ModemComponent::loop() { } break; case ModemComponentState::TURNING_ON_RESET: - this->turn_off_reset(); + this->reset_pin_->digital_write(false); + this->set_state(ModemComponentState::TURNING_OFF_RESET); + break; + case ModemComponentState::TURNING_OFF_RESET: + this->reset_pin_->digital_write(true); + ESP_LOGD(TAG, "turn off reset"); + this->set_state(ModemComponentState::SYNC); + break; case ModemComponentState::TURNING_OFF_POWER: if (time_turn_off_modem + TURN_OFF_MODEM_TIME < now) { this->turn_on_modem(); @@ -189,6 +215,7 @@ bool ModemComponent::check_modem_component_state_timings() { const int now = millis(); ModemComponentStateTiming timing = this->modemComponentStateTimingMap[this->state_]; if (timing.time_limit && ((time_change_state + timing.time_limit) < now)) { + ESP_LOGE(TAG, "State time limit %s", this->state_to_string(this->state_)); this->turn_on_reset(); } if (!timing.poll_period) { @@ -203,7 +230,7 @@ bool ModemComponent::check_modem_component_state_timings() { } void ModemComponent::set_state(ModemComponentState state) { - ESP_LOGCONFIG(TAG, "Mode component change state from %s to %s", this->state_to_string(this->state_), + ESP_LOGCONFIG(TAG, "Modem component change state from %s to %s", this->state_to_string(this->state_), this->state_to_string(state)); this->state_ = state; time_change_state = millis(); @@ -217,6 +244,8 @@ const char *ModemComponent::state_to_string(ModemComponentState state) { return "TURNING_ON_POWER"; case ModemComponentState::TURNING_ON_PWRKEY: return "TURNING_ON_PWRKEY"; + case ModemComponentState::TURNING_OFF_PWRKEY: + return "TURNING_OFF_PWRKEY"; case ModemComponentState::SYNC: return "SYNC"; case ModemComponentState::REGISTRATION_IN_NETWORK: @@ -227,6 +256,8 @@ const char *ModemComponent::state_to_string(ModemComponentState state) { return "CONNECTED"; case ModemComponentState::TURNING_ON_RESET: return "TURNING_ON_RESET"; + case ModemComponentState::TURNING_OFF_RESET: + return "TURNING_OFF_RESET"; case ModemComponentState::TURNING_OFF_POWER: return "TURNING_OFF_POWER"; } @@ -234,15 +265,15 @@ const char *ModemComponent::state_to_string(ModemComponentState state) { } void ModemComponent::turn_on_modem() { - if (power_pin_) { - this->power_pin_->digital_write(true); - time_turn_on_modem = millis(); - ESP_LOGD(TAG, "Modem turn on"); - this->set_state(ModemComponentState::TURNING_ON_POWER); - } else { - ESP_LOGD(TAG, "Can't turn on modem power pin because it is not configured, go to turn on pwrkey"); - this->turn_on_pwrkey(); - } + // if (power_pin_) { + // this->power_pin_->digital_write(true); + // time_turn_on_modem = millis(); + // ESP_LOGD(TAG, "Modem turn on"); + // this->set_state(ModemComponentState::TURNING_ON_POWER); + // } else { + // ESP_LOGD(TAG, "Can't turn on modem power pin because it is not configured, go to turn on pwrkey"); + // this->turn_on_pwrkey(); + // } } void ModemComponent::turn_off_modem() { @@ -253,14 +284,14 @@ void ModemComponent::turn_off_modem() { } void ModemComponent::turn_on_pwrkey() { - if (pwrkey_pin_) { - this->pwrkey_pin_->digital_write(false); - ESP_LOGD(TAG, "pwrkey turn on"); - this->set_state(ModemComponentState::TURNING_ON_PWRKEY); - } else { - ESP_LOGD(TAG, "Can't turn on pwrkey pin because it is not configured, go to reset modem"); - this->turn_on_reset(); - } + // if (pwrkey_pin_) { + // this->pwrkey_pin_->digital_write(false); + // ESP_LOGD(TAG, "pwrkey turn on"); + // this->set_state(ModemComponentState::TURNING_ON_PWRKEY); + // } else { + // ESP_LOGD(TAG, "Can't turn on pwrkey pin because it is not configured, go to reset modem"); + // this->turn_on_reset(); + // } // vTaskDelay(pdMS_TO_TICKS(500)); // NOLINT } @@ -276,12 +307,6 @@ void ModemComponent::turn_on_reset() { this->set_state(ModemComponentState::TURNING_ON_RESET); } -void ModemComponent::turn_off_reset() { - this->reset_pin_->digital_write(true); - ESP_LOGD(TAG, "turn off reset"); - this->set_state(ModemComponentState::SYNC); -} - void ModemComponent::dump_config() { this->dump_connect_params(); ESP_LOGCONFIG(TAG, "Modem:"); @@ -343,7 +368,6 @@ network::IPAddress ModemComponent::get_ip_address() { void ModemComponent::got_ip_event_handler(void *arg, esp_event_base_t event_base, int event_id, void *event_data) { ESP_LOGD(TAG, "IP event! %" PRIu32, event_id); if (event_id == IP_EVENT_PPP_GOT_IP) { - global_modem_component->connected_ = true; global_modem_component->set_state(ModemComponentState::CONNECTED); esp_netif_dns_info_t dns_info; @@ -368,33 +392,6 @@ void ModemComponent::got_ip_event_handler(void *arg, esp_event_base_t event_base } } -void ModemComponent::start_connect_() { - this->connect_begin_ = millis(); - this->status_set_warning(); - turn_on_reset(); - esp_err_t err; - err = esp_netif_set_hostname(this->modem_netif_, App.get_name().c_str()); - if (err != ERR_OK) { - ESP_LOGD(TAG, "esp_netif_set_hostname failed: %s", esp_err_to_name(err)); - } - - // esp_netif_dhcp_status_t status = ESP_NETIF_DHCP_INIT; - - // restart ppp connection - this->dce->exit_data(); - int rssi, ber; - esp_modem::command_result errr = this->dce->get_signal_quality(rssi, ber); - // esp_err_t err = esp_modem::esp_modem_get_signal_quality(dce, &rssi, &ber); - if (errr != esp_modem::command_result::OK) { - ESP_LOGE(TAG, "esp_modem_get_signal_quality failed with"); - return; - } - ESP_LOGD(TAG, "Signal quality: rssi=%d, ber=%d", rssi, ber); - this->dce->set_data(); - - // this->status_set_warning(); -} - bool ModemComponent::is_connected() { return this->state_ == ModemComponentState::CONNECTED; } void ModemComponent::set_power_pin(InternalGPIOPin *power_pin) { this->power_pin_ = power_pin; } void ModemComponent::set_pwrkey_pin(InternalGPIOPin *pwrkey_pin) { this->pwrkey_pin_ = pwrkey_pin; } diff --git a/esphome/components/modem/modem_component.h b/esphome/components/modem/modem_component.h index e879b234d3..963387e0db 100644 --- a/esphome/components/modem/modem_component.h +++ b/esphome/components/modem/modem_component.h @@ -29,11 +29,13 @@ enum class ModemComponentState { STOPPED, TURNING_ON_POWER, TURNING_ON_PWRKEY, + TURNING_OFF_PWRKEY, SYNC, REGISTRATION_IN_NETWORK, CONNECTING, CONNECTED, TURNING_ON_RESET, + TURNING_OFF_RESET, TURNING_OFF_POWER, }; @@ -76,12 +78,14 @@ class ModemComponent : public Component { std::map modemComponentStateTimingMap = { {ModemComponentState::STOPPED, ModemComponentStateTiming(0, 0)}, {ModemComponentState::TURNING_ON_POWER, ModemComponentStateTiming(0, 0)}, - {ModemComponentState::TURNING_ON_PWRKEY, ModemComponentStateTiming(2000, 15000)}, + {ModemComponentState::TURNING_ON_PWRKEY, ModemComponentStateTiming(0, 0)}, + {ModemComponentState::TURNING_OFF_PWRKEY, ModemComponentStateTiming(2000, 0)}, {ModemComponentState::SYNC, ModemComponentStateTiming(2000, 15000)}, {ModemComponentState::REGISTRATION_IN_NETWORK, ModemComponentStateTiming(2000, 15000)}, {ModemComponentState::CONNECTING, ModemComponentStateTiming(2000, 15000)}, {ModemComponentState::CONNECTED, ModemComponentStateTiming(0, 0)}, - {ModemComponentState::TURNING_ON_RESET, ModemComponentStateTiming(110, 0)}, + {ModemComponentState::TURNING_ON_RESET, ModemComponentStateTiming(0, 0)}, + {ModemComponentState::TURNING_OFF_RESET, ModemComponentStateTiming(2000, 0)}, {ModemComponentState::TURNING_OFF_POWER, ModemComponentStateTiming(2000, 0)}, }; @@ -95,9 +99,7 @@ class ModemComponent : public Component { void turn_off_modem(); void turn_on_pwrkey(); void turn_off_pwrkey(); - void start_connect_(); void turn_on_reset(); - void turn_off_reset(); int get_rssi(); int get_modem_voltage(); const char *get_state(); @@ -124,7 +126,6 @@ class ModemComponent : public Component { int change_state_{0}; bool started_{false}; - bool connected_{false}; ModemComponentState state_{ModemComponentState::STOPPED}; int connect_begin_;