11#ifdef USE_MQTT_CERT_BUNDLE
12#include "esp_crt_bundle.h"
49#ifdef USE_MQTT_CERT_BUNDLE
50 mqtt_cfg_.broker.verification.crt_bundle_attach = esp_crt_bundle_attach;
59 mqtt_cfg_.broker.address.transport = MQTT_TRANSPORT_OVER_WS;
62 mqtt_cfg_.broker.address.transport = MQTT_TRANSPORT_OVER_WSS;
67 mqtt_cfg_.broker.address.transport = MQTT_TRANSPORT_OVER_SSL;
69 mqtt_cfg_.broker.address.transport = MQTT_TRANSPORT_OVER_TCP;
76 auto *mqtt_client = esp_mqtt_client_init(&
mqtt_cfg_);
81#if defined(USE_MQTT_IDF_ENQUEUE)
91 ESP_LOGE(TAG,
"Failed to create MQTT task");
102 ESP_LOGE(TAG,
"Failed to init client");
111 if (event !=
nullptr) {
117 uint16_t inbound_dropped = this->
mqtt_event_queue_.get_and_reset_dropped_count();
118 if (inbound_dropped > 0) {
119 ESP_LOGW(TAG,
"Dropped %u inbound MQTT events", inbound_dropped);
122#if defined(USE_MQTT_IDF_ENQUEUE)
134 uint16_t dropped = this->
mqtt_queue_.get_and_reset_dropped_count();
144 ESP_LOGV(TAG,
"Event dispatched from event loop event_id=%d", event.event_id);
145 switch (event.event_id) {
146 case MQTT_EVENT_BEFORE_CONNECT:
147 ESP_LOGV(TAG,
"MQTT_EVENT_BEFORE_CONNECT");
150 case MQTT_EVENT_CONNECTED:
151 ESP_LOGV(TAG,
"MQTT_EVENT_CONNECTED");
153#if defined(USE_MQTT_IDF_ENQUEUE)
159 case MQTT_EVENT_DISCONNECTED:
160 ESP_LOGV(TAG,
"MQTT_EVENT_DISCONNECTED");
163#if defined(USE_MQTT_IDF_ENQUEUE)
170 case MQTT_EVENT_SUBSCRIBED:
171 ESP_LOGV(TAG,
"MQTT_EVENT_SUBSCRIBED, msg_id=%d", event.msg_id);
175 case MQTT_EVENT_UNSUBSCRIBED:
176 ESP_LOGV(TAG,
"MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event.msg_id);
179 case MQTT_EVENT_PUBLISHED:
180 ESP_LOGV(TAG,
"MQTT_EVENT_PUBLISHED, msg_id=%d", event.msg_id);
183 case MQTT_EVENT_DATA: {
184 if (!event.topic.empty()) {
191 ESP_LOGV(TAG,
"MQTT_EVENT_DATA %s", this->
cached_topic_.c_str());
193 event.current_data_offset, event.total_data_len);
195 case MQTT_EVENT_ERROR:
196 ESP_LOGE(TAG,
"MQTT_EVENT_ERROR");
197 if (event.error_handle.error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT) {
198 ESP_LOGE(TAG,
"Last esp-tls error: 0x%x, tls stack error: 0x%x, socket errno: %d (%s)",
199 event.error_handle.esp_tls_last_esp_err, event.error_handle.esp_tls_stack_err,
200 event.error_handle.esp_transport_sock_errno, strerror(event.error_handle.esp_transport_sock_errno));
201 }
else if (event.error_handle.error_type == MQTT_ERROR_TYPE_CONNECTION_REFUSED) {
202 ESP_LOGE(TAG,
"Connection refused error: 0x%x", event.error_handle.connect_return_code);
204 ESP_LOGE(TAG,
"Unknown error type: 0x%x", event.error_handle.error_type);
208 ESP_LOGV(TAG,
"Other event id:%d", event.event_id);
220 if (event ==
nullptr) {
225 event->populate(*
static_cast<esp_mqtt_event_t *
>(event_data));
235#if defined(USE_MQTT_IDF_ENQUEUE)
241 ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
245 while ((elem = this_mqtt->
mqtt_queue_.pop()) !=
nullptr) {
247 switch (elem->
type) {
249 esp_mqtt_client_subscribe(this_mqtt->
handler_.get(), elem->
topic, elem->
qos);
253 esp_mqtt_client_unsubscribe(this_mqtt->
handler_.get(), elem->
topic);
262 ESP_LOGE(TAG,
"Invalid operation type from MQTT queue");
288 elem->retain = retain;
291 if (!elem->set_data(topic, payload,
len)) {
void wake_loop_threadsafe()
Wake the main event loop from another thread or callback.
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
EventPool< Event, MQTT_EVENT_QUEUE_LENGTH - 1 > mqtt_event_pool_
EventPool< struct QueueElement, MQTT_QUEUE_LENGTH - 1 > mqtt_outbound_pool_
CallbackManager< on_connect_callback_t > on_connect_
CallbackManager< on_disconnect_callback_t > on_disconnect_
CallbackManager< on_message_callback_t > on_message_
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
static - Dispatch event to instance method
const char * cl_certificate_
CallbackManager< on_subscribe_callback_t > on_subscribe_
static void esphome_mqtt_task(void *params)
bool enqueue_(MqttQueueTypeT type, const char *topic, int qos=0, bool retain=false, const char *payload=NULL, size_t len=0)
const char * ca_certificate_
static constexpr size_t TASK_STACK_SIZE_TLS
CallbackManager< on_unsubscribe_callback_t > on_unsubscribe_
std::string cached_topic_
esp_mqtt_client_config_t mqtt_cfg_
TaskHandle_t task_handle_
static constexpr size_t TASK_STACK_SIZE
CallbackManager< on_publish_user_callback_t > on_publish_
static constexpr uint32_t DROP_LOG_INTERVAL_MS
NotifyingLockFreeQueue< struct QueueElement, MQTT_QUEUE_LENGTH > mqtt_queue_
uint32_t last_dropped_log_time_
void mqtt_event_handler_(const Event &event)
static constexpr ssize_t TASK_PRIORITY
LockFreeQueue< Event, MQTT_EVENT_QUEUE_LENGTH > mqtt_event_queue_
ESPHOME_LOG_TAG(TAG, "alarm_control_panel")
@ MQTT_QUEUE_TYPE_SUBSCRIBE
@ MQTT_QUEUE_TYPE_UNSUBSCRIBE
@ MQTT_QUEUE_TYPE_PUBLISH
Application App
Global storage of Application pointer - only one Application can exist.