mirror of
https://github.com/esphome/esphome.git
synced 2024-11-25 08:28:12 +01:00
Change MQTT client for ESP32 Arduino (#5157)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
parent
93b7ca77ca
commit
9980b9972f
7 changed files with 29 additions and 28 deletions
|
@ -271,8 +271,8 @@ def exp_mqtt_message(config):
|
|||
async def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await cg.register_component(var, config)
|
||||
# Add required libraries for arduino
|
||||
if CORE.using_arduino:
|
||||
# Add required libraries for ESP8266
|
||||
if CORE.is_esp8266:
|
||||
# https://github.com/OttoWinter/async-mqtt-client/blob/master/library.json
|
||||
cg.add_library("ottowinter/AsyncMqttClient-esphome", "0.8.6")
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#ifdef USE_ESP_IDF
|
||||
#ifdef USE_ESP32
|
||||
|
||||
#include <string>
|
||||
#include "mqtt_backend_idf.h"
|
||||
#include "mqtt_backend_esp32.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
|
||||
|
@ -10,7 +10,7 @@ namespace mqtt {
|
|||
|
||||
static const char *const TAG = "mqtt.idf";
|
||||
|
||||
bool MQTTBackendIDF::initialize_() {
|
||||
bool MQTTBackendESP32::initialize_() {
|
||||
#if ESP_IDF_VERSION_MAJOR < 5
|
||||
mqtt_cfg_.user_context = (void *) this;
|
||||
mqtt_cfg_.buffer_size = MQTT_BUFFER_SIZE;
|
||||
|
@ -95,7 +95,7 @@ bool MQTTBackendIDF::initialize_() {
|
|||
}
|
||||
}
|
||||
|
||||
void MQTTBackendIDF::loop() {
|
||||
void MQTTBackendESP32::loop() {
|
||||
// process new events
|
||||
// handle only 1 message per loop iteration
|
||||
if (!mqtt_events_.empty()) {
|
||||
|
@ -105,7 +105,7 @@ void MQTTBackendIDF::loop() {
|
|||
}
|
||||
}
|
||||
|
||||
void MQTTBackendIDF::mqtt_event_handler_(const Event &event) {
|
||||
void MQTTBackendESP32::mqtt_event_handler_(const Event &event) {
|
||||
ESP_LOGV(TAG, "Event dispatched from event loop event_id=%d", event.event_id);
|
||||
switch (event.event_id) {
|
||||
case MQTT_EVENT_BEFORE_CONNECT:
|
||||
|
@ -166,8 +166,9 @@ void MQTTBackendIDF::mqtt_event_handler_(const Event &event) {
|
|||
}
|
||||
|
||||
/// static - Dispatch event to instance method
|
||||
void MQTTBackendIDF::mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) {
|
||||
MQTTBackendIDF *instance = static_cast<MQTTBackendIDF *>(handler_args);
|
||||
void MQTTBackendESP32::mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id,
|
||||
void *event_data) {
|
||||
MQTTBackendESP32 *instance = static_cast<MQTTBackendESP32 *>(handler_args);
|
||||
// queue event to decouple processing
|
||||
if (instance) {
|
||||
auto event = *static_cast<esp_mqtt_event_t *>(event_data);
|
||||
|
@ -177,4 +178,4 @@ void MQTTBackendIDF::mqtt_event_handler(void *handler_args, esp_event_base_t bas
|
|||
|
||||
} // namespace mqtt
|
||||
} // namespace esphome
|
||||
#endif // USE_ESP_IDF
|
||||
#endif // USE_ESP32
|
|
@ -1,6 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#ifdef USE_ESP_IDF
|
||||
#ifdef USE_ESP32
|
||||
|
||||
#include <string>
|
||||
#include <queue>
|
||||
|
@ -41,7 +41,7 @@ struct Event {
|
|||
error_handle(*event.error_handle) {}
|
||||
};
|
||||
|
||||
class MQTTBackendIDF final : public MQTTBackend {
|
||||
class MQTTBackendESP32 final : public MQTTBackend {
|
||||
public:
|
||||
static const size_t MQTT_BUFFER_SIZE = 4096;
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#ifdef USE_ARDUINO
|
||||
#ifdef USE_ESP8266
|
||||
|
||||
#include "mqtt_backend.h"
|
||||
#include <AsyncMqttClient.h>
|
||||
|
@ -8,7 +8,7 @@
|
|||
namespace esphome {
|
||||
namespace mqtt {
|
||||
|
||||
class MQTTBackendArduino final : public MQTTBackend {
|
||||
class MQTTBackendESP8266 final : public MQTTBackend {
|
||||
public:
|
||||
void set_keep_alive(uint16_t keep_alive) final { mqtt_client_.setKeepAlive(keep_alive); }
|
||||
void set_client_id(const char *client_id) final { mqtt_client_.setClientId(client_id); }
|
||||
|
@ -71,4 +71,4 @@ class MQTTBackendArduino final : public MQTTBackend {
|
|||
} // namespace mqtt
|
||||
} // namespace esphome
|
||||
|
||||
#endif // defined(USE_ARDUINO)
|
||||
#endif // defined(USE_ESP8266)
|
|
@ -556,8 +556,8 @@ static bool topic_match(const char *message, const char *subscription) {
|
|||
}
|
||||
|
||||
void MQTTClientComponent::on_message(const std::string &topic, const std::string &payload) {
|
||||
#ifdef USE_ARDUINO
|
||||
// on Arduino, this is called in lwIP/AsyncTCP task; some components do not like running
|
||||
#ifdef USE_ESP8266
|
||||
// on ESP8266, this is called in lwIP/AsyncTCP task; some components do not like running
|
||||
// from a different task.
|
||||
this->defer([this, topic, payload]() {
|
||||
#endif
|
||||
|
@ -565,7 +565,7 @@ void MQTTClientComponent::on_message(const std::string &topic, const std::string
|
|||
if (topic_match(topic.c_str(), subscription.topic.c_str()))
|
||||
subscription.callback(topic, payload);
|
||||
}
|
||||
#ifdef USE_ARDUINO
|
||||
#ifdef USE_ESP8266
|
||||
});
|
||||
#endif
|
||||
}
|
||||
|
|
|
@ -9,10 +9,10 @@
|
|||
#include "esphome/core/log.h"
|
||||
#include "esphome/components/json/json_util.h"
|
||||
#include "esphome/components/network/ip_address.h"
|
||||
#if defined(USE_ESP_IDF)
|
||||
#include "mqtt_backend_idf.h"
|
||||
#elif defined(USE_ARDUINO)
|
||||
#include "mqtt_backend_arduino.h"
|
||||
#if defined(USE_ESP32)
|
||||
#include "mqtt_backend_esp32.h"
|
||||
#elif defined(USE_ESP8266)
|
||||
#include "mqtt_backend_esp8266.h"
|
||||
#endif
|
||||
#include "lwip/ip_addr.h"
|
||||
|
||||
|
@ -142,7 +142,7 @@ class MQTTClientComponent : public Component {
|
|||
*/
|
||||
void add_ssl_fingerprint(const std::array<uint8_t, SHA1_SIZE> &fingerprint);
|
||||
#endif
|
||||
#ifdef USE_ESP_IDF
|
||||
#ifdef USE_ESP32
|
||||
void set_ca_certificate(const char *cert) { this->mqtt_backend_.set_ca_certificate(cert); }
|
||||
void set_skip_cert_cn_check(bool skip_check) { this->mqtt_backend_.set_skip_cert_cn_check(skip_check); }
|
||||
#endif
|
||||
|
@ -296,10 +296,10 @@ class MQTTClientComponent : public Component {
|
|||
int log_level_{ESPHOME_LOG_LEVEL};
|
||||
|
||||
std::vector<MQTTSubscription> subscriptions_;
|
||||
#if defined(USE_ESP_IDF)
|
||||
MQTTBackendIDF mqtt_backend_;
|
||||
#elif defined(USE_ARDUINO)
|
||||
MQTTBackendArduino mqtt_backend_;
|
||||
#if defined(USE_ESP32)
|
||||
MQTTBackendESP32 mqtt_backend_;
|
||||
#elif defined(USE_ESP8266)
|
||||
MQTTBackendESP8266 mqtt_backend_;
|
||||
#endif
|
||||
|
||||
MQTTClientState state_{MQTT_CLIENT_DISCONNECTED};
|
||||
|
|
|
@ -57,7 +57,6 @@ lib_deps =
|
|||
${common.lib_deps}
|
||||
SPI ; spi (Arduino built-in)
|
||||
Wire ; i2c (Arduino built-int)
|
||||
ottowinter/AsyncMqttClient-esphome@0.8.6 ; mqtt
|
||||
esphome/ESPAsyncWebServer-esphome@2.1.0 ; web_server_base
|
||||
fastled/FastLED@3.3.2 ; fastled_base
|
||||
mikalhart/TinyGPSPlus@1.0.2 ; gps
|
||||
|
@ -89,6 +88,7 @@ lib_deps =
|
|||
${common:arduino.lib_deps}
|
||||
ESP8266WiFi ; wifi (Arduino built-in)
|
||||
Update ; ota (Arduino built-in)
|
||||
ottowinter/AsyncMqttClient-esphome@0.8.6 ; mqtt
|
||||
esphome/ESPAsyncTCP-esphome@1.2.3 ; async_tcp
|
||||
ESP8266HTTPClient ; http_request (Arduino built-in)
|
||||
ESP8266mDNS ; mdns (Arduino built-in)
|
||||
|
|
Loading…
Reference in a new issue