ESPHome  2024.2.0
mqtt_backend_esp8266.h
Go to the documentation of this file.
1 #pragma once
2 
3 #ifdef USE_ESP8266
4 
5 #include "mqtt_backend.h"
6 #include <AsyncMqttClient.h>
7 
8 namespace esphome {
9 namespace mqtt {
10 
11 class MQTTBackendESP8266 final : public MQTTBackend {
12  public:
13  void set_keep_alive(uint16_t keep_alive) final { mqtt_client_.setKeepAlive(keep_alive); }
14  void set_client_id(const char *client_id) final { mqtt_client_.setClientId(client_id); }
15  void set_clean_session(bool clean_session) final { mqtt_client_.setCleanSession(clean_session); }
16  void set_credentials(const char *username, const char *password) final {
17  mqtt_client_.setCredentials(username, password);
18  }
19  void set_will(const char *topic, uint8_t qos, bool retain, const char *payload) final {
20  mqtt_client_.setWill(topic, qos, retain, payload);
21  }
22  void set_server(network::IPAddress ip, uint16_t port) final { mqtt_client_.setServer(ip, port); }
23  void set_server(const char *host, uint16_t port) final { mqtt_client_.setServer(host, port); }
24 #if ASYNC_TCP_SSL_ENABLED
25  void set_secure(bool secure) { mqtt_client.setSecure(secure); }
26  void add_server_fingerprint(const uint8_t *fingerprint) { mqtt_client.addServerFingerprint(fingerprint); }
27 #endif
28 
29  void set_on_connect(std::function<on_connect_callback_t> &&callback) final {
30  this->mqtt_client_.onConnect(std::move(callback));
31  }
32  void set_on_disconnect(std::function<on_disconnect_callback_t> &&callback) final {
33  auto async_callback = [callback](AsyncMqttClientDisconnectReason reason) {
34  // int based enum so casting isn't a problem
35  callback(static_cast<MQTTClientDisconnectReason>(reason));
36  };
37  this->mqtt_client_.onDisconnect(std::move(async_callback));
38  }
39  void set_on_subscribe(std::function<on_subscribe_callback_t> &&callback) final {
40  this->mqtt_client_.onSubscribe(std::move(callback));
41  }
42  void set_on_unsubscribe(std::function<on_unsubscribe_callback_t> &&callback) final {
43  this->mqtt_client_.onUnsubscribe(std::move(callback));
44  }
45  void set_on_message(std::function<on_message_callback_t> &&callback) final {
46  auto async_callback = [callback](const char *topic, const char *payload,
47  AsyncMqttClientMessageProperties async_properties, size_t len, size_t index,
48  size_t total) { callback(topic, payload, len, index, total); };
49  mqtt_client_.onMessage(std::move(async_callback));
50  }
51  void set_on_publish(std::function<on_publish_user_callback_t> &&callback) final {
52  this->mqtt_client_.onPublish(std::move(callback));
53  }
54 
55  bool connected() const final { return mqtt_client_.connected(); }
56  void connect() final { mqtt_client_.connect(); }
57  void disconnect() final { mqtt_client_.disconnect(true); }
58  bool subscribe(const char *topic, uint8_t qos) final { return mqtt_client_.subscribe(topic, qos) != 0; }
59  bool unsubscribe(const char *topic) final { return mqtt_client_.unsubscribe(topic) != 0; }
60  bool publish(const char *topic, const char *payload, size_t length, uint8_t qos, bool retain) final {
61  return mqtt_client_.publish(topic, qos, retain, payload, length, false, 0) != 0;
62  }
64 
65  protected:
66  AsyncMqttClient mqtt_client_;
67 };
68 
69 } // namespace mqtt
70 } // namespace esphome
71 
72 #endif // defined(USE_ESP8266)
void add_server_fingerprint(const uint8_t *fingerprint)
void set_will(const char *topic, uint8_t qos, bool retain, const char *payload) final
void set_on_publish(std::function< on_publish_user_callback_t > &&callback) final
void set_on_message(std::function< on_message_callback_t > &&callback) final
void set_on_connect(std::function< on_connect_callback_t > &&callback) final
void set_on_subscribe(std::function< on_subscribe_callback_t > &&callback) final
bool unsubscribe(const char *topic) final
void set_client_id(const char *client_id) final
void set_on_disconnect(std::function< on_disconnect_callback_t > &&callback) final
void set_keep_alive(uint16_t keep_alive) final
void set_on_unsubscribe(std::function< on_unsubscribe_callback_t > &&callback) final
void set_credentials(const char *username, const char *password) final
std::string size_t len
Definition: helpers.h:292
bool publish(const char *topic, const char *payload, size_t length, uint8_t qos, bool retain) final
uint16_t length
Definition: tt21100.cpp:12
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
bool subscribe(const char *topic, uint8_t qos) final
void set_server(network::IPAddress ip, uint16_t port) final
void set_clean_session(bool clean_session) final
void set_server(const char *host, uint16_t port) final
virtual bool publish(const char *topic, const char *payload, size_t length, uint8_t qos, bool retain)=0