wait for network attached before trying to connect

This commit is contained in:
oarcher 2024-07-31 20:53:06 +02:00
parent 4e6da9f34d
commit 4f74cec977
2 changed files with 34 additions and 21 deletions

View file

@ -146,6 +146,7 @@ void ModemComponent::setup() {
// this->poweron_(); // this->poweron_();
//} //}
// App.feed_wdt(); // App.feed_wdt();
ESP_LOGV(TAG, "Setup finished"); ESP_LOGV(TAG, "Setup finished");
} }
@ -234,13 +235,18 @@ void ModemComponent::create_dte_dce_() {
uint32_t elapsed_ms = millis() - start_ms; uint32_t elapsed_ms = millis() - start_ms;
if (!status) { if (!status) {
ESP_LOGW(TAG, "modem not responding after %" PRIu32 "ms"); ESP_LOGW(TAG, "modem not responding after %" PRIu32 "ms", elapsed_ms);
if (this->power_pin_) { if (this->power_pin_) {
ESP_LOGD(TAG, "Trying to power cycle the modem"); ESP_LOGD(TAG, "Trying to power cycle the modem");
this->poweroff_(); this->poweroff_();
} }
} else { } else {
ESP_LOGD(TAG, "Connected to the modem in %" PRIu32 "ms"); ESP_LOGD(TAG, "Connected to the modem in %" PRIu32 "ms", elapsed_ms);
this->send_init_at_();
if (!this->prepare_sim_()) {
// fatal error
this->disable();
}
} }
} }
} }
@ -354,14 +360,14 @@ void ModemComponent::ip_event_handler(void *arg, esp_event_base_t event_base, in
void ModemComponent::loop() { void ModemComponent::loop() {
static ModemComponentState last_state = this->state_; static ModemComponentState last_state = this->state_;
if (this->power_transition_) { static uint32_t next_loop_millis = millis();
// No loop on power transition
if (this->power_transition_ || (millis() < next_loop_millis)) {
// No loop on power transition, or if some commands need some delay
yield(); yield();
return; return;
} }
const uint32_t now = millis();
switch (this->state_) { switch (this->state_) {
case ModemComponentState::NOT_RESPONDING: case ModemComponentState::NOT_RESPONDING:
if (this->start_) { if (this->start_) {
@ -389,22 +395,19 @@ void ModemComponent::loop() {
case ModemComponentState::DISCONNECTED: case ModemComponentState::DISCONNECTED:
if (this->enabled_) { if (this->enabled_) {
if (this->start_) { if (this->start_) {
if (this->modem_ready()) { if (is_network_attached_()) {
this->send_init_at_(); if (this->start_connect_()) {
if (this->prepare_sim_()) { this->state_ = ModemComponentState::CONNECTING;
ESP_LOGI(TAG, "Starting modem connection"); } else if (!this->modem_ready()) {
if (this->start_connect_()) { if (this->power_pin_) {
this->state_ = ModemComponentState::CONNECTING; this->poweron_();
} else {
this->state_ = ModemComponentState::NOT_RESPONDING;
} }
} else {
this->disable();
} }
} else if (!this->get_power_status()) { } else
this->poweron_(); ESP_LOGD(TAG, "Waiting for the modem to be attached to a network");
} else { next_loop_millis = millis() + 1000; // delay to retry
this->state_ = ModemComponentState::NOT_RESPONDING;
}
} else { } else {
this->start_ = true; this->start_ = true;
} }
@ -426,7 +429,7 @@ void ModemComponent::loop() {
this->status_clear_warning(); this->status_clear_warning();
this->watchdog_.reset(); this->watchdog_.reset();
} else if (now - this->connect_begin_ > 45000) { } else if (millis() - this->connect_begin_ > 45000) {
ESP_LOGW(TAG, "Connecting via Modem failed! Re-connecting..."); ESP_LOGW(TAG, "Connecting via Modem failed! Re-connecting...");
this->state_ = ModemComponentState::DISCONNECTED; this->state_ = ModemComponentState::DISCONNECTED;
} else { } else {
@ -616,6 +619,15 @@ void ModemComponent::dump_connect_params_() {
ESP_LOGCONFIG(TAG, " DNS fallback: %s", network::IPAddress(dns_fallback_ip).str().c_str()); ESP_LOGCONFIG(TAG, " DNS fallback: %s", network::IPAddress(dns_fallback_ip).str().c_str());
} }
bool ModemComponent::is_network_attached_() {
int network_attachment_state;
command_result err = this->dce->get_network_attachment_state(network_attachment_state);
if (err == command_result::OK) {
return network_attachment_state;
} else
return false;
}
void ModemComponent::dump_dce_status_() { void ModemComponent::dump_dce_status_() {
ESP_LOGCONFIG(TAG, "Modem status:"); ESP_LOGCONFIG(TAG, "Modem status:");
int network_attachment_state; int network_attachment_state;

View file

@ -76,6 +76,7 @@ class ModemComponent : public Component {
void create_dte_dce_(); // (re)create dte and dce void create_dte_dce_(); // (re)create dte and dce
bool prepare_sim_(); bool prepare_sim_();
void send_init_at_(); void send_init_at_();
bool is_network_attached_();
bool start_connect_(); bool start_connect_();
void poweron_(); void poweron_();
void poweroff_(); void poweroff_();