I'm using TCP sockets over WiFi to connect one ESP32 with a Python client on my computer. I'm also using a BLE server at the same time and UART to communicate with my main board. I want to download files received on UART to my computer. The thing is, I'm having a quite low speed (between 200 and 300 kbps). What's more, if I activate the BLE, I receive corrupted files because some characters are lost during transmission between my main board and the ESP, on one packet out of 5 roughly.
My question is, is it normal that the Wifi is so slow? I'm having quite bad speed with http_server example but the iperf example gives me correct results. I'm using ESP-IDF v3.1.3. And why am I losing data on the UART only when I activate the BLE?
Socket code on ESP32:
- int write_wifi(const char* response, int len) {
- if(len > BUF_SIZE) {
- ESP_LOGE(SOCK_TAG, "Too long response");
- return 1;
- }
- strncpy(write_buffer, response, len);
- write_len = len;
- xTaskNotifyGive(tcpTaskHandle);
- return 0;
- }
- static void tcp_server_task(void *pvParameters) {
- char rx_buffer[128];
- char addr_str[128];
- int addr_family;
- int ip_protocol;
- while(1) {
- #ifdef CONFIG_EXAMPLE_IPV4
- struct sockaddr_in destAddr;
- destAddr.sin_addr.s_addr = htonl(INADDR_ANY);
- destAddr.sin_family = AF_INET;
- destAddr.sin_port = htons(PORT);
- addr_family = AF_INET;
- ip_protocol = IPPROTO_IP;
- inet_ntoa_r(destAddr.sin_addr, addr_str, sizeof(addr_str) - 1);
- #else // IPV6
- struct sockaddr_in6 destAddr;
- bzero(&destAddr.sin6_addr.un, sizeof(destAddr.sin6_addr.un));
- destAddr.sin6_family = AF_INET6;
- destAddr.sin6_port = htons(PORT);
- addr_family = AF_INET6;
- ip_protocol = IPPROTO_IPV6;
- inet6_ntoa_r(destAddr.sin6_addr, addr_str, sizeof(addr_str) - 1);
- #endif
- int listen_sock = socket(addr_family, SOCK_STREAM, ip_protocol);
- ESP_LOGI(SOCK_TAG, "Socket created");
- int on = 1;
- int err = setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on));
- err = bind(listen_sock, (struct sockaddr *)&destAddr, sizeof(destAddr));
- ESP_LOGI(SOCK_TAG, "Socket binded");
- err = listen(listen_sock, 1);
- ESP_LOGI(SOCK_TAG, "Socket listening");
- while (1) {
- struct sockaddr_in6 sourceAddr; // Large enough for both IPv4 or IPv6
- uint addrLen = sizeof(sourceAddr);
- int sock = accept(listen_sock, (struct sockaddr *)&sourceAddr, &addrLen);
- if (sock < 0) {
- ESP_LOGE(SOCK_TAG, "Unable to accept connection: errno %d", errno);
- break;
- }
- ESP_LOGI(SOCK_TAG, "Socket accepted");
- uart_write_bytes(UART_NUM_1, "@wifi_connected\n", 16);
- while (1) {
- int len = recv(sock, rx_buffer, sizeof(rx_buffer) - 1, 0);
- // Test the request
- // Get the sender's ip address as string
- if (sourceAddr.sin6_family == PF_INET) {
- inet_ntoa_r(((struct sockaddr_in *)&sourceAddr)->sin_addr.s_addr, addr_str, sizeof(addr_str) - 1);
- } else if (sourceAddr.sin6_family == PF_INET6) {
- inet6_ntoa_r(sourceAddr.sin6_addr, addr_str, sizeof(addr_str) - 1);
- rx_buffer[len] = 0; // Null-terminate whatever we received and treat like a string
- }
- ESP_LOGI(SOCK_TAG, "Received %d bytes from %s on socket", len, addr_str);
- wifi_response = 1;
- uart_write_bytes(UART_NUM_1, rx_buffer, len);
- ulTaskNotifyTake(pdTRUE, portMAX_DELAY); // Will be woken up when write_wifi() is called
- send(sock, write_buffer, write_len, 0);
- }
- }
- close(sock);
- uart_write_bytes(UART_NUM_1, "@wifi_disconnected\n", 19);
- }
- }
- vTaskDelete(NULL);
- }
- int current_len;
- int uartstate;
- char read_data[BUF_SIZE];
- static void read_UART()
- {
- uartstate = UART_WAITING;
- // Read data from the UART
- int len = uart_read_bytes(UART_NUM_1, read_data + current_len, BUF_SIZE-1-current_len, 20 / portTICK_RATE_MS);
- current_len += len;
- read_data[current_len] = '\0';
- if(len > 0) {
- ESP_LOGI(TAG, "Received %d bytes on UART. current_len = %d", len, current_len);
- }
- //If '\n' is detected
- if (strchr((char*)read_data, '\n') || current_len == BUF_SIZE-1) {
- if (read_data[0] == '@') {
- uartstate = COMMAND_RECEIVED;
- // Process command
- ESP_LOGD(TAG, "Command received");
- } else if(wifi_response) { // wifi_response is set to 1 when we get a request from Wifi, so that the answer can be sent to Wifi
- write_wifi((char*)read_data, current_len);
- wifi_response = 0;
- } else {
- //the message is for the BLE
- uartstate = WRITE_CHARACTERISTIC;
- // write to BLE
- }
- current_len = 0;
- }
- }
Code: Select all
#
# Wi-Fi
#
CONFIG_SW_COEXIST_ENABLE=y
CONFIG_SW_COEXIST_PREFERENCE_WIFI=
CONFIG_SW_COEXIST_PREFERENCE_BT=
CONFIG_SW_COEXIST_PREFERENCE_BALANCE=y
CONFIG_SW_COEXIST_PREFERENCE_VALUE=2
CONFIG_ESP32_WIFI_STATIC_RX_BUFFER_NUM=16
CONFIG_ESP32_WIFI_DYNAMIC_RX_BUFFER_NUM=64
CONFIG_ESP32_WIFI_STATIC_TX_BUFFER=
CONFIG_ESP32_WIFI_DYNAMIC_TX_BUFFER=y
CONFIG_ESP32_WIFI_TX_BUFFER_TYPE=1
CONFIG_ESP32_WIFI_DYNAMIC_TX_BUFFER_NUM=64
CONFIG_ESP32_WIFI_CSI_ENABLED=
CONFIG_ESP32_WIFI_AMPDU_TX_ENABLED=y
CONFIG_ESP32_WIFI_TX_BA_WIN=32
CONFIG_ESP32_WIFI_AMPDU_RX_ENABLED=y
CONFIG_ESP32_WIFI_RX_BA_WIN=32
CONFIG_ESP32_WIFI_NVS_ENABLED=y
CONFIG_ESP32_WIFI_TASK_PINNED_TO_CORE_0=y
CONFIG_ESP32_WIFI_TASK_PINNED_TO_CORE_1=
CONFIG_ESP32_WIFI_SOFTAP_BEACON_MAX_LEN=752
#
# Bluetooth
#
CONFIG_BT_ENABLED=y
CONFIG_BTDM_CONTROLLER_PINNED_TO_CORE_0=y
CONFIG_BTDM_CONTROLLER_PINNED_TO_CORE_1=
CONFIG_BTDM_CONTROLLER_PINNED_TO_CORE=0
CONFIG_BTDM_CONTROLLER_HCI_MODE_VHCI=y
CONFIG_BTDM_CONTROLLER_HCI_MODE_UART_H4=
#
# MODEM SLEEP Options
#
CONFIG_BTDM_CONTROLLER_MODEM_SLEEP=y
CONFIG_BTDM_MODEM_SLEEP_MODE_ORIG=y
CONFIG_BTDM_MODEM_SLEEP_MODE_EVED=
CONFIG_BTDM_LPCLK_SEL_MAIN_XTAL=y
CONFIG_BLUEDROID_ENABLED=y
CONFIG_BLUEDROID_PINNED_TO_CORE_0=y
CONFIG_BLUEDROID_PINNED_TO_CORE_1=
CONFIG_BLUEDROID_PINNED_TO_CORE=0
CONFIG_BTC_TASK_STACK_SIZE=3072
CONFIG_BLUEDROID_MEM_DEBUG=
CONFIG_CLASSIC_BT_ENABLED=y
CONFIG_A2DP_ENABLE=y
CONFIG_A2DP_SINK_TASK_STACK_SIZE=2048
CONFIG_A2DP_SOURCE_TASK_STACK_SIZE=2048
CONFIG_BT_SPP_ENABLED=
CONFIG_HFP_ENABLE=
CONFIG_GATTS_ENABLE=y
CONFIG_GATTC_ENABLE=y
CONFIG_GATTC_CACHE_NVS_FLASH=
CONFIG_BLE_SMP_ENABLE=y
CONFIG_BT_STACK_NO_LOG=