panic when shutting down mqtt client after publishing

noweare
Posts: 67
Joined: Tue Jul 02, 2019 11:35 am

panic when shutting down mqtt client after publishing

Postby noweare » Sun Sep 04, 2022 2:51 am

Using idf version 4.4.2 esp32

I am sending data via mqtt.

I try to down wifi, mqtt and netif after publishing because it is a long time between publishing, but i get an warning then
an assert fail.

How do i shut down wifi,mqtt and netif so that I can use the same start up process each time.

First time through I can start up and publish data ok but shutting down mqtt I get error msg:

I (18680) MQTT: MQTT_PUBLIISHED
I (18690) MQTT_CLIENT: Client asked to disconnect
W (18690) MQTT_CLIENT: Client asked to stop, but was not started
assert failed: tlsf_free heap_tlsf.c:872 (!block_is_free(block) && "block already marked as free")

Then I get a panic abort

The commands i use are for shutting down mqtt client are:

Code: Select all

 esp_mqtt_client_stop(client);
 esp_mqtt_client_disconnect(client);
 esp_mqtt_client_destroy(client);

It looks like it is a heap memory problem like a some memory was marked as free but it was not.

Any pointers in some direction is appreciated.

cruvus
Posts: 59
Joined: Fri Jul 08, 2022 5:08 pm
Location: Planet Earth

Re: panic when shutting down mqtt client after publishing

Postby cruvus » Sun Sep 04, 2022 7:41 am

You should do something like

Code: Select all

if (!Client) return;
esp_mqtt_client_stop(Client);
esp_mqtt_client_destroy(Client);
Client = NULL;
I had similar problems when calling these functions twice. The second time crashed the app.
ESP32 / ESP-IDF 5.1.4

noweare
Posts: 67
Joined: Tue Jul 02, 2019 11:35 am

Re: panic when shutting down mqtt client after publishing

Postby noweare » Sun Sep 04, 2022 3:21 pm

I will try that but I am thinking i need to shut down everything loops, clients, events etc....

chegewara
Posts: 2333
Joined: Wed Jun 14, 2017 9:00 pm

Re: panic when shutting down mqtt client after publishing

Postby chegewara » Sun Sep 04, 2022 6:18 pm

This function is enough, the other 2 are optional, just read the notes:

Code: Select all

/**
 * @brief Destroys the client handle
 *
 * Notes:
 *  - Cannot be called from the mqtt event handler
 *
 * @param client    mqtt client handle
 *
 * @return ESP_OK
 *         ESP_ERR_INVALID_ARG on wrong initialization
 */
esp_err_t esp_mqtt_client_destroy(esp_mqtt_client_handle_t client);

noweare
Posts: 67
Joined: Tue Jul 02, 2019 11:35 am

Re: panic when shutting down mqtt client after publishing

Postby noweare » Mon Sep 05, 2022 7:53 pm

Thanks for that. That command worked shutting down mqtt between the sending of the data points.

I have more work to do as far as shutting down wifi completely to save battery power between
data sending.

I get an error when running wifi_init routine after the first time through the code (after first data point sent).
E (30548) esp_netif_lwip: esp_netif_new: Failed to configure netif with config=0x3ffbee80 (config or if_key is NULL or duplicate key)

This is probably due to not shutting down wifi or netif completely as the error msg tells me about a duplicate. The netif
esp_netif_deinit(void) has a note that "Deinitialization is not supported yet" so I can't use that. IDF ver 4.4.2

Right now i use esp_wifi_stop() and esp_wifi_start() and only the first time run the whole wifi int routine
ie. nvs_flash_init(), esp_netif_int(), esp_event_loop_create_default() .... .
I am probably using more power than needed though between data points.

Good learning experience, though.

chegewara
Posts: 2333
Joined: Wed Jun 14, 2017 9:00 pm

Re: panic when shutting down mqtt client after publishing

Postby chegewara » Wed Sep 07, 2022 1:22 pm

Yeah, you dont need whole init procedure, just init mqtt client and sub all topics.

Who is online

Users browsing this forum: Bing [Bot] and 78 guests