mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 15:12:06 +00:00 
			
		
		
		
	improved sync
This commit is contained in:
		| @@ -336,12 +336,8 @@ void ModemComponent::loop() { | |||||||
|               network_attach_retry--; |               network_attach_retry--; | ||||||
|               if (network_attach_retry == 0) { |               if (network_attach_retry == 0) { | ||||||
|                 ESP_LOGE(TAG, "modem is unable to attach to a network"); |                 ESP_LOGE(TAG, "modem is unable to attach to a network"); | ||||||
|                 if (this->power_pin_) { |                 network_attach_retry = 10; | ||||||
|                   this->poweroff_(); |                 this->component_state_ = ModemComponentState::NOT_RESPONDING; | ||||||
|                 } else { |  | ||||||
|                   network_attach_retry = 10; |  | ||||||
|                   this->component_state_ = ModemComponentState::NOT_RESPONDING; |  | ||||||
|                 } |  | ||||||
|               } |               } | ||||||
|               next_loop_millis = millis() + 1000;  // delay to retry |               next_loop_millis = millis() + 1000;  // delay to retry | ||||||
|             } |             } | ||||||
| @@ -392,8 +388,6 @@ void ModemComponent::loop() { | |||||||
|     case ModemComponentState::DISABLED: |     case ModemComponentState::DISABLED: | ||||||
|       if (this->internal_state_.enabled) { |       if (this->internal_state_.enabled) { | ||||||
|         this->component_state_ = ModemComponentState::DISCONNECTED; |         this->component_state_ = ModemComponentState::DISCONNECTED; | ||||||
|       } else if (this->internal_state_.powered_on) { |  | ||||||
|         this->poweroff_(); |  | ||||||
|       } |       } | ||||||
|       next_loop_millis = millis() + 2000;  // delay for next loop |       next_loop_millis = millis() + 2000;  // delay for next loop | ||||||
|       break; |       break; | ||||||
| @@ -428,8 +422,8 @@ void ModemComponent::modem_lazy_init_() { | |||||||
|  |  | ||||||
|   this->dte_ = create_uart_dte(&dte_config); |   this->dte_ = create_uart_dte(&dte_config); | ||||||
|  |  | ||||||
|   if (this->dte_->set_mode(modem_mode::COMMAND_MODE)) { |   if (!this->dte_->set_mode(modem_mode::COMMAND_MODE)) { | ||||||
|     ESP_LOGD(TAG, "dte in command mode"); |     ESP_LOGW(TAG, "Unable to set DTE in command mode."); | ||||||
|   } |   } | ||||||
|   esp_modem_dce_config_t dce_config = ESP_MODEM_DCE_DEFAULT_CONFIG(this->apn_.c_str()); |   esp_modem_dce_config_t dce_config = ESP_MODEM_DCE_DEFAULT_CONFIG(this->apn_.c_str()); | ||||||
|  |  | ||||||
| @@ -478,13 +472,12 @@ bool ModemComponent::modem_sync_() { | |||||||
|     auto command_mode = [this]() -> bool { |     auto command_mode = [this]() -> bool { | ||||||
|       ESP_LOGVV(TAG, "trying command mode"); |       ESP_LOGVV(TAG, "trying command mode"); | ||||||
|       this->dce->set_mode(modem_mode::UNDEF); |       this->dce->set_mode(modem_mode::UNDEF); | ||||||
|       return this->dce->set_mode(modem_mode::COMMAND_MODE) && this->get_imei(); |       return this->dce->set_mode(modem_mode::COMMAND_MODE); | ||||||
|     }; |     }; | ||||||
|  |  | ||||||
|     auto cmux_command_mode = [this]() -> bool { |     auto cmux_command_mode = [this]() -> bool { | ||||||
|       ESP_LOGVV(TAG, "trying cmux command mode"); |       ESP_LOGVV(TAG, "trying cmux command mode"); | ||||||
|       return this->dce->set_mode(modem_mode::CMUX_MANUAL_MODE) && |       return this->dce->set_mode(modem_mode::CMUX_MANUAL_MODE) && this->dce->set_mode(modem_mode::CMUX_MANUAL_COMMAND); | ||||||
|              this->dce->set_mode(modem_mode::CMUX_MANUAL_COMMAND) && this->get_imei(); |  | ||||||
|     }; |     }; | ||||||
|  |  | ||||||
|     // The cmux state is supposed to be the same before the reboot. But if it has changed (new firwmare), we will try |     // The cmux state is supposed to be the same before the reboot. But if it has changed (new firwmare), we will try | ||||||
| @@ -503,18 +496,26 @@ bool ModemComponent::modem_sync_() { | |||||||
|       this->internal_state_.powered_on = false; |       this->internal_state_.powered_on = false; | ||||||
|     } else { |     } else { | ||||||
|       ESP_LOGD(TAG, "Connected to the modem in %" PRIu32 "ms", elapsed_ms); |       ESP_LOGD(TAG, "Connected to the modem in %" PRIu32 "ms", elapsed_ms); | ||||||
|  |       delay(2000);  // NOLINT | ||||||
|       this->internal_state_.powered_on = true; |       this->internal_state_.powered_on = true; | ||||||
|     } |     } | ||||||
|   } else { |   } else { | ||||||
|     // modem responded without need to recover command mode |     // modem responded without need to recover command mode | ||||||
|  |     ESP_LOGD(TAG, "Modem already synced"); | ||||||
|     this->internal_state_.powered_on = true; |     this->internal_state_.powered_on = true; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   if (status && !this->internal_state_.modem_synced) { |   if (status && !this->internal_state_.modem_synced) { | ||||||
|  |     this->internal_state_.modem_synced = true; | ||||||
|  |  | ||||||
|     // First time the modem is synced, or modem recovered |     // First time the modem is synced, or modem recovered | ||||||
|     App.feed_wdt(); |     App.feed_wdt(); | ||||||
|     watchdog::WatchdogManager wdt(30000); |     watchdog::WatchdogManager wdt(30000); | ||||||
|     this->internal_state_.modem_synced = true; |     delay(2000);  // NOLINT | ||||||
|  |     if (!this->get_imei()) { | ||||||
|  |       ESP_LOGW(TAG, "Unable to sync modem"); | ||||||
|  |       return false; | ||||||
|  |     } | ||||||
|  |  | ||||||
|     if (!this->prepare_sim_()) { |     if (!this->prepare_sim_()) { | ||||||
|       // fatal error |       // fatal error | ||||||
| @@ -544,6 +545,8 @@ bool ModemComponent::prepare_sim_() { | |||||||
|     return false; |     return false; | ||||||
|   } |   } | ||||||
|  |  | ||||||
|  |   delay(this->command_delay_); | ||||||
|  |  | ||||||
|   if (!pin_ok) { |   if (!pin_ok) { | ||||||
|     if (!this->pin_code_.empty()) { |     if (!this->pin_code_.empty()) { | ||||||
|       ESP_LOGV(TAG, "Set pin code: %s", this->pin_code_.c_str()); |       ESP_LOGV(TAG, "Set pin code: %s", this->pin_code_.c_str()); | ||||||
| @@ -671,6 +674,9 @@ void ModemComponent::poweroff_() { | |||||||
|   if (this->power_pin_) { |   if (this->power_pin_) { | ||||||
|     this->internal_state_.power_state = ModemPowerState::TOFF; |     this->internal_state_.power_state = ModemPowerState::TOFF; | ||||||
|     this->internal_state_.power_transition = true; |     this->internal_state_.power_transition = true; | ||||||
|  |   } else { | ||||||
|  |     // This will powercycle the modem | ||||||
|  |     this->send_at("AT+CPOF"); | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user