From 9980b9972fcac080a1eb1987107484644e7a8d1d Mon Sep 17 00:00:00 2001 From: Jimmy Hedman Date: Mon, 7 Aug 2023 23:16:42 +0200 Subject: [PATCH] Change MQTT client for ESP32 Arduino (#5157) Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com> --- esphome/components/mqtt/__init__.py | 4 ++-- ..._backend_idf.cpp => mqtt_backend_esp32.cpp} | 17 +++++++++-------- ...mqtt_backend_idf.h => mqtt_backend_esp32.h} | 4 ++-- ...ackend_arduino.h => mqtt_backend_esp8266.h} | 6 +++--- esphome/components/mqtt/mqtt_client.cpp | 6 +++--- esphome/components/mqtt/mqtt_client.h | 18 +++++++++--------- platformio.ini | 2 +- 7 files changed, 29 insertions(+), 28 deletions(-) rename esphome/components/mqtt/{mqtt_backend_idf.cpp => mqtt_backend_esp32.cpp} (93%) rename esphome/components/mqtt/{mqtt_backend_idf.h => mqtt_backend_esp32.h} (98%) rename esphome/components/mqtt/{mqtt_backend_arduino.h => mqtt_backend_esp8266.h} (96%) diff --git a/esphome/components/mqtt/__init__.py b/esphome/components/mqtt/__init__.py index 7207eaddc1..102c070eb6 100644 --- a/esphome/components/mqtt/__init__.py +++ b/esphome/components/mqtt/__init__.py @@ -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") diff --git a/esphome/components/mqtt/mqtt_backend_idf.cpp b/esphome/components/mqtt/mqtt_backend_esp32.cpp similarity index 93% rename from esphome/components/mqtt/mqtt_backend_idf.cpp rename to esphome/components/mqtt/mqtt_backend_esp32.cpp index 7a7aca3fa6..2d4e6802f2 100644 --- a/esphome/components/mqtt/mqtt_backend_idf.cpp +++ b/esphome/components/mqtt/mqtt_backend_esp32.cpp @@ -1,7 +1,7 @@ -#ifdef USE_ESP_IDF +#ifdef USE_ESP32 #include -#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(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(handler_args); // queue event to decouple processing if (instance) { auto event = *static_cast(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 diff --git a/esphome/components/mqtt/mqtt_backend_idf.h b/esphome/components/mqtt/mqtt_backend_esp32.h similarity index 98% rename from esphome/components/mqtt/mqtt_backend_idf.h rename to esphome/components/mqtt/mqtt_backend_esp32.h index 9c7a5f80e9..a4ee96ca59 100644 --- a/esphome/components/mqtt/mqtt_backend_idf.h +++ b/esphome/components/mqtt/mqtt_backend_esp32.h @@ -1,6 +1,6 @@ #pragma once -#ifdef USE_ESP_IDF +#ifdef USE_ESP32 #include #include @@ -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; diff --git a/esphome/components/mqtt/mqtt_backend_arduino.h b/esphome/components/mqtt/mqtt_backend_esp8266.h similarity index 96% rename from esphome/components/mqtt/mqtt_backend_arduino.h rename to esphome/components/mqtt/mqtt_backend_esp8266.h index 6399ec88e0..2d91877e9d 100644 --- a/esphome/components/mqtt/mqtt_backend_arduino.h +++ b/esphome/components/mqtt/mqtt_backend_esp8266.h @@ -1,6 +1,6 @@ #pragma once -#ifdef USE_ARDUINO +#ifdef USE_ESP8266 #include "mqtt_backend.h" #include @@ -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) diff --git a/esphome/components/mqtt/mqtt_client.cpp b/esphome/components/mqtt/mqtt_client.cpp index cb5d306976..d3f759c072 100644 --- a/esphome/components/mqtt/mqtt_client.cpp +++ b/esphome/components/mqtt/mqtt_client.cpp @@ -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 } diff --git a/esphome/components/mqtt/mqtt_client.h b/esphome/components/mqtt/mqtt_client.h index 83ed3cc645..00eb3fdd40 100644 --- a/esphome/components/mqtt/mqtt_client.h +++ b/esphome/components/mqtt/mqtt_client.h @@ -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 &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 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}; diff --git a/platformio.ini b/platformio.ini index 8141c803f1..ba149ce99e 100644 --- a/platformio.ini +++ b/platformio.ini @@ -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)