mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 07:03:55 +00:00 
			
		
		
		
	registration in network with minimal delays
This commit is contained in:
		| @@ -99,6 +99,7 @@ async def to_code(config): | |||||||
|         cg.add_library("WiFi", None) |         cg.add_library("WiFi", None) | ||||||
|     if CORE.using_esp_idf: |     if CORE.using_esp_idf: | ||||||
|         add_idf_sdkconfig_option("CONFIG_LWIP_PPP_SUPPORT", True) |         add_idf_sdkconfig_option("CONFIG_LWIP_PPP_SUPPORT", True) | ||||||
|  |         # add_idf_sdkconfig_option("CONFIG_TASK_WDT_TIMEOUT_S", 10) | ||||||
|         add_idf_component( |         add_idf_component( | ||||||
|             name="esp_modem", |             name="esp_modem", | ||||||
|             repo="https://github.com/espressif/esp-protocols.git", |             repo="https://github.com/espressif/esp-protocols.git", | ||||||
|   | |||||||
| @@ -4,11 +4,13 @@ | |||||||
| #include "esphome/core/util.h" | #include "esphome/core/util.h" | ||||||
| #include "esphome/core/application.h" | #include "esphome/core/application.h" | ||||||
|  |  | ||||||
| #include "esp_modem_c_api_types.h" |  | ||||||
| #include "esp_netif_ppp.h" |  | ||||||
|  |  | ||||||
| #ifdef USE_ESP32 | #ifdef USE_ESP32 | ||||||
|  |  | ||||||
|  | #include "esp_modem_c_api_types.h" | ||||||
|  | #include "esp_netif_ppp.h" | ||||||
|  | #include "cxx_include/esp_modem_types.hpp" | ||||||
|  |  | ||||||
| #include <cinttypes> | #include <cinttypes> | ||||||
| #include "driver/gpio.h" | #include "driver/gpio.h" | ||||||
| #include <lwip/dns.h> | #include <lwip/dns.h> | ||||||
| @@ -22,6 +24,7 @@ uint32_t time_turn_on_modem = 0; | |||||||
|  |  | ||||||
| #define TIME_TO_NEXT_HARD_RESET 30000 | #define TIME_TO_NEXT_HARD_RESET 30000 | ||||||
| #define TIME_TO_START_MODEM 9000 | #define TIME_TO_START_MODEM 9000 | ||||||
|  | #define TIME_CHECK_REGISTRATION_IN_NETWORK 1000 | ||||||
|  |  | ||||||
| namespace esphome { | namespace esphome { | ||||||
| namespace modem { | namespace modem { | ||||||
| @@ -108,35 +111,50 @@ void ModemComponent::loop() { | |||||||
|   const uint32_t now = millis(); |   const uint32_t now = millis(); | ||||||
|   switch (this->state_) { |   switch (this->state_) { | ||||||
|     case ModemComponentState::STOPPED: |     case ModemComponentState::STOPPED: | ||||||
|       if (time_check_rssi + TIME_TO_START_MODEM < now) { |       this->turn_on_modem(); | ||||||
|  |       // if (time_check_rssi + TIME_TO_START_MODEM < now) { | ||||||
|  |       //   time_check_rssi = now; | ||||||
|  |       //   // this->dce->set_command_mode(); | ||||||
|  |       //   if (get_rssi()) { | ||||||
|  |       //     ESP_LOGD(TAG, "Starting modem connection"); | ||||||
|  |       //     ESP_LOGD(TAG, "SIgnal quality: rssi=%d", get_rssi()); | ||||||
|  |       //     this->state_ = ModemComponentState::CONNECTING; | ||||||
|  |       //     this->dce->set_data(); | ||||||
|  |       //     // this->start_connect_(); | ||||||
|  |       //   } | ||||||
|  |       //   if (time_hard_reset_modem + TIME_TO_NEXT_HARD_RESET < now) { | ||||||
|  |       //     time_hard_reset_modem = now; | ||||||
|  |       //     reset_modem(); | ||||||
|  |       //   } | ||||||
|  |       // } | ||||||
|  |       break; | ||||||
|  |     case ModemComponentState::TURNING_ON: | ||||||
|  |       if (this->dce->sync() != esp_modem::command_result::OK){ | ||||||
|  |         ESP_LOGD(TAG, "sync OK"); | ||||||
|  |         this->state_ = ModemComponentState::REGISTRATION_IN_NETWORK; | ||||||
|  |       } | ||||||
|  |       break; | ||||||
|  |     case ModemComponentState::REGISTRATION_IN_NETWORK: | ||||||
|  |       if (time_check_rssi + TIME_CHECK_REGISTRATION_IN_NETWORK < now){ | ||||||
|         time_check_rssi = now; |         time_check_rssi = now; | ||||||
|         // this->dce->set_command_mode(); |  | ||||||
|         if (get_rssi()) { |         if (get_rssi()) { | ||||||
|           ESP_LOGD(TAG, "Starting modem connection"); |           ESP_LOGD(TAG, "Starting modem connection"); | ||||||
|           ESP_LOGD(TAG, "SIgnal quality: rssi=%d", get_rssi()); |           ESP_LOGD(TAG, "SIgnal quality: rssi=%d", get_rssi()); | ||||||
|           this->state_ = ModemComponentState::CONNECTING; |           this->state_ = ModemComponentState::CONNECTING; | ||||||
|           this->dce->set_data(); |           this->dce->set_data(); | ||||||
|           // this->start_connect_(); |           // this->start_connect_(); | ||||||
|         } |         } else { | ||||||
|         if (time_hard_reset_modem + TIME_TO_NEXT_HARD_RESET < now) { |           ESP_LOGD(TAG, "Wait RSSI"); | ||||||
|           time_hard_reset_modem = now; |  | ||||||
|           reset_modem(); |  | ||||||
|         } |         } | ||||||
|       } |       } | ||||||
|       break; |       break; | ||||||
|     case ModemComponentState::RESETTING: |  | ||||||
|       break; |  | ||||||
|     case ModemComponentState::TURNING_ON: |  | ||||||
|  |  | ||||||
|       break; |  | ||||||
|     case ModemComponentState::TURNING_OFF: |  | ||||||
|       break; |  | ||||||
|     case ModemComponentState::CONNECTING: |     case ModemComponentState::CONNECTING: | ||||||
|       break; |       break; | ||||||
|     case ModemComponentState::CONNECTED: |     case ModemComponentState::CONNECTED: | ||||||
|       if (time_info_print < now) { |       if (time_info_print < now) { | ||||||
|         // ESP_LOGI(TAG, "voltage %dV.", get_modem_voltage() / 1000); |         // ESP_LOGI(TAG, "voltage %dV.", get_modem_voltage() / 1000); | ||||||
|         if (esp_netif_is_netif_up(this->modem_netif_)) { |         if (esp_netif_is_netif_up(this->modem_netif_)) { | ||||||
|  |            | ||||||
|           ESP_LOGD(TAG, "esp_netif_is_netif_UP"); |           ESP_LOGD(TAG, "esp_netif_is_netif_UP"); | ||||||
|         } else { |         } else { | ||||||
|           ESP_LOGD(TAG, "esp_netif_is_netif_DOWN"); |           ESP_LOGD(TAG, "esp_netif_is_netif_DOWN"); | ||||||
| @@ -144,6 +162,10 @@ void ModemComponent::loop() { | |||||||
|         time_info_print = now + 5000; |         time_info_print = now + 5000; | ||||||
|       } |       } | ||||||
|       break; |       break; | ||||||
|  |     case ModemComponentState::RESETTING: | ||||||
|  |       break; | ||||||
|  |     case ModemComponentState::TURNING_OFF: | ||||||
|  |       break; | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -152,6 +174,7 @@ bool ModemComponent::turn_on_modem() { | |||||||
|     this->power_pin_->digital_write(true); |     this->power_pin_->digital_write(true); | ||||||
|     time_turn_on_modem = millis(); |     time_turn_on_modem = millis(); | ||||||
|     vTaskDelay(pdMS_TO_TICKS(1900));  // NOLINT |     vTaskDelay(pdMS_TO_TICKS(1900));  // NOLINT | ||||||
|  |     this->state_ = ModemComponentState::TURNING_ON; | ||||||
|     ESP_LOGD(TAG, "modem is on"); |     ESP_LOGD(TAG, "modem is on"); | ||||||
|     return true; |     return true; | ||||||
|   } else { |   } else { | ||||||
|   | |||||||
| @@ -6,6 +6,7 @@ | |||||||
| #include "esphome/core/hal.h" | #include "esphome/core/hal.h" | ||||||
| #include "esphome/components/network/ip_address.h" | #include "esphome/components/network/ip_address.h" | ||||||
|  |  | ||||||
|  |  | ||||||
| #ifdef USE_ESP32 | #ifdef USE_ESP32 | ||||||
|  |  | ||||||
| using esphome::esp_log_printf_; | using esphome::esp_log_printf_; | ||||||
| @@ -26,11 +27,12 @@ enum ModemType { | |||||||
|  |  | ||||||
| enum class ModemComponentState { | enum class ModemComponentState { | ||||||
|   STOPPED, |   STOPPED, | ||||||
|   RESETTING, |  | ||||||
|   TURNING_ON, |   TURNING_ON, | ||||||
|   TURNING_OFF, |   REGISTRATION_IN_NETWORK, | ||||||
|   CONNECTING, |   CONNECTING, | ||||||
|   CONNECTED, |   CONNECTED, | ||||||
|  |   RESETTING, | ||||||
|  |   TURNING_OFF, | ||||||
| }; | }; | ||||||
|  |  | ||||||
| class ModemComponent : public Component { | class ModemComponent : public Component { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user