Merge branch 'feature/mqtt_support_event_loop' into 'master'

MQTT: Support for esp event loop

See merge request idf/esp-idf!4815
This commit is contained in:
Angus Gratton 2019-06-05 11:04:16 +08:00
commit 826ff7186a
6 changed files with 37 additions and 14 deletions

@ -1 +1 @@
Subproject commit 18b6f2c58231728805ff813d46021d07ed023dcb Subproject commit e205913b2cf3eff20b1f8ced7c76cf03533f130b

View file

@ -56,9 +56,13 @@ URI
const esp_mqtt_client_config_t mqtt_cfg = { const esp_mqtt_client_config_t mqtt_cfg = {
.uri = "mqtt://iot.eclipse.org", .uri = "mqtt://iot.eclipse.org",
.event_handle = mqtt_event_handler,
// .user_context = (void *)your_context // .user_context = (void *)your_context
}; };
esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);
esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client);
esp_mqtt_client_start(client);
- Note: By default mqtt client uses event loop library to post related mqtt events (connected, subsribed, published, etc.)
- If there are any options related to the URI in - If there are any options related to the URI in
``esp_mqtt_client_config_t``, the option defined by the URI will be ``esp_mqtt_client_config_t``, the option defined by the URI will be
@ -68,7 +72,6 @@ URI
const esp_mqtt_client_config_t mqtt_cfg = { const esp_mqtt_client_config_t mqtt_cfg = {
.uri = "mqtt://iot.eclipse.org:1234", .uri = "mqtt://iot.eclipse.org:1234",
.event_handle = mqtt_event_handler,
.port = 4567, .port = 4567,
}; };
//MQTT client will connect to iot.eclipse.org using port 4567 //MQTT client will connect to iot.eclipse.org using port 4567

View file

@ -40,7 +40,7 @@ extern const uint8_t iot_eclipse_org_pem_start[] asm("_binary_iot_eclipse_org_
#endif #endif
extern const uint8_t iot_eclipse_org_pem_end[] asm("_binary_iot_eclipse_org_pem_end"); extern const uint8_t iot_eclipse_org_pem_end[] asm("_binary_iot_eclipse_org_pem_end");
static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event)
{ {
esp_mqtt_client_handle_t client = event->client; esp_mqtt_client_handle_t client = event->client;
int msg_id; int msg_id;
@ -87,17 +87,21 @@ static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event)
return ESP_OK; return ESP_OK;
} }
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) {
ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id);
mqtt_event_handler_cb(event_data);
}
static void mqtt_app_start(void) static void mqtt_app_start(void)
{ {
const esp_mqtt_client_config_t mqtt_cfg = { const esp_mqtt_client_config_t mqtt_cfg = {
.uri = CONFIG_BROKER_URI, .uri = CONFIG_BROKER_URI,
.event_handle = mqtt_event_handler,
.cert_pem = (const char *)iot_eclipse_org_pem_start, .cert_pem = (const char *)iot_eclipse_org_pem_start,
}; };
ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size());
esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);
esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client);
esp_mqtt_client_start(client); esp_mqtt_client_start(client);
} }
@ -109,6 +113,7 @@ void app_main()
esp_log_level_set("*", ESP_LOG_INFO); esp_log_level_set("*", ESP_LOG_INFO);
esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE); esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE);
esp_log_level_set("MQTT_EXAMPLE", ESP_LOG_VERBOSE);
esp_log_level_set("TRANSPORT_TCP", ESP_LOG_VERBOSE); esp_log_level_set("TRANSPORT_TCP", ESP_LOG_VERBOSE);
esp_log_level_set("TRANSPORT_SSL", ESP_LOG_VERBOSE); esp_log_level_set("TRANSPORT_SSL", ESP_LOG_VERBOSE);
esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE);

View file

@ -33,7 +33,7 @@
static const char *TAG = "MQTT_EXAMPLE"; static const char *TAG = "MQTT_EXAMPLE";
static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event)
{ {
esp_mqtt_client_handle_t client = event->client; esp_mqtt_client_handle_t client = event->client;
int msg_id; int msg_id;
@ -83,14 +83,16 @@ static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event)
return ESP_OK; return ESP_OK;
} }
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) {
ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id);
mqtt_event_handler_cb(event_data);
}
static void mqtt_app_start(void) static void mqtt_app_start(void)
{ {
esp_mqtt_client_config_t mqtt_cfg = { esp_mqtt_client_config_t mqtt_cfg = {
.uri = CONFIG_BROKER_URL, .uri = CONFIG_BROKER_URL,
.event_handle = mqtt_event_handler,
// .user_context = (void *)your_context
}; };
#if CONFIG_BROKER_URL_FROM_STDIN #if CONFIG_BROKER_URL_FROM_STDIN
char line[128]; char line[128];
@ -117,6 +119,7 @@ static void mqtt_app_start(void)
#endif /* CONFIG_BROKER_URL_FROM_STDIN */ #endif /* CONFIG_BROKER_URL_FROM_STDIN */
esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);
esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client);
esp_mqtt_client_start(client); esp_mqtt_client_start(client);
} }
@ -128,6 +131,7 @@ void app_main()
esp_log_level_set("*", ESP_LOG_INFO); esp_log_level_set("*", ESP_LOG_INFO);
esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE); esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE);
esp_log_level_set("MQTT_EXAMPLE", ESP_LOG_VERBOSE);
esp_log_level_set("TRANSPORT_TCP", ESP_LOG_VERBOSE); esp_log_level_set("TRANSPORT_TCP", ESP_LOG_VERBOSE);
esp_log_level_set("TRANSPORT_SSL", ESP_LOG_VERBOSE); esp_log_level_set("TRANSPORT_SSL", ESP_LOG_VERBOSE);
esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE);

