33 ESP_LOGI(TAG,
"Connecting");
41 ESP_LOGI(TAG,
"Disconnecting");
56 buf_append_printf(delete_cmd,
sizeof(delete_cmd), 0,
"AT+CMGD=%d", this->
parse_index_);
64 ESP_LOGV(TAG,
"S: %s - %d",
message.c_str(), this->state_);
75 ESP_LOGV(TAG,
"R: %s - %d",
message.c_str(), this->state_);
78 if (message ==
"RING") {
82 }
else if (message ==
"NO CARRIER") {
87 }
else if (message.starts_with(
"+DTMF:")) {
89 size_t start =
message.find_first_not_of(
' ', 6);
90 if (start != std::string::npos) {
92 ESP_LOGD(TAG,
"DTMF received: %s",
key.c_str());
108 if (message !=
"ERROR" && !
message.starts_with(
"+CME ERROR:")) {
114 ESP_LOGW(TAG,
"DTMF detection not supported by the module: %s",
message.c_str());
116 ESP_LOGW(TAG,
"Not ack. %d %s", this->
state_, message.c_str());
123 ESP_LOGW(TAG,
"Received unexpected OK. Ignoring");
131 bool message_available =
message.starts_with(
"+CMTI:");
132 if (!message_available) {
136 }
else if (message ==
"NO CARRIER") {
141 }
else if (message.starts_with(
"+CUSD:")) {
186 ESP_LOGD(TAG,
"SendUssd2: '%s'",
message.c_str());
189 ESP_LOGD(TAG,
"Dialing ussd code: '%s' done.", this->
ussd_.c_str());
200 ESP_LOGD(TAG,
"Check ussd code: '%s'",
message.c_str());
201 if (
message.starts_with(
"+CUSD:")) {
229 ESP_LOGD(TAG,
"Registered OK");
234 ESP_LOGW(TAG,
"Registration Fail");
252 if (message.starts_with(
"+CSQ:")) {
253 size_t comma =
message.find(
',', 6);
258 if (this->rssi_sensor_ !=
nullptr) {
259 this->rssi_sensor_->publish_state(rssi);
261 ESP_LOGD(TAG,
"RSSI: %d", rssi);
264 ESP_LOGD(TAG,
"RSSI: %d", rssi);
272 if (message.starts_with(
"+CMGL:") && this->parse_index_ == 0) {
276 while (
end != start) {
284 this->
sender_ = message.substr(start + 1,
end - start - 2);
295 ESP_LOGD(TAG,
"Invalid message %d %s", this->
state_, message.c_str());
307 if (message.starts_with(
"+CLCC:") && this->parse_index_ == 0) {
312 while (
end != start) {
319 if (current_call_state == 4) {
320 ESP_LOGV(TAG,
"Premature call state '4'. Ignoring, waiting for RING");
323 ESP_LOGD(TAG,
"Call state is now: %d", current_call_state);
324 if (current_call_state == 0)
337 ESP_LOGD(TAG,
"Invalid message %d %s", this->
state_, message.c_str());
353 if (ok ||
message.starts_with(
"+CMGL:")) {
355 "Received SMS from: %s\n"
357 this->
sender_.c_str(), this->message_.c_str());
375 if (message ==
">") {
377 ESP_LOGI(TAG,
"Sending to %s message: '%s'", this->
recipient_.c_str(), this->outgoing_message_.c_str());
389 if (message.starts_with(
"+CMGS:")) {
390 ESP_LOGD(TAG,
"SMS Sent OK: %s",
message.c_str());
402 ESP_LOGI(TAG,
"Dialing: '%s'", this->
recipient_.c_str());
412 if (message.starts_with(
"+CLIP:")) {
413 std::string caller_id;
417 while (
end != start) {
421 caller_id =
message.substr(start + 1,
end - start - 2);
431 ESP_LOGI(TAG,
"Incoming call from %s", caller_id.c_str());
438 ESP_LOGI(TAG,
"Call connected");
446 ESP_LOGW(TAG,
"Unhandled: %s - %d",
message.c_str(), this->state_);
451void Sim800LComponent::loop() {
464 if (line !=
nullptr) {
477void Sim800LComponent::send_sms(
const std::string &recipient,
const std::string &
message) {
483void Sim800LComponent::send_ussd(
const std::string &ussd_code) {
484 ESP_LOGD(TAG,
"Sending USSD code: %s", ussd_code.c_str());
485 this->
ussd_ = ussd_code;
489void Sim800LComponent::dump_config() {
490 ESP_LOGCONFIG(TAG,
"SIM800L:");
491#ifdef USE_BINARY_SENSOR
492 LOG_BINARY_SENSOR(
" ",
"Registered", this->registered_binary_sensor_);
495 LOG_SENSOR(
" ",
"Rssi", this->rssi_sensor_);
498void Sim800LComponent::dial(
const std::string &recipient) {
507#ifdef USE_BINARY_SENSOR
508 PUBLISH_STATE(this->registered_binary_sensor_, registered);
const char * partial()
The partial line, NUL terminated, for prompts with no line end; the first N - 1 characters on the byt...
size_t length() const
Length of the partial line; 0 after a line is returned and while an overlong line is dropped.
char * feed(uint8_t c)
Feed one byte; returns the completed line (empty lines included), valid until the next call,...
void reset()
Forget the partial line and start a new one with the next byte.
void log_if_overflowed(const char *tag) const
Log the shared "line dropped" warning when the last feed() overflowed.
void send_cmd_(const std::string &message)
CallbackManager< void(const std::string &)> ussd_received_callback_
CallbackManager< void()> call_connected_callback_
void set_registered_(bool registered)
CallbackManager< void(const std::string &)> incoming_call_callback_
void parse_cmd_(std::string message)
CallbackManager< void()> call_disconnected_callback_
LineReader< SIM800L_READ_BUFFER_LENGTH > line_reader_
std::string outgoing_message_
CallbackManager< void(const std::string &)> dtmf_received_callback_
CallbackManager< void(const std::string &, const std::string &)> sms_received_callback_
void write_str(const char *str)
void write_byte(uint8_t data)
bool read_available(F &&on_byte)
Same as read_available_chunks(), calling on_byte(uint8_t) once per byte.
size_t write(uint8_t data)
const LogString * message
@ STATE_PARSE_SMS_RESPONSE
ESPHOME_LOG_TAG(TAG, "sim800l")
optional< T > parse_number(const char *str)
Parse an unsigned decimal number from a null-terminated string.