ESPHome  2022.6.3
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 uint8_t SIM800L_READ_BUFFER_LENGTH = 255;
20 
21 enum State {
44 };
45 
47  public:
49  void update() override;
50  void loop() override;
51  void dump_config() override;
52 #ifdef USE_BINARY_SENSOR
54  registered_binary_sensor_ = registered_binary_sensor;
55  }
56 #endif
57 #ifdef USE_SENSOR
58  void set_rssi_sensor(sensor::Sensor *rssi_sensor) { rssi_sensor_ = rssi_sensor; }
59 #endif
60  void add_on_sms_received_callback(std::function<void(std::string, std::string)> callback) {
61  this->callback_.add(std::move(callback));
62  }
63  void send_sms(const std::string &recipient, const std::string &message);
64  void dial(const std::string &recipient);
65 
66  protected:
67  void send_cmd_(const std::string &message);
68  void parse_cmd_(std::string message);
69  void set_registered_(bool registered);
70 
71 #ifdef USE_BINARY_SENSOR
73 #endif
74 
75 #ifdef USE_SENSOR
77 #endif
78  std::string sender_;
80  size_t read_pos_{0};
81  uint8_t parse_index_{0};
82  uint8_t watch_dog_{0};
83  bool expect_ack_{false};
85  bool registered_{false};
86 
87  std::string recipient_;
88  std::string outgoing_message_;
91 
93 };
94 
95 class Sim800LReceivedMessageTrigger : public Trigger<std::string, std::string> {
96  public:
99  [this](const std::string &message, const std::string &sender) { this->trigger(message, sender); });
100  }
101 };
102 
103 template<typename... Ts> class Sim800LSendSmsAction : public Action<Ts...> {
104  public:
106  TEMPLATABLE_VALUE(std::string, recipient)
107  TEMPLATABLE_VALUE(std::string, message)
108 
109  void play(Ts... x) {
110  auto recipient = this->recipient_.value(x...);
111  auto message = this->message_.value(x...);
112  this->parent_->send_sms(recipient, message);
113  }
114 
115  protected:
117 };
118 
119 template<typename... Ts> class Sim800LDialAction : public Action<Ts...> {
120  public:
122  TEMPLATABLE_VALUE(std::string, recipient)
123 
124  void play(Ts... x) {
125  auto recipient = this->recipient_.value(x...);
126  this->parent_->dial(recipient);
127  }
128 
129  protected:
131 };
132 
133 } // namespace sim800l
134 } // namespace esphome
Sim800LComponent * parent_
Definition: sim800l.h:130
void send_cmd_(const std::string &message)
Definition: sim800l.cpp:43
void send_sms(const std::string &recipient, const std::string &message)
Definition: sim800l.cpp:280
void add_on_sms_received_callback(std::function< void(std::string, std::string)> callback)
Definition: sim800l.h:60
void set_registered_binary_sensor(binary_sensor::BinarySensor *registered_binary_sensor)
Definition: sim800l.h:53
This class simplifies creating components that periodically check a state.
Definition: component.h:266
UARTComponent * parent_
Definition: uart.h:68
Sim800LDialAction(Sim800LComponent *parent)
Definition: sim800l.h:121
void set_rssi_sensor(sensor::Sensor *rssi_sensor)
Definition: sim800l.h:58
const uint8_t SIM800L_READ_BUFFER_LENGTH
Definition: sim800l.h:19
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:50
TEMPLATABLE_VALUE(std::string, recipient) void play(Ts... x)
Definition: sim800l.h:122
char read_buffer_[SIM800L_READ_BUFFER_LENGTH]
Definition: sim800l.h:79
binary_sensor::BinarySensor * registered_binary_sensor_
Definition: sim800l.h:72
sensor::Sensor * rssi_sensor_
Definition: sim800l.h:76
void dial(const std::string &recipient)
Definition: sim800l.cpp:296
TEMPLATABLE_VALUE(std::string, recipient) TEMPLATABLE_VALUE(std
Definition: sim800l.h:106
void set_registered_(bool registered)
Definition: sim800l.cpp:303
Definition: a4988.cpp:4
Base class for all binary_sensor-type classes.
Definition: binary_sensor.h:26
Base-class for all sensors.
Definition: sensor.h:47
CallbackManager< void(std::string, std::string)> callback_
Definition: sim800l.h:92
Sim800LReceivedMessageTrigger(Sim800LComponent *parent)
Definition: sim800l.h:97
Sim800LSendSmsAction(Sim800LComponent *parent)
Definition: sim800l.h:105