View file

@ -32,7 +32,7 @@
static const char *TAG = "MQTTWS_EXAMPLE"; static const char *TAG = "MQTTWS_EXAMPLE";
static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event)
{ {
esp_mqtt_client_handle_t client = event->client; esp_mqtt_client_handle_t client = event->client;
int msg_id; int msg_id;
@ -79,16 +79,19 @@ static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event)
return ESP_OK; return ESP_OK;
} }
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) {
ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id);
mqtt_event_handler_cb(event_data);
}
static void mqtt_app_start(void) static void mqtt_app_start(void)
{ {
const esp_mqtt_client_config_t mqtt_cfg = { const esp_mqtt_client_config_t mqtt_cfg = {
.uri = CONFIG_BROKER_URI, .uri = CONFIG_BROKER_URI,
.event_handle = mqtt_event_handler,
// .user_context = (void *)your_context
}; };
esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);
esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client);
esp_mqtt_client_start(client); esp_mqtt_client_start(client);
} }
@ -100,6 +103,7 @@ void app_main()
esp_log_level_set("*", ESP_LOG_INFO); esp_log_level_set("*", ESP_LOG_INFO);
esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE); esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE);
esp_log_level_set("MQTT_EXAMPLE", ESP_LOG_VERBOSE);
esp_log_level_set("TRANSPORT_TCP", ESP_LOG_VERBOSE); esp_log_level_set("TRANSPORT_TCP", ESP_LOG_VERBOSE);
esp_log_level_set("TRANSPORT_SSL", ESP_LOG_VERBOSE); esp_log_level_set("TRANSPORT_SSL", ESP_LOG_VERBOSE);
esp_log_level_set("TRANSPORT_WS", ESP_LOG_VERBOSE); esp_log_level_set("TRANSPORT_WS", ESP_LOG_VERBOSE);

View file

@ -39,7 +39,7 @@ extern const uint8_t iot_eclipse_org_pem_start[] asm("_binary_iot_eclipse_org_
#endif #endif
extern const uint8_t iot_eclipse_org_pem_end[] asm("_binary_iot_eclipse_org_pem_end"); extern const uint8_t iot_eclipse_org_pem_end[] asm("_binary_iot_eclipse_org_pem_end");
static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event) static esp_err_t mqtt_event_handler_cb(esp_mqtt_event_handle_t event)
{ {
esp_mqtt_client_handle_t client = event->client; esp_mqtt_client_handle_t client = event->client;
int msg_id; int msg_id;
@ -86,16 +86,22 @@ static esp_err_t mqtt_event_handler(esp_mqtt_event_handle_t event)
return ESP_OK; return ESP_OK;
} }
static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data) {
ESP_LOGD(TAG, "Event dispatched from event loop base=%s, event_id=%d", base, event_id);
mqtt_event_handler_cb(event_data);
}
static void mqtt_app_start(void) static void mqtt_app_start(void)
{ {
const esp_mqtt_client_config_t mqtt_cfg = { const esp_mqtt_client_config_t mqtt_cfg = {
.uri = CONFIG_BROKER_URI, .uri = CONFIG_BROKER_URI,
.event_handle = mqtt_event_handler,
.cert_pem = (const char *)iot_eclipse_org_pem_start, .cert_pem = (const char *)iot_eclipse_org_pem_start,
}; };
ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size()); ESP_LOGI(TAG, "[APP] Free memory: %d bytes", esp_get_free_heap_size());
esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg); esp_mqtt_client_handle_t client = esp_mqtt_client_init(&mqtt_cfg);
esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client);
esp_mqtt_client_start(client); esp_mqtt_client_start(client);
} }
@ -107,6 +113,7 @@ void app_main()
esp_log_level_set("*", ESP_LOG_INFO); esp_log_level_set("*", ESP_LOG_INFO);
esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE); esp_log_level_set("MQTT_CLIENT", ESP_LOG_VERBOSE);
esp_log_level_set("MQTT_EXAMPLE", ESP_LOG_VERBOSE);
esp_log_level_set("TRANSPORT_TCP", ESP_LOG_VERBOSE); esp_log_level_set("TRANSPORT_TCP", ESP_LOG_VERBOSE);
esp_log_level_set("TRANSPORT_SSL", ESP_LOG_VERBOSE); esp_log_level_set("TRANSPORT_SSL", ESP_LOG_VERBOSE);
esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE); esp_log_level_set("TRANSPORT", ESP_LOG_VERBOSE);