ESP IDF MQTT connection lost after few hours

nopnop2002
Posts: 112
Joined: Thu Oct 03, 2019 10:52 pm
Contact:

Re: ESP IDF MQTT connection lost after few hours

Postby nopnop2002 » Mon Oct 21, 2024 10:05 am

I have my own server running in a docker, on local hardware.

I don't know the OS of your MQTT server, but I believe you can add Wi-Fi router functionality to this server.

I used Raspbian to build an MQTT server and Hotspot at the same time a long time ago.

Cimby1
Posts: 22
Joined: Thu Aug 22, 2024 12:56 pm

Re: ESP IDF MQTT connection lost after few hours

Postby Cimby1 » Mon Oct 21, 2024 10:56 am

I don't want to do anything like this. I plan to use this in an environment with a wifi router and an MQTT server hosted locally or over the internet. Nothing more.

Cimby1
Posts: 22
Joined: Thu Aug 22, 2024 12:56 pm

Re: ESP IDF MQTT connection lost after few hours

Postby Cimby1 » Fri Oct 25, 2024 5:22 pm

A quick update on the mqtt problem. It happend again with no visable Wifi error. And no error in the mosquitto log.
I always try to foolproof my code, so I tried to manually stop my MQTT and see what's happend.

Manually:

Code: Select all

I (206506) mqtt: MQTT_EVENT_BEFORE_CONNECT
E (206506) esp-tls: [sock=54] delayed connect error: Connection reset by peer
E (206516) transport_base: Failed to open a new connection: 32772
E (206516) mqtt_client: Error transport connect
I (206516) mqtt: MQTT_EVENT_ERROR
I (206526) mqtt: MQTT_EVENT_DISCONNECTED
And for comparison the error that is haunting me in my dreams:

Code: Select all

I (380022) mqtt: MQTT_EVENT_BEFORE_CONNECT
E (380022) esp-tls: [sock=54] connect() error: Host is unreachable
E (380022) transport_base: Failed to open a new connection: 32772
E (380022) mqtt_client: Error transport connect
I (380032) mqtt: MQTT_EVENT_ERROR
I (380032) mqtt: MQTT_EVENT_DISCONNECTED
I added a new function to my wifi. Try to reconnect as many time as it can, if a connection was ever established at the first bootup.

Code: Select all

 /* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
     * happened. */
    if (bits & WIFI_CONNECTED_BIT)
    {
        ESP_LOGI(TAG, "connected to ap SSID:%s password:%s",
                 WIFI_SSID, WIFI_PASS);

        if (established_connection_once == false)
        {
            ESP_LOGI(TAG, "First connection established!");
            established_connection_once = true;
        }
    }
    else if (bits & WIFI_FAIL_BIT)
    {
        ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s in %d tries",
                 WIFI_SSID, WIFI_PASS, MAXIMUM_RETRY);

        if (established_connection_once == true)
        {
            ESP_LOGI(TAG, "Esp lost it's wifi connection, trying again !");
            s_retry_num = 0;
        }
    }
    else
    {
        ESP_LOGE(TAG, "UNEXPECTED EVENT");
    }
Any ideas what is still causing the error ?
Host is unreachable

agiry_avi
Posts: 2
Joined: Mon Oct 28, 2024 10:45 am

Re: ESP IDF MQTT connection lost after few hours

Postby agiry_avi » Mon Oct 28, 2024 11:14 am

Hello,
I have kinda the same error on my ESP32c3.
On startup, I m able to connect to the wifi network registered in the memory.
Then I switch to the mode WIFI_MODE_APSTA, to host a web page and configure another wifi network.
Then, when I try to reconnect, i have :

Code: Select all

I (107143) mqtt_event_handler: MQTT_EVENT_BEFORE_CONNECT
E (107143) esp-tls: [sock=48] connect() error: Host is unreachable
E (107143) esp-tls: Failed to open new connection
E (107153) transport_base: Failed to open a new connection
E (107153) mqtt_client: Error transport connect
However, my device is connected to the wifi, I can see it in my administrator panel.

With wireshark in a working state, I can see the DNS request and response and then TCP and TLS exchanges.
In a not working state, I also have DNS request but I never have the TCP and TLS exchanges.

I have tried to open a socket to the MQTT network when I have MQTT_EVENT_BEFORE_CONNECT, and I got errno 118.
Looking on the internet, it means that I m not connected to my local network.

Cimby1
Posts: 22
Joined: Thu Aug 22, 2024 12:56 pm

Re: ESP IDF MQTT connection lost after few hours

Postby Cimby1 » Tue Oct 29, 2024 12:00 pm

I would say, welcome to the unsolved mystery club.
I had a few tries with a similiar approch like you did. (I made a fall back mechanism when the wi-fi is unavailable)
I solved my issue with starting AP and STA mode separaretly. Before each start/init of a mode I deleted the mode that I no longer use.

But I still don’t know what is causing the MQTT error…

agiry_avi
Posts: 2
Joined: Mon Oct 28, 2024 10:45 am

Re: ESP IDF MQTT connection lost after few hours

Postby agiry_avi » Wed Oct 30, 2024 9:37 am

I've found a partial solution, when I get the error, I restart init of the wifi connection.
It solved the problem but I think it's a bit dirty

Cimby1
Posts: 22
Joined: Thu Aug 22, 2024 12:56 pm

Re: ESP IDF MQTT connection lost after few hours

Postby Cimby1 » Thu Oct 31, 2024 3:35 pm

With my problem I did the same. In the MQTT handler I restart the wi-fi connection.
It is not the perfect solution, but I don’t have a better idea.

Who is online

Users browsing this forum: Baidu [Spider], brushlow18 and 116 guests