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_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]))

View file

@ -15,8 +15,8 @@
#include <cstring>
#include <iostream>
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) {

View file

@ -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> dce;
@ -73,6 +74,7 @@ class ModemComponent : public Component {
std::string username_;
std::string password_;
std::string apn_;
std::vector<std::string> init_at_commands_;
ModemModel model_;
std::unordered_map<std::string, ModemModel> modem_model_map_ = {{"BG96", ModemModel::BG96},
{"SIM800", ModemModel::SIM800},

View file

@ -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: |-