8 static const char *
const TAG =
"sim800l";
31 ESP_LOGI(TAG,
"Connecting...");
39 ESP_LOGI(TAG,
"Disconnecting...");
62 ESP_LOGV(TAG,
"S: %s - %d", message.c_str(), this->
state_);
73 ESP_LOGV(TAG,
"R: %s - %d", message.c_str(), this->
state_);
76 if (message ==
"RING") {
80 }
else if (message ==
"NO CARRIER") {
88 bool ok = message ==
"OK";
97 ESP_LOGW(TAG,
"Not ack. %d %s", this->
state_, message.c_str());
104 ESP_LOGW(TAG,
"Received unexpected OK. Ignoring");
112 bool message_available = message.compare(0, 6,
"+CMTI:") == 0;
113 if (!message_available) {
114 if (message ==
"RING") {
117 }
else if (message ==
"NO CARRIER") {
122 }
else if (message.compare(0, 6,
"+CUSD:") == 0) {
162 ESP_LOGD(TAG,
"SendUssd2: '%s'", message.c_str());
163 if (message ==
"OK") {
165 ESP_LOGD(TAG,
"Dialing ussd code: '%s' done.", this->
ussd_.c_str());
176 ESP_LOGD(TAG,
"Check ussd code: '%s'", message.c_str());
177 if (message.compare(0, 6,
"+CUSD:") == 0) {
181 size_t end = message.find_last_of(
',');
183 this->
ussd_ = message.substr(start + 1, end - start - 2);
198 bool registered = message.compare(0, 6,
"+CREG:") == 0 && (message[9] ==
'1' || message[9] ==
'5');
201 ESP_LOGD(TAG,
"Registered OK");
206 ESP_LOGW(TAG,
"Registration Fail");
207 if (message[7] ==
'0') {
224 if (message.compare(0, 5,
"+CSQ:") == 0) {
225 size_t comma = message.find(
',', 6);
227 int rssi = parse_number<int>(message.substr(6, comma - 6)).value_or(0);
233 ESP_LOGD(TAG,
"RSSI: %d", rssi);
236 ESP_LOGD(TAG,
"RSSI: %d", rssi);
244 if (message.compare(0, 6,
"+CMGL:") == 0 && this->
parse_index_ == 0) {
246 size_t end = message.find(
',', start);
248 while (end != start) {
251 this->
parse_index_ = parse_number<uint8_t>(message.substr(start, end - start)).value_or(0);
256 this->
sender_ = message.substr(start + 1, end - start - 2);
263 end = message.find(
',', start);
267 ESP_LOGD(TAG,
"Invalid message %d %s", this->
state_, message.c_str());
279 if (message.compare(0, 6,
"+CLCC:") == 0 && this->
parse_index_ == 0) {
282 size_t end = message.find(
',', start);
284 while (end != start) {
289 uint8_t current_call_state = parse_number<uint8_t>(message.substr(start, end - start)).value_or(6);
291 ESP_LOGD(TAG,
"Call state is now: %d", current_call_state);
292 if (current_call_state == 0)
301 end = message.find(
',', start);
305 ESP_LOGD(TAG,
"Invalid message %d %s", this->
state_, message.c_str());
321 if (ok || message.compare(0, 6,
"+CMGL:") == 0) {
322 ESP_LOGD(TAG,
"Received SMS from: %s", this->
sender_.c_str());
323 ESP_LOGD(TAG,
"%s", this->
message_.c_str());
341 if (message ==
">") {
355 if (message.compare(0, 6,
"+CMGS:") == 0) {
356 ESP_LOGD(TAG,
"SMS Sent OK: %s", message.c_str());
368 ESP_LOGI(TAG,
"Dialing: '%s'", this->
recipient_.c_str());
378 if (message.compare(0, 6,
"+CLIP:") == 0) {
379 std::string caller_id;
381 size_t end = message.find(
',', start);
383 while (end != start) {
387 caller_id = message.substr(start + 1, end - start - 2);
393 end = message.find(
',', start);
397 ESP_LOGI(TAG,
"Incoming call from %s", caller_id.c_str());
404 ESP_LOGI(TAG,
"Call connected");
412 ESP_LOGW(TAG,
"Unhandled: %s - %d", message.c_str(), this->
state_);
426 ESP_LOGVV(TAG,
"Buffer pos: %u %d", this->
read_pos_, byte);
428 if (byte == ASCII_CR)
459 ESP_LOGD(TAG,
"Sending USSD code: %s", ussd_code.c_str());
460 this->
ussd_ = ussd_code;
465 ESP_LOGCONFIG(TAG,
"SIM800L:");
466 #ifdef USE_BINARY_SENSOR 482 #ifdef USE_BINARY_SENSOR void write_str(const char *str)
CallbackManager< void(std::string)> ussd_received_callback_
CallbackManager< void(std::string, std::string)> sms_received_callback_
void write_byte(uint8_t data)
void send_cmd_(const std::string &message)
void send_sms(const std::string &recipient, const std::string &message)
CallbackManager< void()> call_disconnected_callback_
void update() override
Retrieve the latest sensor values. This operation takes approximately 16ms.
bool read_byte(uint8_t *data)
void parse_cmd_(std::string message)
void publish_state(float state)
Publish a new state to the front-end.
char read_buffer_[SIM800L_READ_BUFFER_LENGTH]
void publish_state(bool state)
Publish a new state to the front-end.
binary_sensor::BinarySensor * registered_binary_sensor_
void send_ussd(const std::string &ussd_code)
void dump_config() override
sensor::Sensor * rssi_sensor_
CallbackManager< void(std::string)> incoming_call_callback_
void dial(const std::string &recipient)
CallbackManager< void()> call_connected_callback_
void set_registered_(bool registered)
Implementation of SPI Controller mode.
const uint16_t SIM800L_READ_BUFFER_LENGTH
std::string outgoing_message_
size_t write(uint8_t data)