13 static const char *
const TAG =
"mqtt.idf";
16 #if ESP_IDF_VERSION_MAJOR < 5 49 mqtt_cfg_.transport = MQTT_TRANSPORT_OVER_SSL;
56 mqtt_cfg_.transport = MQTT_TRANSPORT_OVER_TCP;
88 mqtt_cfg_.broker.address.transport = MQTT_TRANSPORT_OVER_SSL;
95 mqtt_cfg_.broker.address.transport = MQTT_TRANSPORT_OVER_TCP;
98 auto *mqtt_client = esp_mqtt_client_init(&
mqtt_cfg_);
102 esp_mqtt_client_register_event(mqtt_client, MQTT_EVENT_ANY,
mqtt_event_handler,
this);
105 ESP_LOGE(TAG,
"Failed to initialize IDF-MQTT");
121 ESP_LOGV(TAG,
"Event dispatched from event loop event_id=%d", event.event_id);
122 switch (event.event_id) {
123 case MQTT_EVENT_BEFORE_CONNECT:
124 ESP_LOGV(TAG,
"MQTT_EVENT_BEFORE_CONNECT");
127 case MQTT_EVENT_CONNECTED:
128 ESP_LOGV(TAG,
"MQTT_EVENT_CONNECTED");
132 case MQTT_EVENT_DISCONNECTED:
133 ESP_LOGV(TAG,
"MQTT_EVENT_DISCONNECTED");
139 case MQTT_EVENT_SUBSCRIBED:
140 ESP_LOGV(TAG,
"MQTT_EVENT_SUBSCRIBED, msg_id=%d", event.msg_id);
144 case MQTT_EVENT_UNSUBSCRIBED:
145 ESP_LOGV(TAG,
"MQTT_EVENT_UNSUBSCRIBED, msg_id=%d", event.msg_id);
148 case MQTT_EVENT_PUBLISHED:
149 ESP_LOGV(TAG,
"MQTT_EVENT_PUBLISHED, msg_id=%d", event.msg_id);
152 case MQTT_EVENT_DATA: {
153 static std::string topic;
154 if (!event.topic.empty()) {
157 ESP_LOGV(TAG,
"MQTT_EVENT_DATA %s", topic.c_str());
158 this->
on_message_.call(!event.topic.empty() ? topic.c_str() :
nullptr,
event.data.data(),
event.data.size(),
159 event.current_data_offset,
event.total_data_len);
161 case MQTT_EVENT_ERROR:
162 ESP_LOGE(TAG,
"MQTT_EVENT_ERROR");
163 if (event.error_handle.error_type == MQTT_ERROR_TYPE_TCP_TRANSPORT) {
164 ESP_LOGE(TAG,
"Last error code reported from esp-tls: 0x%x", event.error_handle.esp_tls_last_esp_err);
165 ESP_LOGE(TAG,
"Last tls stack error number: 0x%x", event.error_handle.esp_tls_stack_err);
166 ESP_LOGE(TAG,
"Last captured errno : %d (%s)", event.error_handle.esp_transport_sock_errno,
167 strerror(event.error_handle.esp_transport_sock_errno));
168 }
else if (event.error_handle.error_type == MQTT_ERROR_TYPE_CONNECTION_REFUSED) {
169 ESP_LOGE(TAG,
"Connection refused error: 0x%x", event.error_handle.connect_return_code);
171 ESP_LOGE(TAG,
"Unknown error type: 0x%x", event.error_handle.error_type);
175 ESP_LOGV(TAG,
"Other event id:%d", event.event_id);
186 auto event = *
static_cast<esp_mqtt_event_t *
>(event_data);
value_type const & value() const
CallbackManager< on_subscribe_callback_t > on_subscribe_
void mqtt_event_handler_(const Event &event)
optional< std::string > ca_certificate_
CallbackManager< on_unsubscribe_callback_t > on_unsubscribe_
std::queue< Event > mqtt_events_
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
static const size_t MQTT_BUFFER_SIZE
CallbackManager< on_connect_callback_t > on_connect_
CallbackManager< on_message_callback_t > on_message_
CallbackManager< on_publish_user_callback_t > on_publish_
Implementation of SPI Controller mode.
CallbackManager< on_disconnect_callback_t > on_disconnect_
esp_mqtt_client_config_t mqtt_cfg_
optional< std::string > cl_certificate_
optional< std::string > cl_key_