Change MQTT client for ESP32 Arduino (#5157)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
Jimmy Hedman 2023-08-07 23:16:42 +02:00 committed by GitHub
parent 93b7ca77ca
commit 9980b9972f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 29 additions and 28 deletions

View file

@ -271,8 +271,8 @@ def exp_mqtt_message(config):
async def to_code(config): async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID]) var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config) await cg.register_component(var, config)
# Add required libraries for arduino # Add required libraries for ESP8266
if CORE.using_arduino: if CORE.is_esp8266:
# https://github.com/OttoWinter/async-mqtt-client/blob/master/library.json # https://github.com/OttoWinter/async-mqtt-client/blob/master/library.json
cg.add_library("ottowinter/AsyncMqttClient-esphome", "0.8.6") cg.add_library("ottowinter/AsyncMqttClient-esphome", "0.8.6")

View file

@ -1,7 +1,7 @@
#ifdef USE_ESP_IDF #ifdef USE_ESP32
#include <string> #include <string>
#include "mqtt_backend_idf.h" #include "mqtt_backend_esp32.h"
#include "esphome/core/log.h" #include "esphome/core/log.h"
#include "esphome/core/helpers.h" #include "esphome/core/helpers.h"
@ -10,7 +10,7 @@ namespace mqtt {
static const char *const TAG = "mqtt.idf"; static const char *const TAG = "mqtt.idf";
bool MQTTBackendIDF::initialize_() { bool MQTTBackendESP32::initialize_() {
#if ESP_IDF_VERSION_MAJOR < 5 #if ESP_IDF_VERSION_MAJOR < 5
mqtt_cfg_.user_context = (void *) this; mqtt_cfg_.user_context = (void *) this;
mqtt_cfg_.buffer_size = MQTT_BUFFER_SIZE; mqtt_cfg_.buffer_size = MQTT_BUFFER_SIZE;
@ -95,7 +95,7 @@ bool MQTTBackendIDF::initialize_() {
} }
} }
void MQTTBackendIDF::loop() { void MQTTBackendESP32::loop() {
// process new events // process new events
// handle only 1 message per loop iteration // handle only 1 message per loop iteration
if (!mqtt_events_.empty()) { 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); ESP_LOGV(TAG, "Event dispatched from event loop event_id=%d", event.event_id);
switch (event.event_id) { switch (event.event_id) {
case MQTT_EVENT_BEFORE_CONNECT: case MQTT_EVENT_BEFORE_CONNECT:
@ -166,8 +166,9 @@ void MQTTBackendIDF::mqtt_event_handler_(const Event &event) {
} }
/// static - Dispatch event to instance method /// 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) { void MQTTBackendESP32::mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id,
MQTTBackendIDF *instance = static_cast<MQTTBackendIDF *>(handler_args); void *event_data) {
MQTTBackendESP32 *instance = static_cast<MQTTBackendESP32 *>(handler_args);
// queue event to decouple processing // queue event to decouple processing
if (instance) { if (instance) {
auto event = *static_cast<esp_mqtt_event_t *>(event_data); 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 mqtt
} // namespace esphome } // namespace esphome
#endif // USE_ESP_IDF #endif // USE_ESP32

View file

@ -1,6 +1,6 @@
#pragma once #pragma once
#ifdef USE_ESP_IDF #ifdef USE_ESP32
#include <string> #include <string>
#include <queue> #include <queue>
@ -41,7 +41,7 @@ struct Event {
error_handle(*event.error_handle) {} error_handle(*event.error_handle) {}
}; };
class MQTTBackendIDF final : public MQTTBackend { class MQTTBackendESP32 final : public MQTTBackend {
public: public:
static const size_t MQTT_BUFFER_SIZE = 4096; static const size_t MQTT_BUFFER_SIZE = 4096;

View file

@ -1,6 +1,6 @@
#pragma once #pragma once
#ifdef USE_ARDUINO #ifdef USE_ESP8266
#include "mqtt_backend.h" #include "mqtt_backend.h"
#include <AsyncMqttClient.h> #include <AsyncMqttClient.h>
@ -8,7 +8,7 @@
namespace esphome { namespace esphome {
namespace mqtt { namespace mqtt {
class MQTTBackendArduino final : public MQTTBackend { class MQTTBackendESP8266 final : public MQTTBackend {
public: public:
void set_keep_alive(uint16_t keep_alive) final { mqtt_client_.setKeepAlive(keep_alive); } 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); } 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 mqtt
} // namespace esphome } // namespace esphome
#endif // defined(USE_ARDUINO) #endif // defined(USE_ESP8266)

View file

@ -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) { void MQTTClientComponent::on_message(const std::string &topic, const std::string &payload) {
#ifdef USE_ARDUINO #ifdef USE_ESP8266
// on Arduino, this is called in lwIP/AsyncTCP task; some components do not like running // on ESP8266, this is called in lwIP/AsyncTCP task; some components do not like running
// from a different task. // from a different task.
this->defer([this, topic, payload]() { this->defer([this, topic, payload]() {
#endif #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())) if (topic_match(topic.c_str(), subscription.topic.c_str()))
subscription.callback(topic, payload); subscription.callback(topic, payload);
} }
#ifdef USE_ARDUINO #ifdef USE_ESP8266
}); });
#endif #endif
} }

View file

@ -9,10 +9,10 @@
#include "esphome/core/log.h" #include "esphome/core/log.h"
#include "esphome/components/json/json_util.h" #include "esphome/components/json/json_util.h"
#include "esphome/components/network/ip_address.h" #include "esphome/components/network/ip_address.h"
#if defined(USE_ESP_IDF) #if defined(USE_ESP32)
#include "mqtt_backend_idf.h" #include "mqtt_backend_esp32.h"
#elif defined(USE_ARDUINO) #elif defined(USE_ESP8266)
#include "mqtt_backend_arduino.h" #include "mqtt_backend_esp8266.h"
#endif #endif
#include "lwip/ip_addr.h" #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); void add_ssl_fingerprint(const std::array<uint8_t, SHA1_SIZE> &fingerprint);
#endif #endif
#ifdef USE_ESP_IDF #ifdef USE_ESP32
void set_ca_certificate(const char *cert) { this->mqtt_backend_.set_ca_certificate(cert); } 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); } void set_skip_cert_cn_check(bool skip_check) { this->mqtt_backend_.set_skip_cert_cn_check(skip_check); }
#endif #endif
@ -296,10 +296,10 @@ class MQTTClientComponent : public Component {
int log_level_{ESPHOME_LOG_LEVEL}; int log_level_{ESPHOME_LOG_LEVEL};
std::vector<MQTTSubscription> subscriptions_; std::vector<MQTTSubscription> subscriptions_;
#if defined(USE_ESP_IDF) #if defined(USE_ESP32)
MQTTBackendIDF mqtt_backend_; MQTTBackendESP32 mqtt_backend_;
#elif defined(USE_ARDUINO) #elif defined(USE_ESP8266)
MQTTBackendArduino mqtt_backend_; MQTTBackendESP8266 mqtt_backend_;
#endif #endif
MQTTClientState state_{MQTT_CLIENT_DISCONNECTED}; MQTTClientState state_{MQTT_CLIENT_DISCONNECTED};

View file

@ -57,7 +57,6 @@ lib_deps =
${common.lib_deps} ${common.lib_deps}
SPI ; spi (Arduino built-in) SPI ; spi (Arduino built-in)
Wire ; i2c (Arduino built-int) Wire ; i2c (Arduino built-int)
ottowinter/AsyncMqttClient-esphome@0.8.6 ; mqtt
esphome/ESPAsyncWebServer-esphome@2.1.0 ; web_server_base esphome/ESPAsyncWebServer-esphome@2.1.0 ; web_server_base
fastled/FastLED@3.3.2 ; fastled_base fastled/FastLED@3.3.2 ; fastled_base
mikalhart/TinyGPSPlus@1.0.2 ; gps mikalhart/TinyGPSPlus@1.0.2 ; gps
@ -89,6 +88,7 @@ lib_deps =
${common:arduino.lib_deps} ${common:arduino.lib_deps}
ESP8266WiFi ; wifi (Arduino built-in) ESP8266WiFi ; wifi (Arduino built-in)
Update ; ota (Arduino built-in) Update ; ota (Arduino built-in)
ottowinter/AsyncMqttClient-esphome@0.8.6 ; mqtt
esphome/ESPAsyncTCP-esphome@1.2.3 ; async_tcp esphome/ESPAsyncTCP-esphome@1.2.3 ; async_tcp
ESP8266HTTPClient ; http_request (Arduino built-in) ESP8266HTTPClient ; http_request (Arduino built-in)
ESP8266mDNS ; mdns (Arduino built-in) ESP8266mDNS ; mdns (Arduino built-in)