I'm using ESP32 as wifi AP with tcp server on it. Android phone runs app that have next logic:
1) connects to esp32 wifi AP
2) connects to TCP socket
3) exchanging TCP data (about 900 bytes (6 packets of 150 bytes) in total)
4) closing socket connection.
5) back to step 2.
Sometimes I got next error:
I have stydied behaviour of connection and have find out, that this happens after socket closed connection.assertion "netconn_alloc: undefined netconn_type" failed: file "D:/Projects/ESP32/TOOLS/esp-idf/components/lwip/api/api_msg.c", line 696, function: netconn_alloc
abort() was called at PC 0x400d5383 on core 1
Some extended part of log:
Code: Select all
[0;33mW (127916) TCP: connect socket error 128 Socket is not connected[0m
[0;33mW (127916) TCP: tcp disconnected... stop.
[0m
Code:128
[0;33mW (127916) TCP: (Socket is not connected)
[0m
socket closed
assertion "netconn_alloc: undefined netconn_type" failed: file "D:/Projects/ESP32/TOOLS/esp-idf/components/lwip/api/api_msg.c", line 696, function: netconn_alloc
abort() was called at PC 0x400d5383 on core 1
Backtrace: 0x4008ff38:0x3ffe3a60 0x4009010f:0x3ffe3a80 0x400d5383:0x3ffe3aa0 0x4017b59f:0x3ffe3ad0 0x4017b6ef:0x3ffe3af0 0x4018147d:0x3ffe3b10 0x40181e65:0x3ffe3b30 0x401848d6:0x3ffe3b50 0x4018aa6a:0x3ffe3b70 0x4017c0fd:0x3ffe3b90
Code: Select all
#define BUF_SIZE (512)
#define EXAMPLE_DEFAULT_PKTSIZE 1460
#define TCP_SERVER_IP "192.168.4.1"
#define TCP_SERVER_PORT 333
typedef struct MyData_s
{
uint8_t TCP_Buf[BUF_SIZE]; // 512
uint8_t UART_Buf[BUF_SIZE]; //512
uint32_t UART_RxCounter;
uint32_t TCP_len;
uint32_t UART_len;
uint8_t TCP_got_packet;
uint8_t UART_got_packet;
bool TCP_conn; // TRUE: tcp connected
}MyData_t;
void wifi_init_softap()
{
wifi_event_group = xEventGroupCreate();
tcpip_adapter_init();
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
wifi_config_t wifi_config = {
.ap = {
.ssid = WIFI_SSID,
.ssid_len = strlen(WIFI_SSID),
.max_connection = 1,
.authmode = WIFI_AUTH_OPEN
},
};
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());
ESP_LOGI(WIFI_TAG, "wifi_init_softap finished.SSID:%s password:%s",
WIFI_SSID, WIFI_PASS);
}
esp_err_t create_tcp_server()
{
ESP_LOGI(TCP_TAG, "server socket....port=%d\n", TCP_SERVER_PORT);
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket < 0) {
show_socket_error_reason(server_socket);
return ESP_FAIL;
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(TCP_SERVER_PORT);
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
show_socket_error_reason(server_socket);
close(server_socket);
return ESP_FAIL;
}
if (listen(server_socket, 5) < 0) {
show_socket_error_reason(server_socket);
close(server_socket);
return ESP_FAIL;
}
connect_socket = accept(server_socket, (struct sockaddr*)&client_addr, &socklen);
if (connect_socket<0) {
show_socket_error_reason(connect_socket);
close(server_socket);
return ESP_FAIL;
}
/*connection established now can send/recv*/
MyData.TCP_conn = true;
ESP_LOGI(TCP_TAG, "tcp connection established!");
ESP_LOGI(TCP_TAG, "socket:%d", server_socket);
return ESP_OK;
}
bool tcp_server_task_restart; // TRUE: restart task
static void tcp_server_task(void *pvParameters)
{
ESP_LOGI(TCP_TAG, "task tcp_conn.");
tcp_server_task_restart = false;
/*create tcp socket*/
int socket_ret;
ESP_LOGI(TCP_TAG, "tcp_server will start after 100ms!");
vTaskDelay(100 / portTICK_RATE_MS);
ESP_LOGI(TCP_TAG, "creating tcp server...");
socket_ret=create_tcp_server();
if(socket_ret == ESP_FAIL) {
ESP_LOGI(TCP_TAG, "create tcp socket error,stop.");
MyData.TCP_conn = false;
tcp_server_task_restart = true;
vTaskDelete(NULL);
}
while (!tcp_server_task_restart)
{
uint8_t databuff[EXAMPLE_DEFAULT_PKTSIZE]; // 1460
int len = recv(connect_socket, databuff, EXAMPLE_DEFAULT_PKTSIZE, 0);
MyData.TCP_len = len;
if (len > 0)
{
memcpy(MyData.TCP_Buf, databuff, MyData.TCP_len);
printf("FL_len:%d, FL_data:%s\n", MyData.TCP_len, MyData.TCP_Buf); // debug
MyData.TCP_got_packet = true;
}
vTaskDelay(10 / portTICK_RATE_MS);//every 10ms
if (len <= 0)
{
int err_ret = check_working_socket();
if (err_ret == ECONNRESET || ECONNABORTED)
{
ESP_LOGW(TCP_TAG, "tcp disconnected... stop.\n");
printf("Code:%d\n", err_ret);
if(err_ret == ENOTCONN)
{
ESP_LOGW(TCP_TAG, "(Socket is not connected)\n");
}
break;
}
}
}
close_socket();
printf("socket closed\n");
MyData.TCP_conn = false;
tcp_server_task_restart = true;
vTaskDelete(NULL);
}
static void watch_tcp_srv_task(void *pvParameters)
{
while(1)
{
if(tcp_server_task_restart == true)
{
printf("TCP task is RESTARTING\n\n");
xTaskCreate(&tcp_server2_task,"tcp_server_task", 4096, NULL, 5, NULL);
printf("tcp_server2_task created\n");
}
vTaskDelay(1000 / portTICK_RATE_MS);//every 1s
}
}
void app_main()
{
//WIFI
ESP_LOGI(WIFI_TAG, "ESP_WIFI_MODE_AP");
wifi_init_softap();
//TCP
xTaskCreate(&tcp_server_task, "tcp_server_task", 4096, NULL, 5, NULL);
xTaskCreate(&watch_tcp_srv_task, "watch_tcp_srv_task", 1024, NULL, 5, NULL);
}
Here is part of log to see route:
Code: Select all
TCP task is RESTARTING
tcp_server_task created
[0;32mI (118796) TCP: task tcp_conn.[0m
[0;32mI (118796) TCP: tcp_server will start after 100ms![0m
[0;32mI (118896) TCP: creating tcp server...[0m
[0;32mI (118896) TCP: server socket....port=333
[0m
[0;32mI (119476) TCP: tcp connection established![0m
[0;32mI (119476) TCP: socket:4104[0m
[0;32mI (120106) GOT FULL PACKET:, size: 150[0m
FL_UART(len:150):$i}t
FL_len:11, FL_data:$VЊt
[0;32mI (120576) GOT FULL PACKET:, size: 150[0m
FL_UART(len:150):$VЊt„Іа
FL_len:11, FL_data:$iЃt
[0;32mI (121046) GOT FULL PACKET:, size: 150[0m
FL_UART(len:150):$iЃt
FL_len:11, FL_data:$`Њ©t
[0;32mI (121516) GOT FULL PACKET:, size: 150[0m
FL_UART(len:150):$`Њ©t
FL_len:11, FL_data:$`Њ®t
[0;32mI (121986) GOT FULL PACKET:, size: 150[0m
FL_UART(len:150):$`Њ®t
FL_len:11, FL_data:$VЊ·t
[0;32mI (122456) GOT FULL PACKET:, size: 150[0m
FL_UART(len:150):$VЊ·t
FL_len:11, FL_data:$
[0;33mW (123086) TCP: connect socket error 128 Socket is not connected[0m
[0;33mW (123086) TCP: tcp disconnected... stop.
[0m
Code:128
[0;33mW (123086) TCP: (Socket is not connected)
[0m
socket closed
TCP task is RESTARTING
Second task is monitoring state of tcp server - if socket has been closed, tcp_server_task deletes and "watchdog" task re-creating it.
Not sure if this is good way, but it worked until android app (new version) started to re-opening sockets each 900 bytes.