Webserver lost connection of ESP32 module

huybui91
Posts: 10
Joined: Thu Dec 14, 2023 6:15 am

Webserver lost connection of ESP32 module

Postby huybui91 » Thu Jan 11, 2024 8:34 am

Hello all my friends,
I'm doing small project using ESP32 connect to webserver with using esp_websocket_client.h.
Everything so far is ok but I have problem that after established connection to server and recieved information from sever, About 10s or 20s later, I check list device of server connection and saw that Module ID of ESP32 was dissapeared although event handler still recieve opcode =10. I try reset by RST button and check again, server still connect to device and always connection but something the issue happen --> The problem occurs randomly. I'm using framwork 5.2

I don't know why if Server is disconnect to client then Socket event have to recieve signal "Disconnected" but In my picture attachement below, The event still recieve opcode = 10 mean that Ping- Pong process ( hearbeat of Socket) still working.

See my code and picture below to more understand:
1. Start socket connect to server
In here, I also config

Code: Select all

void Websocket_Start(void *pvParameters)
{
    TAG = "WEBSOCKET_START";
    esp_err_t ret;
    char websocket_url[100];
    snprintf(websocket_url, sizeof(websocket_url), "%s%s", BASE_WEBSOCKET_URL, CHIP_ID);
    ESP_LOGI(TAG, "Connecting to %s", websocket_url);
    esp_websocket_client_config_t websocket_cfg = {
        .uri = websocket_url,
        .disable_auto_reconnect = false,
        .keep_alive_enable = true,
        .keep_alive_interval = 10, 
        .ping_interval_sec = 5,
        .reconnect_timeout_ms = 2000,
        .network_timeout_ms = 30000};
    client_Socket = esp_websocket_client_init(&websocket_cfg);
    ret = esp_websocket_register_events(client_Socket, WEBSOCKET_EVENT_ANY, websocket_event_handler, (void *)client_Socket);
    ret = esp_websocket_client_start(client_Socket);
    while (1)
    {
        vTaskDelay(5000 / portTICK_PERIOD_MS); // Adjust as needed
    }
}
2. Socket Event handler

Code: Select all

void websocket_event_handler(void *handler_args, esp_event_base_t base, int32_t event_id, void *event_data)
{
    TAG = "WEBSOCKET_EVENT";
    esp_websocket_event_data_t *data = (esp_websocket_event_data_t *)event_data;
    switch (event_id)
    {
    case WEBSOCKET_EVENT_CONNECTED:
        ESP_LOGI(TAG, "WEBSOCKET_EVENT_CONNECTED");
        break;
    case WEBSOCKET_EVENT_DISCONNECTED:
        ESP_LOGI(TAG, "WEBSOCKET_EVENT_DISCONNECTED");
        break;
    case WEBSOCKET_EVENT_DATA:
        ESP_LOGI(TAG, "WEBSOCKET_EVENT_DATA");
        ESP_LOGI(TAG, "Received opcode=%d", data->op_code);
        ESP_LOGW(TAG, "WEBSOCKET Received=%.*s", data->data_len, (char *)data->data_ptr);
        ESP_LOGW(TAG, "Total payload length=%d, data_len=%d, current payload offset=%d\r\n", data->payload_len, data->data_len, data->payload_offset);
        // Parse json funct ion
        parse_json((char *)data->data_ptr);
        break;
    case WEBSOCKET_EVENT_ERROR:
        ESP_LOGI(TAG, "WEBSOCKET_EVENT_ERROR");
        break;
    }
}
Attachments
Heartbeat.jpg
Heartbeat.jpg (90.95 KiB) Viewed 48807 times

liaifat85
Posts: 200
Joined: Wed Dec 06, 2023 2:46 pm

Re: Webserver lost connection of ESP32 module

Postby liaifat85 » Thu Jan 11, 2024 12:22 pm

Adjust the timeout values in your WebSocket configuration cautiously. For example, try increasing the network_timeout_ms value to see if it affects the stability.

huybui91
Posts: 10
Joined: Thu Dec 14, 2023 6:15 am

Re: Webserver lost connection of ESP32 module

Postby huybui91 » Fri Jan 12, 2024 9:54 am

liaifat85 wrote:
Thu Jan 11, 2024 12:22 pm
Adjust the timeout values in your WebSocket configuration cautiously. For example, try increasing the network_timeout_ms value to see if it affects the stability.
Hello
I also adjust all value about time but can't solve problem. After connection to server, around 5s later, Server lost connect to device. Another way I think, Is it problem come from server?

huybui91
Posts: 10
Joined: Thu Dec 14, 2023 6:15 am

Re: Webserver lost connection of ESP32 module

Postby huybui91 » Mon Jan 15, 2024 5:57 am

Hi
I just update some finding.
After download code, if I press button "RST" on board or run command

Code: Select all

esp_restart()
, board will auto restart again and server will lost connection to device.
If I just dis-connect and connect USB, board connect to server as normal

huybui91
Posts: 10
Joined: Thu Dec 14, 2023 6:15 am

Re: Webserver lost connection of ESP32 module

Postby huybui91 » Tue Jan 16, 2024 1:55 am

anyone can help me

Who is online

Users browsing this forum: ESP_Sprite, piter5 and 107 guests