Please help how to reconnect to MQTT broker after resume

amichalu
Posts: 1
Joined: Mon Oct 05, 2020 7:58 pm

Please help how to reconnect to MQTT broker after resume

Postby amichalu » Mon Oct 05, 2020 8:03 pm

Hi, I'm facing problem with ESP32 DevKitv1, I cannot reconnect to broker, scenario is following:

1) MQTT client connected to broker (mosquitto);

2) Messages are being sent to broker normally;

3) after while mosquitto is down,

4) MQTT client reports disconnection;

5) mosquitto is up again and working normally, network connection valid (ping answers);

4) MQTT client cannot reconnect any more, log: "MQTT_CLIENT: Client force reconnect requested"

I use code modified, based on MQTT example https://github.com/espressif/esp-idf/tr ... s/mqtt/tcp:

Code: Select all

case MQTT_EVENT_DISCONNECTED:
            ESP_LOGI(TAG, "MQTT_EVENT_DISCONNECTED");
            ESP_LOGI(TAG, "Trying to reconnect...");

            xFrequency = 2*1000 / portTICK_PERIOD_MS;
            xLastWakeTime = xTaskGetTickCount();
            for( ;; )
            {
                ESP_LOGI(TAG, "another attempt ...");
                vTaskDelayUntil( &xLastWakeTime, xFrequency );                
                esp_err_t err = esp_mqtt_client_reconnect(client);   // <====== it seems not working !!!
                if (err != ESP_OK) ESP_LOGI(TAG, "reconnect error !");
            }
            break;

toljatyr
Posts: 12
Joined: Mon Mar 07, 2022 10:39 am

Re: Please help how to reconnect to MQTT broker after resume

Postby toljatyr » Thu Apr 28, 2022 2:23 pm

I faced a sumilar problem.
I disabled auto reconnection in the settings of my MqttClient.
After it successfully connected to the broker I called esp_mqtt_client_disconnect()
MQTT_EVENT_DISCONNECTED event occurred
Then I called esp_mqtt_client_reconnect() however, no connection followed but the messages in log:
MQTT_CLIENT: Client force reconnect requested
MQTT_CLIENT: The client is not waiting for reconnection. Ignore the request
so my client stayed disconnected. I don't know how to force it to reconnect.

Who is online

Users browsing this forum: artisdom, Bing [Bot], Google [Bot] and 92 guests