diff --git a/esphome/components/modem/__init__.py b/esphome/components/modem/__init__.py index fb5b7cc9fe..edda7753df 100644 --- a/esphome/components/modem/__init__.py +++ b/esphome/components/modem/__init__.py @@ -197,17 +197,16 @@ async def to_code(config): cg.add(var.add_init_at_command(cmd)) modem_model = config[CONF_MODEL] - cg.add_define("USE_MODEM_MODEL", modem_model) - cg.add_define(f"USE_MODEM_MODEL_{modem_model}") + cg.add(var.set_model(modem_model)) if config[CONF_ENABLE_GNSS]: - cg.add_define("USE_MODEM_GNSS") cg.add(var.set_gnss_power_command(MODEM_MODELS_GNSS_POWER[modem_model])) if power_spec := MODEM_MODELS_POWER.get(modem_model, None): - cg.add_define("USE_MODEM_POWER") - for spec, value in power_spec.items(): - cg.add_define(f"USE_MODEM_POWER_{spec.upper()}", value) + cg.add(var.set_power_ton(power_spec["ton"])) + cg.add(var.set_power_tonuart(power_spec["tonuart"])) + cg.add(var.set_power_toff(power_spec["toff"])) + cg.add(var.set_power_toffuart(power_spec["toffuart"])) cg.add(var.set_apn(config[CONF_APN])) @@ -220,7 +219,6 @@ async def to_code(config): if status_pin := config.get(CONF_STATUS_PIN, None): pin = await cg.gpio_pin_expression(status_pin) cg.add(var.set_status_pin(pin)) - cg.add_define("USE_MODEM_STATUS") if power_pin := config.get(CONF_POWER_PIN, None): pin = await cg.gpio_pin_expression(power_pin) diff --git a/esphome/components/modem/modem_component.cpp b/esphome/components/modem/modem_component.cpp index d6eb35ba3a..6c94241099 100644 --- a/esphome/components/modem/modem_component.cpp +++ b/esphome/components/modem/modem_component.cpp @@ -22,11 +22,6 @@ #include #include -#ifndef USE_MODEM_MODEL -#define USE_MODEM_MODEL "GENERIC" -#define USE_MODEM_MODEL_GENERIC -#endif - #define ESPHL_ERROR_CHECK(err, message) \ if ((err) != ESP_OK) { \ ESP_LOGE(TAG, message ": (%d) %s", err, esp_err_to_name(err)); \ @@ -188,7 +183,7 @@ void ModemComponent::setup() { } ESP_LOGCONFIG(TAG, "Config Modem:"); - ESP_LOGCONFIG(TAG, " Model : %s", USE_MODEM_MODEL); + ESP_LOGCONFIG(TAG, " Model : %s", this->model_.c_str()); ESP_LOGCONFIG(TAG, " APN : %s", this->apn_.c_str()); ESP_LOGCONFIG(TAG, " PIN code : %s", (this->pin_code_.empty()) ? "No" : "Yes (not shown)"); ESP_LOGCONFIG(TAG, " Tx Pin : GPIO%u", this->tx_pin_->get_pin()); @@ -246,7 +241,6 @@ void ModemComponent::loop() { return; } -#ifdef USE_MODEM_POWER if (this->internal_state_.power_transition) { watchdog::WatchdogManager wdt(30000); @@ -254,11 +248,11 @@ void ModemComponent::loop() { switch (this->internal_state_.power_state) { case ModemPowerState::TON: this->power_pin_->digital_write(false); - delay(USE_MODEM_POWER_TON); + delay(this->power_ton_); this->power_pin_->digital_write(true); - next_loop_millis = millis() + USE_MODEM_POWER_TONUART; // delay for next loop + next_loop_millis = millis() + this->power_tonuart_; // delay for next loop this->internal_state_.power_state = ModemPowerState::TONUART; - ESP_LOGD(TAG, "Will check that the modem is on in %.1fs...", float(USE_MODEM_POWER_TONUART) / 1000); + ESP_LOGD(TAG, "Will check that the modem is on in %.1fs...", float(this->power_tonuart_) / 1000); break; case ModemPowerState::TONUART: this->internal_state_.power_transition = false; @@ -274,11 +268,11 @@ void ModemComponent::loop() { case ModemPowerState::TOFF: delay(10); this->power_pin_->digital_write(false); - delay(USE_MODEM_POWER_TOFF); + delay(this->power_toff_); this->power_pin_->digital_write(true); this->internal_state_.power_state = ModemPowerState::TOFFUART; - ESP_LOGD(TAG, "Will check that the modem is off in %.1fs...", float(USE_MODEM_POWER_TOFFUART) / 1000); - next_loop_millis = millis() + USE_MODEM_POWER_TOFFUART; // delay for next loop + ESP_LOGD(TAG, "Will check that the modem is off in %.1fs...", float(this->power_toffuart_) / 1000); + next_loop_millis = millis() + this->power_toffuart_; // delay for next loop break; case ModemPowerState::TOFFUART: this->internal_state_.power_transition = false; @@ -296,7 +290,6 @@ void ModemComponent::loop() { yield(); return; } -#endif // USE_MODEM_POWER switch (this->component_state_) { case ModemComponentState::NOT_RESPONDING: @@ -477,19 +470,21 @@ void ModemComponent::modem_lazy_init_() { } esp_modem_dce_config_t dce_config = ESP_MODEM_DCE_DEFAULT_CONFIG(this->apn_.c_str()); -#if defined(USE_MODEM_MODEL_GENERIC) - this->dce = create_generic_dce(&dce_config, this->dte_, this->ppp_netif_); -#elif defined(USE_MODEM_MODEL_BG96) - this->dce = create_BG96_dce(&dce_config, this->dte_, this->ppp_netif_); -#elif defined(USE_MODEM_MODEL_SIM800) - this->dce = create_SIM800_dce(&dce_config, this->dte_, this->ppp_netif_); -#elif defined(USE_MODEM_MODEL_SIM7000) - this->dce = create_SIM7000_dce(&dce_config, this->dte_, this->ppp_netif_); -#elif defined(USE_MODEM_MODEL_SIM7600) || defined(USE_MODEM_MODEL_SIM7670) - this->dce = create_SIM7600_dce(&dce_config, this->dte_, this->ppp_netif_); -#else -#error Modem model not known -#endif + if (this->model_ == "GENERIC") { + this->dce = create_generic_dce(&dce_config, this->dte_, this->ppp_netif_); + } else if (this->model_ == "BG96") { + this->dce = create_BG96_dce(&dce_config, this->dte_, this->ppp_netif_); + } else if (this->model_ == "SIM800") { + this->dce = create_SIM800_dce(&dce_config, this->dte_, this->ppp_netif_); + } else if (this->model_ == "SIM7000") { + this->dce = create_SIM7000_dce(&dce_config, this->dte_, this->ppp_netif_); + } else if (this->model_ == "SIM7600" || this->model_ == "SIM7670") { + this->dce = create_SIM7600_dce(&dce_config, this->dte_, this->ppp_netif_); + } else { + ESP_LOGE(TAG, "Invalid model %s", this->model_.c_str()); + return; + } + // flow control not fully implemented, but kept here for future work // if (dte_config.uart_config.flow_control == ESP_MODEM_FLOW_CONTROL_HW) { // if (command_result::OK != this->dce->set_flow_control(2, 2)) { @@ -708,23 +703,23 @@ void ModemComponent::ip_event_handler(void *arg, esp_event_base_t event_base, in } void ModemComponent::poweron_() { -#ifdef USE_MODEM_POWER - this->internal_state_.power_state = ModemPowerState::TON; - this->internal_state_.power_transition = true; -#else - if (this->modem_ready()) { - ESP_LOGV(TAG, "Modem is already ON"); + if (this->power_pin_) { + this->internal_state_.power_state = ModemPowerState::TON; + this->internal_state_.power_transition = true; } else { - ESP_LOGW(TAG, "No 'power_pin' defined: Not able to poweron the modem"); + if (this->modem_ready()) { + ESP_LOGV(TAG, "Modem is already ON"); + } else { + ESP_LOGW(TAG, "No 'power_pin' defined: Not able to poweron the modem"); + } } -#endif // USE_MODEM_POWER } void ModemComponent::poweroff_() { -#ifdef USE_MODEM_POWER - this->internal_state_.power_state = ModemPowerState::TOFF; - this->internal_state_.power_transition = true; -#endif // USE_MODEM_POWER + if (this->power_pin_) { + this->internal_state_.power_state = ModemPowerState::TOFF; + this->internal_state_.power_transition = true; + } } void ModemComponent::dump_connect_params_() { diff --git a/esphome/components/modem/modem_component.h b/esphome/components/modem/modem_component.h index ee13f0021a..c3c8238f0e 100644 --- a/esphome/components/modem/modem_component.h +++ b/esphome/components/modem/modem_component.h @@ -11,7 +11,6 @@ // esp_modem will use esphome logger (needed if other components include esphome/core/log.h) // We need to do this because "cxx_include/esp_modem_api.hpp" is not a pure C++ header, and use logging. -// FIXME: Find another workaround ?. // error: using declarations in the global namespace in headers are prohibited // [google-global-names-in-headers,-warnings-as-errors] using esphome::esp_log_printf_; // NOLINT(google-global-names-in-headers): @@ -38,27 +37,31 @@ enum class ModemComponentState { DISABLED, }; -#ifdef USE_MODEM_POWER enum class ModemPowerState { TON, TONUART, TOFF, TOFFUART, }; -#endif // USE_MODEM_POWER class ModemComponent : public Component { public: void set_use_address(const std::string &use_address) { this->use_address_ = use_address; } void set_rx_pin(InternalGPIOPin *rx_pin) { this->rx_pin_ = rx_pin; } void set_tx_pin(InternalGPIOPin *tx_pin) { this->tx_pin_ = tx_pin; } + void set_model(const std::string model) { this->model_ = model; } void set_power_pin(GPIOPin *power_pin) { this->power_pin_ = power_pin; } + void set_power_ton(int ton) { this->power_ton_ = ton; } + void set_power_tonuart(int tonuart) { this->power_tonuart_ = tonuart; } + void set_power_toff(int toff) { this->power_toff_ = toff; } + void set_power_toffuart(int toffuart) { this->power_toffuart_ = toffuart; } void set_status_pin(GPIOPin *status_pin) { this->status_pin_ = status_pin; } void set_username(const std::string &username) { this->username_ = username; } void set_password(const std::string &password) { this->password_ = password; } void set_pin_code(const std::string &pin_code) { this->pin_code_ = pin_code; } void set_apn(const std::string &apn) { this->apn_ = apn; } void set_gnss_power_command(const std::string &at_command) { this->gnss_power_command_ = at_command; } + std::string get_gnss_power_command() { return this->gnss_power_command_; } void set_not_responding_cb(Trigger<> *not_responding_cb) { this->not_responding_cb_ = not_responding_cb; } void enable_cmux() { this->cmux_ = true; } void enable_debug(); @@ -113,7 +116,12 @@ class ModemComponent : public Component { // Attributes from yaml config InternalGPIOPin *tx_pin_; InternalGPIOPin *rx_pin_; + std::string model_; GPIOPin *status_pin_{nullptr}; + int power_ton_; + int power_tonuart_; + int power_toff_; + int power_toffuart_; GPIOPin *power_pin_{nullptr}; std::string pin_code_; std::string username_; @@ -155,12 +163,10 @@ class ModemComponent : public Component { uint32_t connect_begin; // guess power state bool powered_on{false}; -#ifdef USE_MODEM_POWER // Will be true when power transitionning bool power_transition{false}; // states for triggering on/off signals ModemPowerState power_state{ModemPowerState::TOFFUART}; -#endif // USE_MODEM_POWER }; InternalState internal_state_; }; diff --git a/esphome/components/modem/sensor/modem_sensor.cpp b/esphome/components/modem/sensor/modem_sensor.cpp index d80bc965d1..18ebaa200b 100644 --- a/esphome/components/modem/sensor/modem_sensor.cpp +++ b/esphome/components/modem/sensor/modem_sensor.cpp @@ -40,7 +40,9 @@ void ModemSensor::update() { if (modem::global_modem_component->dce && modem::global_modem_component->modem_ready()) { this->update_signal_sensors_(); App.feed_wdt(); - this->update_gnss_sensors_(); + if (!modem::global_modem_component->get_gnss_power_command().empty()) { + this->update_gnss_sensors_(); + } } } @@ -64,7 +66,6 @@ void ModemSensor::update_signal_sensors_() { } } -#ifdef USE_MODEM_GNSS std::map get_gnssinfo_tokens(const std::string &gnss_info, const std::string &module) { // for 7670 (18 tokens): // +CGNSSINFO: 3,12,,04,00,48.6167297,N,4.5600739,W,060824,101218.00,75.7,0.000,234.10,2.52,1.88,1.68,08 @@ -233,8 +234,6 @@ void ModemSensor::update_gnss_sensors_() { } } -#endif // USE_MODEM_GNSS - } // namespace modem_sensor } // namespace esphome diff --git a/esphome/components/modem/sensor/modem_sensor.h b/esphome/components/modem/sensor/modem_sensor.h index e5cc4abf5c..d604e73a9d 100644 --- a/esphome/components/modem/sensor/modem_sensor.h +++ b/esphome/components/modem/sensor/modem_sensor.h @@ -20,14 +20,12 @@ class ModemSensor : public PollingComponent { void set_rssi_sensor(sensor::Sensor *rssi_sensor) { this->rssi_sensor_ = rssi_sensor; } void set_ber_sensor(sensor::Sensor *ber_sensor) { this->ber_sensor_ = ber_sensor; } -#ifdef USE_MODEM_GNSS void set_latitude_sensor(sensor::Sensor *latitude_sensor) { this->gnss_latitude_sensor_ = latitude_sensor; } void set_longitude_sensor(sensor::Sensor *longitude_sensor) { this->gnss_longitude_sensor_ = longitude_sensor; } void set_altitude_sensor(sensor::Sensor *altitude_sensor) { this->gnss_altitude_sensor_ = altitude_sensor; } void set_course_sensor(sensor::Sensor *course_sensor) { this->gnss_course_sensor_ = course_sensor; } void set_speed_sensor(sensor::Sensor *speed_sensor) { this->gnss_speed_sensor_ = speed_sensor; } void set_accuracy_sensor(sensor::Sensor *accuracy_sensor) { this->gnss_accuracy_sensor_ = accuracy_sensor; } -#endif // USE_MODEM_GNSS // ========== INTERNAL METHODS ========== // (In most use cases you won't need these) @@ -42,7 +40,6 @@ class ModemSensor : public PollingComponent { sensor::Sensor *ber_sensor_{nullptr}; void update_signal_sensors_(); -#ifdef USE_MODEM_GNSS sensor::Sensor *gnss_latitude_sensor_{nullptr}; sensor::Sensor *gnss_longitude_sensor_{nullptr}; sensor::Sensor *gnss_altitude_sensor_{nullptr}; @@ -50,7 +47,6 @@ class ModemSensor : public PollingComponent { sensor::Sensor *gnss_course_sensor_{nullptr}; sensor::Sensor *gnss_accuracy_sensor_{nullptr}; void update_gnss_sensors_(); -#endif // USE_MODEM_GNSS }; } // namespace modem_sensor