From ead5995f13fdd465f3a61339a22c5cc3d4e173d9 Mon Sep 17 00:00:00 2001 From: Alex Pyslar Date: Tue, 22 Oct 2024 12:52:41 +0300 Subject: [PATCH] get modem voltage and ber and cmux --- esphome/components/modem/modem_component.cpp | 56 ++++++++++++++------ esphome/components/modem/modem_component.h | 10 ++-- 2 files changed, 44 insertions(+), 22 deletions(-) diff --git a/esphome/components/modem/modem_component.cpp b/esphome/components/modem/modem_component.cpp index 2ac7aea5d0..68c9cf3974 100644 --- a/esphome/components/modem/modem_component.cpp +++ b/esphome/components/modem/modem_component.cpp @@ -69,8 +69,6 @@ void ModemComponent::setup() { ESP_LOGD(TAG, "Initializing esp_modem"); this->modem_netif_init_(); this->dte_init_(); - - this->started_ = true; } void ModemComponent::loop() { @@ -154,11 +152,12 @@ void ModemComponent::loop() { // The state of waiting for the modem to register in the network case ModemComponentState::REGISTRATION_IN_NETWORK: - if (get_rssi_()) { + if (get_rssi()) { 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->set_state_(ModemComponentState::CONNECTING); - this->dce_->set_data(); + // this->dce_->set_data(); + this->dce_->set_mode(esp_modem::modem_mode::CMUX_MODE); } else { ESP_LOGD(TAG, "Wait RSSI"); } @@ -172,11 +171,7 @@ void ModemComponent::loop() { // The state of network connection established case ModemComponentState::CONNECTED: - if (esp_netif_is_netif_up(this->modem_netif_)) { - ESP_LOGD(TAG, "esp_netif_is_netif_UP"); - } else { - ESP_LOGD(TAG, "esp_netif_is_netif_DOWN"); - } + ESP_LOGD(TAG, "The modem works!"); break; default: @@ -234,6 +229,7 @@ void ModemComponent::set_state_(ModemComponentState state) { switch (state) { case ModemComponentState::SYNC: this->dce_init_(); + this->started = true; break; default: @@ -302,14 +298,40 @@ void ModemComponent::dump_connect_params() { ESP_LOGCONFIG(TAG, " DNS2: %s", network::IPAddress(&dns_info.ip.u_addr.ip4).str().c_str()); } -int ModemComponent::get_rssi_() { - int rssi = 0, ber = 0; - 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"); +int ModemComponent::get_rssi() { + if (this->started) { + int rssi = 0, ber = 0; + esp_modem::command_result errr = this->dce_->get_signal_quality(rssi, ber); + if (errr != esp_modem::command_result::OK) { + ESP_LOGE(TAG, "esp_modem_get_signal_quality failed with"); + } + return rssi; } - return rssi; + return 0; +} + +int ModemComponent::get_ber() { + if (this->started) { + int rssi = 0, ber = 0; + esp_modem::command_result errr = this->dce_->get_signal_quality(rssi, ber); + if (errr != esp_modem::command_result::OK) { + ESP_LOGE(TAG, "esp_modem_get_signal_quality failed with"); + } + return ber; + } + return 0; +} + +int ModemComponent::get_modem_voltage() { + if (this->started) { + int milli_volt = 0, bcs = 0, bcl = 0; + esp_modem::command_result errr = this->dce_->get_battery_status(milli_volt, bcs, bcl); + if (errr != esp_modem::command_result::OK) { + ESP_LOGE(TAG, "esp_modem_get_modem_voltage failed with"); + } + return milli_volt; + } + return 0; } float ModemComponent::get_setup_priority() const { return setup_priority::MODEM; } diff --git a/esphome/components/modem/modem_component.h b/esphome/components/modem/modem_component.h index 7b3cd5f026..a29a1a78b1 100644 --- a/esphome/components/modem/modem_component.h +++ b/esphome/components/modem/modem_component.h @@ -9,7 +9,7 @@ #include "esphome/components/network/ip_address.h" #include -using esphome::esp_log_printf_; +using esphome::esp_log_printf_; // NOLINT #include "esp_netif.h" #include "cxx_include/esp_modem_api.hpp" @@ -55,6 +55,7 @@ class ModemComponent : public Component { float get_setup_priority() const override; bool can_proceed() override; bool is_connected(); + bool started{false}; void set_power_pin(InternalGPIOPin *power_pin); void set_pwrkey_pin(InternalGPIOPin *pwrkey_pin); void set_type(ModemType type); @@ -67,6 +68,9 @@ class ModemComponent : public Component { void set_uart_event_queue_size(int uart_event_queue_size); void set_uart_tx_buffer_size(int uart_tx_buffer_size); void set_uart_rx_buffer_size(int uart_rx_buffer_size); + int get_rssi(); + int get_ber(); + int get_modem_voltage(); network::IPAddress get_ip_address(); std::string get_use_address() const; @@ -93,8 +97,6 @@ class ModemComponent : public Component { void dce_init_(); bool check_modem_component_state_timings_(); - int get_rssi_(); - int get_modem_voltage_(); const char *get_state_(); void set_state_(ModemComponentState state); const char *state_to_string_(ModemComponentState state); @@ -118,8 +120,6 @@ class ModemComponent : public Component { uint pull_time_{0}; uint change_state_{0}; - bool started_{false}; - ModemComponentState state_{ModemComponentState::TURNING_ON_POWER}; int connect_begin_; esp_netif_t *modem_netif_{nullptr};