ESPHome  2022.12.8
sim800l.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <utility>
4 
5 #include "esphome/core/defines.h"
7 #ifdef USE_BINARY_SENSOR
9 #endif
10 #ifdef USE_SENSOR
12 #endif
15 
16 namespace esphome {
17 namespace sim800l {
18 
19 const uint16_t SIM800L_READ_BUFFER_LENGTH = 1024;
20 
21 enum State {
48 };
49 
51  public:
53  void update() override;
54  void loop() override;
55  void dump_config() override;
56 #ifdef USE_BINARY_SENSOR
58  registered_binary_sensor_ = registered_binary_sensor;
59  }
60 #endif
61 #ifdef USE_SENSOR
62  void set_rssi_sensor(sensor::Sensor *rssi_sensor) { rssi_sensor_ = rssi_sensor; }
63 #endif
64  void add_on_sms_received_callback(std::function<void(std::string, std::string)> callback) {
65  this->sms_received_callback_.add(std::move(callback));
66  }
67  void add_on_incoming_call_callback(std::function<void(std::string)> callback) {
68  this->incoming_call_callback_.add(std::move(callback));
69  }
70  void add_on_call_connected_callback(std::function<void()> callback) {
71  this->call_connected_callback_.add(std::move(callback));
72  }
73  void add_on_call_disconnected_callback(std::function<void()> callback) {
74  this->call_disconnected_callback_.add(std::move(callback));
75  }
76  void add_on_ussd_received_callback(std::function<void(std::string)> callback) {
77  this->ussd_received_callback_.add(std::move(callback));
78  }
79  void send_sms(const std::string &recipient, const std::string &message);
80  void send_ussd(const std::string &ussd_code);
81  void dial(const std::string &recipient);
82  void connect();
83  void disconnect();
84 
85  protected:
86  void send_cmd_(const std::string &message);
87  void parse_cmd_(std::string message);
88  void set_registered_(bool registered);
89 
90 #ifdef USE_BINARY_SENSOR
92 #endif
93 
94 #ifdef USE_SENSOR
96 #endif
97  std::string sender_;
98  std::string message_;
100  size_t read_pos_{0};
101  uint8_t parse_index_{0};
102  uint8_t watch_dog_{0};
103  bool expect_ack_{false};
105  bool registered_{false};
106 
107  std::string recipient_;
108  std::string outgoing_message_;
109  std::string ussd_;
115  uint8_t call_state_{6};
116 
122 };
123 
124 class Sim800LReceivedMessageTrigger : public Trigger<std::string, std::string> {
125  public:
128  [this](const std::string &message, const std::string &sender) { this->trigger(message, sender); });
129  }
130 };
131 
132 class Sim800LIncomingCallTrigger : public Trigger<std::string> {
133  public:
135  parent->add_on_incoming_call_callback([this](const std::string &caller_id) { this->trigger(caller_id); });
136  }
137 };
138 
140  public:
142  parent->add_on_call_connected_callback([this]() { this->trigger(); });
143  }
144 };
145 
147  public:
149  parent->add_on_call_disconnected_callback([this]() { this->trigger(); });
150  }
151 };
152 class Sim800LReceivedUssdTrigger : public Trigger<std::string> {
153  public:
155  parent->add_on_ussd_received_callback([this](const std::string &ussd) { this->trigger(ussd); });
156  }
157 };
158 
159 template<typename... Ts> class Sim800LSendSmsAction : public Action<Ts...> {
160  public:
162  TEMPLATABLE_VALUE(std::string, recipient)
163  TEMPLATABLE_VALUE(std::string, message)
164 
165  void play(Ts... x) {
166  auto recipient = this->recipient_.value(x...);
167  auto message = this->message_.value(x...);
168  this->parent_->send_sms(recipient, message);
169  }
170 
171  protected:
173 };
174 
175 template<typename... Ts> class Sim800LSendUssdAction : public Action<Ts...> {
176  public:
178  TEMPLATABLE_VALUE(std::string, ussd)
179 
180  void play(Ts... x) {
181  auto ussd_code = this->ussd_.value(x...);
182  this->parent_->send_ussd(ussd_code);
183  }
184 
185  protected:
187 };
188 
189 template<typename... Ts> class Sim800LDialAction : public Action<Ts...> {
190  public:
192  TEMPLATABLE_VALUE(std::string, recipient)
193 
194  void play(Ts... x) {
195  auto recipient = this->recipient_.value(x...);
196  this->parent_->dial(recipient);
197  }
198 
199  protected:
201 };
202 template<typename... Ts> class Sim800LConnectAction : public Action<Ts...> {
203  public:
205 
206  void play(Ts... x) { this->parent_->connect(); }
207 
208  protected:
210 };
211 
212 template<typename... Ts> class Sim800LDisconnectAction : public Action<Ts...> {
213  public:
215 
216  void play(Ts... x) { this->parent_->disconnect(); }
217 
218  protected:
220 };
221 
222 } // namespace sim800l
223 } // namespace esphome
CallbackManager< void(std::string)> ussd_received_callback_
Definition: sim800l.h:121
Sim800LIncomingCallTrigger(Sim800LComponent *parent)
Definition: sim800l.h:134
Sim800LComponent * parent_
Definition: sim800l.h:200
CallbackManager< void(std::string, std::string)> sms_received_callback_
Definition: sim800l.h:117
void send_cmd_(const std::string &message)
Definition: sim800l.cpp:61
void send_sms(const std::string &recipient, const std::string &message)
Definition: sim800l.cpp:451
Sim800LCallConnectedTrigger(Sim800LComponent *parent)
Definition: sim800l.h:141
void add_on_sms_received_callback(std::function< void(std::string, std::string)> callback)
Definition: sim800l.h:64
CallbackManager< void()> call_disconnected_callback_
Definition: sim800l.h:120
void set_registered_binary_sensor(binary_sensor::BinarySensor *registered_binary_sensor)
Definition: sim800l.h:57
This class simplifies creating components that periodically check a state.
Definition: component.h:267
UARTComponent * parent_
Definition: uart.h:68
void add_on_incoming_call_callback(std::function< void(std::string)> callback)
Definition: sim800l.h:67
Sim800LDialAction(Sim800LComponent *parent)
Definition: sim800l.h:191
void set_rssi_sensor(sensor::Sensor *rssi_sensor)
Definition: sim800l.h:62
void update() override
Retrieve the latest sensor values. This operation takes approximately 16ms.
Definition: sim800l.cpp:13
void parse_cmd_(std::string message)
Definition: sim800l.cpp:69
Sim800LReceivedUssdTrigger(Sim800LComponent *parent)
Definition: sim800l.h:154
Sim800LCallDisconnectedTrigger(Sim800LComponent *parent)
Definition: sim800l.h:148
TEMPLATABLE_VALUE(std::string, recipient) void play(Ts... x)
Definition: sim800l.h:192
char read_buffer_[SIM800L_READ_BUFFER_LENGTH]
Definition: sim800l.h:99
binary_sensor::BinarySensor * registered_binary_sensor_
Definition: sim800l.h:91
void send_ussd(const std::string &ussd_code)
Definition: sim800l.cpp:457
sensor::Sensor * rssi_sensor_
Definition: sim800l.h:95
CallbackManager< void(std::string)> incoming_call_callback_
Definition: sim800l.h:118
void dial(const std::string &recipient)
Definition: sim800l.cpp:472
CallbackManager< void()> call_connected_callback_
Definition: sim800l.h:119
void add_on_call_disconnected_callback(std::function< void()> callback)
Definition: sim800l.h:73
void add_on_call_connected_callback(std::function< void()> callback)
Definition: sim800l.h:70
TEMPLATABLE_VALUE(std::string, recipient) TEMPLATABLE_VALUE(std
Definition: sim800l.h:162
void set_registered_(bool registered)
Definition: sim800l.cpp:479
Sim800LConnectAction(Sim800LComponent *parent)
Definition: sim800l.h:204
Definition: a4988.cpp:4
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:28
Base-class for all sensors.
Definition: sensor.h:50
const uint16_t SIM800L_READ_BUFFER_LENGTH
Definition: sim800l.h:19
TEMPLATABLE_VALUE(std::string, ussd) void play(Ts... x)
Definition: sim800l.h:178
void add_on_ussd_received_callback(std::function< void(std::string)> callback)
Definition: sim800l.h:76
Sim800LReceivedMessageTrigger(Sim800LComponent *parent)
Definition: sim800l.h:126
Sim800LDisconnectAction(Sim800LComponent *parent)
Definition: sim800l.h:214
Sim800LSendSmsAction(Sim800LComponent *parent)
Definition: sim800l.h:161
Sim800LSendUssdAction(Sim800LComponent *parent)
Definition: sim800l.h:177