add init_at

This commit is contained in:
oarcher 2024-05-15 11:53:31 +02:00
parent 7260eb6225
commit ef6382edee
4 changed files with 40 additions and 24 deletions

View file

@ -22,6 +22,7 @@ CONF_PIN_CODE = "pin_code"
CONF_APN = "apn" CONF_APN = "apn"
CONF_STATUS_PIN = "status_pin" CONF_STATUS_PIN = "status_pin"
CONF_DTR_PIN = "dtr_pin" CONF_DTR_PIN = "dtr_pin"
CONF_INIT_AT = "init_at"
modem_ns = cg.esphome_ns.namespace("modem") modem_ns = cg.esphome_ns.namespace("modem")
@ -44,6 +45,7 @@ CONFIG_SCHEMA = cv.All(
cv.Optional(CONF_USERNAME): cv.string, cv.Optional(CONF_USERNAME): cv.string,
cv.Optional(CONF_PASSWORD): cv.string, cv.Optional(CONF_PASSWORD): cv.string,
cv.Optional(CONF_USE_ADDRESS): 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), ).extend(cv.COMPONENT_SCHEMA),
cv.require_framework_version( cv.require_framework_version(
@ -90,6 +92,10 @@ async def to_code(config):
if pin_code := config.get(CONF_PIN_CODE, None): if pin_code := config.get(CONF_PIN_CODE, None):
cg.add(var.set_pin_code(pin_code)) 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_model(config[CONF_MODEL]))
cg.add(var.set_apn(config[CONF_APN])) cg.add(var.set_apn(config[CONF_APN]))

View file

@ -15,8 +15,8 @@
#include <cstring> #include <cstring>
#include <iostream> #include <iostream>
static const size_t CONFIG_MODEM_UART_RX_BUFFER_SIZE = 1024; static const size_t CONFIG_MODEM_UART_RX_BUFFER_SIZE = 2048;
static const size_t CONFIG_MODEM_UART_TX_BUFFER_SIZE = 512; static const size_t CONFIG_MODEM_UART_TX_BUFFER_SIZE = 1024;
static const uint8_t CONFIG_MODEM_UART_EVENT_QUEUE_SIZE = 30; 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 size_t CONFIG_MODEM_UART_EVENT_TASK_STACK_SIZE = 2048;
static const uint8_t CONFIG_MODEM_UART_EVENT_TASK_PRIORITY = 5; static const uint8_t CONFIG_MODEM_UART_EVENT_TASK_PRIORITY = 5;
@ -152,8 +152,31 @@ void ModemComponent::setup() {
assert(this->dce); assert(this->dce);
this->started_ = true;
this->poweron(); 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"); ESP_LOGV(TAG, "Setup finished");
} }
@ -178,21 +201,6 @@ void ModemComponent::start_connect_() {
command_result res = command_result::TIMEOUT; 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(); res = this->dce->sync();
if (res != command_result::OK) { if (res != command_result::OK) {

View file

@ -58,6 +58,7 @@ class ModemComponent : public Component {
void set_model(const std::string &model) { void set_model(const std::string &model) {
this->model_ = this->modem_model_map_.count(model) ? modem_model_map_[model] : ModemModel::UNKNOWN; 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_); } bool get_status() { return gpio_get_level(this->status_pin_); }
std::unique_ptr<DCE> dce; std::unique_ptr<DCE> dce;
@ -73,6 +74,7 @@ class ModemComponent : public Component {
std::string username_; std::string username_;
std::string password_; std::string password_;
std::string apn_; std::string apn_;
std::vector<std::string> init_at_commands_;
ModemModel model_; ModemModel model_;
std::unordered_map<std::string, ModemModel> modem_model_map_ = {{"BG96", ModemModel::BG96}, std::unordered_map<std::string, ModemModel> modem_model_map_ = {{"BG96", ModemModel::BG96},
{"SIM800", ModemModel::SIM800}, {"SIM800", ModemModel::SIM800},

View file

@ -1,5 +1,5 @@
--- ---
# copy of test5.yaml with gsm network # copy of test5.yaml with modem network
esphome: esphome:
name: test5-1 name: test5-1
build_path: build/test5-1 build_path: build/test5-1
@ -12,7 +12,7 @@ esp32:
framework: framework:
type: esp-idf type: esp-idf
gsm: modem:
rx_pin: 26 rx_pin: 26
tx_pin: 27 tx_pin: 27
power_pin: 4 power_pin: 4