"esp_mqtt_client_register_event " and ESP_EVENT_ANY
Posted: Wed Sep 18, 2019 10:31 pm
Hi All,
I'm setting up an ESP32 device to connect to an RPi over WiFi. This connection is successful.
Next, I try to connect to the MQTT broker running on the RPi (Mosquitto).
According to the example here, @line 122, we can use the "ESP_EVENT_ANY_ID" for the second parameter (mqtt_event_id_t), however this gives a compiler error :
part of my app_main (which is in a .cpp file) :
What is the solution to capture MQTT events in ESP_IDF V4.X ?
thanks in advance,
Paul
I'm setting up an ESP32 device to connect to an RPi over WiFi. This connection is successful.
Next, I try to connect to the MQTT broker running on the RPi (Mosquitto).
According to the example here, @line 122, we can use the "ESP_EVENT_ANY_ID" for the second parameter (mqtt_event_id_t), however this gives a compiler error :
Code: Select all
/CMakeFiles/__idf_main.dir/main.cpp.obj -c ../main/main.cpp
In file included from /home/paulvdbergh/esp/esp-idf/components/esp_event/include/esp_event.h:25,
from ../main/main.cpp:15:
../main/main.cpp: In function 'void app_main()':
/home/paulvdbergh/esp/esp-idf/components/esp_event/include/esp_event_base.h:37:32: error: invalid conversion from 'int' to 'esp_mqtt_event_id_t' [-fpermissive]
#define ESP_EVENT_ANY_ID -1 /**< register handler for any event id */
^~
../main/main.cpp:94:41: note: in expansion of macro 'ESP_EVENT_ANY_ID'
esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, client);
^~~~~~~~~~~~~~~~
In file included from ../main/main.cpp:19:
/home/paulvdbergh/esp/esp-idf/components/mqtt/esp-mqtt/include/mqtt_client.h:261:95: note: initializing argument 2 of 'esp_err_t esp_mqtt_client_register_event(esp_mqtt_client_handle_t, esp_mqtt_event_id_t, esp_event_handler_t, void*)'
esp_err_t esp_mqtt_client_register_event(esp_mqtt_client_handle_t client, esp_mqtt_event_id_t event, esp_event_handler_t event_handler, void* event_handler_arg);
~~~~~~~~~~~~~~~~~~~~^~~~~
[827/835] /home/paulvdbergh/.espressif/tools/xtensa-esp32-elf/esp32-2019r1-8.2.0/xtensa-esp32-elf/bin/xtensa-esp32-elf-gcc
[
Code: Select all
extern "C"
void app_main()
{
wifi_event_group = xEventGroupCreate();
//Initialize NVS
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
{
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
tcpip_adapter_init();
ESP_ERROR_CHECK(esp_event_loop_create_default());
// Connect to WiFi
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, nullptr));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL));
wifi_config_t wifi_cfg;
memset(&wifi_cfg, 0, sizeof(wifi_config_t));
strncpy((char*)wifi_cfg.sta.ssid, "IoTT_Buster", sizeof(wifi_cfg.sta.ssid));
strncpy((char*)wifi_cfg.sta.password, "IoTT_ESP32", sizeof(wifi_cfg.sta.password));
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_cfg));
ESP_ERROR_CHECK(esp_wifi_start());
ESP_LOGI(__FUNCTION__, "Waiting for wifi...");
xEventGroupWaitBits(wifi_event_group, WIFI_CONNECTED_BIT, false, true, portMAX_DELAY);
ESP_LOGI(__FUNCTION__, "Connected to wifi.");
// Connect to MQTT Broker
esp_mqtt_client_config_t mqtt_cfg;
memset(&mqtt_cfg, 0, sizeof(esp_mqtt_client_config_t));
mqtt_cfg.uri = "mqtt://192.168.4.1";
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);
}
thanks in advance,
Paul