From ef6382edeeab647b6c2581b159599579e5239110 Mon Sep 17 00:00:00 2001 From: oarcher Date: Wed, 15 May 2024 11:53:31 +0200 Subject: [PATCH] add init_at --- esphome/components/modem/__init__.py | 6 +++ esphome/components/modem/modem_component.cpp | 44 ++++++++++++-------- esphome/components/modem/modem_component.h | 2 + tests/test5.1.yaml | 12 +++--- 4 files changed, 40 insertions(+), 24 deletions(-) diff --git a/esphome/components/modem/__init__.py b/esphome/components/modem/__init__.py index 20637a3e1c..ebc3661f26 100644 --- a/esphome/components/modem/__init__.py +++ b/esphome/components/modem/__init__.py @@ -22,6 +22,7 @@ CONF_PIN_CODE = "pin_code" CONF_APN = "apn" CONF_STATUS_PIN = "status_pin" CONF_DTR_PIN = "dtr_pin" +CONF_INIT_AT = "init_at" modem_ns = cg.esphome_ns.namespace("modem") @@ -44,6 +45,7 @@ CONFIG_SCHEMA = cv.All( cv.Optional(CONF_USERNAME): cv.string, cv.Optional(CONF_PASSWORD): cv.string, cv.Optional(CONF_USE_ADDRESS): cv.string, + cv.Optional(CONF_INIT_AT): cv.All(cv.ensure_list(cv.string)), } ).extend(cv.COMPONENT_SCHEMA), cv.require_framework_version( @@ -90,6 +92,10 @@ async def to_code(config): if pin_code := config.get(CONF_PIN_CODE, None): cg.add(var.set_pin_code(pin_code)) + if init_at := config.get(CONF_INIT_AT, None): + for cmd in init_at: + cg.add(var.add_init_at_command(cmd)) + cg.add(var.set_model(config[CONF_MODEL])) cg.add(var.set_apn(config[CONF_APN])) diff --git a/esphome/components/modem/modem_component.cpp b/esphome/components/modem/modem_component.cpp index 0eb5dd0897..1f7622f4f5 100644 --- a/esphome/components/modem/modem_component.cpp +++ b/esphome/components/modem/modem_component.cpp @@ -15,8 +15,8 @@ #include #include -static const size_t CONFIG_MODEM_UART_RX_BUFFER_SIZE = 1024; -static const size_t CONFIG_MODEM_UART_TX_BUFFER_SIZE = 512; +static const size_t CONFIG_MODEM_UART_RX_BUFFER_SIZE = 2048; +static const size_t CONFIG_MODEM_UART_TX_BUFFER_SIZE = 1024; static const uint8_t CONFIG_MODEM_UART_EVENT_QUEUE_SIZE = 30; static const size_t CONFIG_MODEM_UART_EVENT_TASK_STACK_SIZE = 2048; static const uint8_t CONFIG_MODEM_UART_EVENT_TASK_PRIORITY = 5; @@ -152,8 +152,31 @@ void ModemComponent::setup() { assert(this->dce); - this->started_ = true; this->poweron(); + + esp_modem::command_result res; + res = this->dce->sync(); + int retry = 0; + while (res != command_result::OK) { + res = this->dce->sync(); + if (res != command_result::OK) { + App.feed_wdt(); + vTaskDelay(pdMS_TO_TICKS(1000)); + } + retry++; + if (retry > 20) + break; + } + + // send initial AT commands from yaml + for (const auto &cmd : this->init_at_commands_) { + std::string result; + command_result err = this->dce->at(cmd.c_str(), result, 1000); + delay(100); + ESP_LOGI(TAG, "Init AT command: %s (status %d) -> %s", cmd.c_str(), (int) err, result.c_str()); + } + + this->started_ = true; ESP_LOGV(TAG, "Setup finished"); } @@ -178,21 +201,6 @@ void ModemComponent::start_connect_() { command_result res = command_result::TIMEOUT; - // int retry = 0; - // while (res != command_result::OK) { - // res = this->dce->sync(); - // if (res != command_result::OK) { - // ESP_LOGW(TAG, "modem not responding"); - // ESP_LOGI(TAG, "Status: %d", (int) this->get_status()); - // this->dce->set_command_mode(); - // App.feed_wdt(); - // vTaskDelay(pdMS_TO_TICKS(7000)); - // } - // retry++; - // if (retry > 10) - // break; - // } - res = this->dce->sync(); if (res != command_result::OK) { diff --git a/esphome/components/modem/modem_component.h b/esphome/components/modem/modem_component.h index 28565c684f..f747784d00 100644 --- a/esphome/components/modem/modem_component.h +++ b/esphome/components/modem/modem_component.h @@ -58,6 +58,7 @@ class ModemComponent : public Component { void set_model(const std::string &model) { this->model_ = this->modem_model_map_.count(model) ? modem_model_map_[model] : ModemModel::UNKNOWN; } + void add_init_at_command(const std::string &cmd) { this->init_at_commands_.push_back(cmd); } bool get_status() { return gpio_get_level(this->status_pin_); } std::unique_ptr dce; @@ -73,6 +74,7 @@ class ModemComponent : public Component { std::string username_; std::string password_; std::string apn_; + std::vector init_at_commands_; ModemModel model_; std::unordered_map modem_model_map_ = {{"BG96", ModemModel::BG96}, {"SIM800", ModemModel::SIM800}, diff --git a/tests/test5.1.yaml b/tests/test5.1.yaml index 848acbe59f..7514057493 100644 --- a/tests/test5.1.yaml +++ b/tests/test5.1.yaml @@ -1,5 +1,5 @@ --- -# copy of test5.yaml with gsm network +# copy of test5.yaml with modem network esphome: name: test5-1 build_path: build/test5-1 @@ -12,7 +12,7 @@ esp32: framework: type: esp-idf -gsm: +modem: rx_pin: 26 tx_pin: 27 power_pin: 4 @@ -96,7 +96,7 @@ binary_sensor: id: modbus_binsensortest register_type: read address: 0x3200 - bitmask: 0x80 # (bit 8) + bitmask: 0x80 # (bit 8) lambda: "return x;" - platform: tm1638 @@ -646,9 +646,9 @@ display: id: primarydisplay stb_pin: allow_other_uses: true - number: 5 # TM1638 STB - clk_pin: 18 # TM1638 CLK - dio_pin: 23 # TM1638 DIO + number: 5 # TM1638 STB + clk_pin: 18 # TM1638 CLK + dio_pin: 23 # TM1638 DIO update_interval: 5s intensity: 5 lambda: |-