Webserver lost connection of ESP32 module
Posted: 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
2. Socket Event handler
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
}
}
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;
}
}