Low speed on Wifi and character loss

M3te0ra2
Posts: 3
Joined: Fri Apr 05, 2019 11:00 am

Low speed on Wifi and character loss

Postby M3te0ra2 » Wed Apr 10, 2019 9:46 am

Hi,

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:
  1. int write_wifi(const char* response, int len) {
  2.     if(len > BUF_SIZE) {
  3.         ESP_LOGE(SOCK_TAG, "Too long response");
  4.         return 1;
  5.     }
  6.  
  7.     strncpy(write_buffer, response, len);
  8.  
  9.     write_len = len;
  10.  
  11.     xTaskNotifyGive(tcpTaskHandle);
  12.  
  13.     return 0;
  14. }
  15.  
  16.  
  17. static void tcp_server_task(void *pvParameters) {
  18.     char rx_buffer[128];
  19.     char addr_str[128];
  20.     int addr_family;
  21.     int ip_protocol;
  22.  
  23.     while(1) {
  24.  
  25. #ifdef CONFIG_EXAMPLE_IPV4
  26.         struct sockaddr_in destAddr;
  27.         destAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  28.         destAddr.sin_family = AF_INET;
  29.         destAddr.sin_port = htons(PORT);
  30.         addr_family = AF_INET;
  31.         ip_protocol = IPPROTO_IP;
  32.         inet_ntoa_r(destAddr.sin_addr, addr_str, sizeof(addr_str) - 1);
  33. #else // IPV6
  34.         struct sockaddr_in6 destAddr;
  35.         bzero(&destAddr.sin6_addr.un, sizeof(destAddr.sin6_addr.un));
  36.         destAddr.sin6_family = AF_INET6;
  37.         destAddr.sin6_port = htons(PORT);
  38.         addr_family = AF_INET6;
  39.         ip_protocol = IPPROTO_IPV6;
  40.         inet6_ntoa_r(destAddr.sin6_addr, addr_str, sizeof(addr_str) - 1);
  41. #endif
  42.  
  43.         int listen_sock = socket(addr_family, SOCK_STREAM, ip_protocol);
  44.         ESP_LOGI(SOCK_TAG, "Socket created");
  45.  
  46.         int on = 1;
  47.         int err = setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on));
  48.  
  49.         err = bind(listen_sock, (struct sockaddr *)&destAddr, sizeof(destAddr));
  50.         ESP_LOGI(SOCK_TAG, "Socket binded");
  51.  
  52.         err = listen(listen_sock, 1);
  53.         ESP_LOGI(SOCK_TAG, "Socket listening");
  54.  
  55.         while (1) {
  56.  
  57.             struct sockaddr_in6 sourceAddr; // Large enough for both IPv4 or IPv6
  58.             uint addrLen = sizeof(sourceAddr);
  59.             int sock = accept(listen_sock, (struct sockaddr *)&sourceAddr, &addrLen);
  60.             if (sock < 0) {
  61.                 ESP_LOGE(SOCK_TAG, "Unable to accept connection: errno %d", errno);
  62.                 break;
  63.             }
  64.             ESP_LOGI(SOCK_TAG, "Socket accepted");
  65.             uart_write_bytes(UART_NUM_1, "@wifi_connected\n", 16);
  66.  
  67.             while (1) {
  68.                 int len = recv(sock, rx_buffer, sizeof(rx_buffer) - 1, 0);
  69.                 // Test the request
  70.  
  71.                     // Get the sender's ip address as string
  72.                     if (sourceAddr.sin6_family == PF_INET) {
  73.                         inet_ntoa_r(((struct sockaddr_in *)&sourceAddr)->sin_addr.s_addr, addr_str, sizeof(addr_str) - 1);
  74.                     } else if (sourceAddr.sin6_family == PF_INET6) {
  75.                         inet6_ntoa_r(sourceAddr.sin6_addr, addr_str, sizeof(addr_str) - 1);
  76.                         rx_buffer[len] = 0; // Null-terminate whatever we received and treat like a string
  77.                     }
  78.  
  79.                     ESP_LOGI(SOCK_TAG, "Received %d bytes from %s on socket", len, addr_str);
  80.                     wifi_response = 1;
  81.                     uart_write_bytes(UART_NUM_1, rx_buffer, len);
  82.  
  83.                     ulTaskNotifyTake(pdTRUE, portMAX_DELAY); // Will be woken up when write_wifi() is called
  84.  
  85.                     send(sock, write_buffer, write_len, 0);
  86.  
  87.                 }
  88.             }
  89.  
  90.             close(sock);
  91.             uart_write_bytes(UART_NUM_1, "@wifi_disconnected\n", 19);
  92.         }
  93.     }
  94.     vTaskDelete(NULL);
  95. }
UART code on ESP32:
  1. int current_len;
  2. int uartstate;
  3. char read_data[BUF_SIZE];
  4.  
  5. static void read_UART()
  6. {
  7.     uartstate = UART_WAITING;
  8.     // Read data from the UART
  9.     int len = uart_read_bytes(UART_NUM_1, read_data + current_len, BUF_SIZE-1-current_len, 20 / portTICK_RATE_MS);
  10.  
  11.     current_len += len;
  12.  
  13.     read_data[current_len] = '\0';
  14.  
  15.     if(len > 0) {
  16.       ESP_LOGI(TAG, "Received %d bytes on UART. current_len = %d", len, current_len);
  17.     }
  18.  
  19.     //If '\n' is detected
  20.     if (strchr((char*)read_data, '\n') || current_len == BUF_SIZE-1) {
  21.         if (read_data[0] == '@') {
  22.                 uartstate = COMMAND_RECEIVED;
  23.                 // Process command
  24.                 ESP_LOGD(TAG, "Command received");
  25.         } 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
  26.             write_wifi((char*)read_data, current_len);
  27.             wifi_response = 0;
  28.         } else {
  29.             //the message is for the BLE
  30.             uartstate = WRITE_CHARACTERISTIC;
  31.             // write to BLE
  32.         }
  33.  
  34.         current_len = 0;
  35.     }
  36. }
sdkconfig:

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=
Thanks in advance

M3te0ra2
Posts: 3
Joined: Fri Apr 05, 2019 11:00 am

Re: Low speed on Wifi and character loss

Postby M3te0ra2 » Thu Apr 11, 2019 1:48 pm

No one?

littlesky
Posts: 51
Joined: Fri Jun 09, 2017 7:49 am

Re: Low speed on Wifi and character loss

Postby littlesky » Fri Apr 19, 2019 7:54 am

Is the sdkconfig the same as the iperf example? Did you enable WiFi and BT software coexist?

Who is online

Users browsing this forum: Baidu [Spider], Google [Bot] and 237 guests