"esp_mqtt_client_register_event " and ESP_EVENT_ANY

User avatar
PaulVdBergh
Posts: 58
Joined: Fri Feb 23, 2018 4:45 pm
Location: Brasschaat, Belgium

"esp_mqtt_client_register_event " and ESP_EVENT_ANY

Postby PaulVdBergh » 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 :

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 
[
part of my app_main (which is in a .cpp file) :

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);

}
What is the solution to capture MQTT events in ESP_IDF V4.X ?

thanks in advance,

Paul

User avatar
PaulVdBergh
Posts: 58
Joined: Fri Feb 23, 2018 4:45 pm
Location: Brasschaat, Belgium

Re: "esp_mqtt_client_register_event " and ESP_EVENT_ANY

Postby PaulVdBergh » Thu Sep 19, 2019 6:44 pm

Hi all,

I changed the code to :

Code: Select all

//	Connect to MQTT Broker

	ESP_LOGI(__FUNCTION__, "Waiting for MQTT...");
	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, MQTT_EVENT_ERROR, mqtt_event_handler, client);
	esp_mqtt_client_register_event(client, MQTT_EVENT_CONNECTED, mqtt_event_handler, client);
	esp_mqtt_client_register_event(client, MQTT_EVENT_DISCONNECTED, mqtt_event_handler, client);
	esp_mqtt_client_register_event(client, MQTT_EVENT_SUBSCRIBED, mqtt_event_handler, client);
	esp_mqtt_client_register_event(client, MQTT_EVENT_UNSUBSCRIBED, mqtt_event_handler, client);
	esp_mqtt_client_register_event(client, MQTT_EVENT_PUBLISHED, mqtt_event_handler, client);
	esp_mqtt_client_register_event(client, MQTT_EVENT_DATA, mqtt_event_handler, client);
	esp_mqtt_client_register_event(client, MQTT_EVENT_BEFORE_CONNECT, mqtt_event_handler, client);

	esp_mqtt_client_start(client);
	xEventGroupWaitBits(wifi_event_group, MQTT_CONNECTED_BIT, false, true, portMAX_DELAY);
	ESP_LOGI(__FUNCTION__, "Connected to MQTT Broker.");
And the event handler :

Code: Select all

static void mqtt_event_handler(void* handler_args, esp_event_base_t base, int32_t event_id, void* event_data)
{
	esp_mqtt_event_handle_t event = (esp_mqtt_event_handle_t)event_data;
	esp_mqtt_client_handle_t client = event->client;

	switch(event->event_id)
	{
		case MQTT_EVENT_CONNECTED:
		{
			ESP_LOGI(__FUNCTION__, "MQTT_EVENT_CONNECTED");
			xEventGroupSetBits(wifi_event_group, MQTT_CONNECTED_BIT);
			esp_mqtt_client_subscribe(client, "#", 0);
			break;
		}

		case MQTT_EVENT_DISCONNECTED:
		{
			ESP_LOGI(__FUNCTION__, "MQTT_EVENT_DISCONNECTED");
			xEventGroupClearBits(wifi_event_group, MQTT_CONNECTED_BIT);
			break;
		}

		case MQTT_EVENT_SUBSCRIBED:
		{
			ESP_LOGI(__FUNCTION__, "MQTT_EVENT_SUBSCRIBED");
			break;
		}

		case MQTT_EVENT_UNSUBSCRIBED:
		{
			ESP_LOGI(__FUNCTION__, "MQTT_EVENT_UNSUBSCRIBED");
			break;
		}

		case MQTT_EVENT_PUBLISHED:
		{
			ESP_LOGI(__FUNCTION__, "MQTT_EVENT_PUBLISHED");
			break;
		}

		case MQTT_EVENT_DATA:
		{
			ESP_LOGI(__FUNCTION__, "MQTT_EVENT_DATA");
			ESP_LOGI(__FUNCTION__, "TOPIC = %.*s", event->topic_len, event->topic);
			ESP_LOGI(__FUNCTION__, "DATA = %.*s", event->data_len, event->data);
			break;
		}

		case MQTT_EVENT_ERROR:
		{
			ESP_LOGI(__FUNCTION__, "MQTT_EVENT_ERROR");
			break;
		}

		default:
		{
			ESP_LOGI(__FUNCTION__, "UNHANDLED MQTT EVENT : %d", event->event_id);
			break;
		}
	}
}
This is working as expected, I receive the mqtt messages in the MQTT_EVENT_DATA case.

Now I have to implement the function(s) to handle te received messages ...

Greats, Paul.

Who is online

Users browsing this forum: No registered users and 98 guests