2017-03-20 05:43:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
tcp_perf example
|
|
|
|
|
|
|
|
Using this example to test tcp throughput performance.
|
|
|
|
esp<->esp or esp<->ap
|
|
|
|
|
|
|
|
step1:
|
2017-03-21 12:06:32 +00:00
|
|
|
init wifi as AP/STA using config SSID/PASSWORD.
|
2017-03-20 05:43:04 +00:00
|
|
|
|
|
|
|
step2:
|
2017-03-21 12:06:32 +00:00
|
|
|
creat a tcp sever/client socket using config PORT/(IP).
|
|
|
|
if sever: wating for connect.
|
|
|
|
if client connect to sever.
|
2017-03-20 05:43:04 +00:00
|
|
|
step3:
|
2017-03-21 12:06:32 +00:00
|
|
|
send/receive data to/from each other.
|
|
|
|
if the tcp connect established. esp will send or receive data.
|
|
|
|
you can see the info in com port output.
|
2017-03-20 05:43:04 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "tcp_perf.h"
|
|
|
|
|
|
|
|
int connectedflag = 0;
|
2017-03-21 12:06:32 +00:00
|
|
|
int totle_data = 0;
|
2017-03-20 05:43:04 +00:00
|
|
|
|
|
|
|
#if ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO
|
|
|
|
|
2017-03-21 12:06:32 +00:00
|
|
|
int totle_pack = 0;
|
|
|
|
int send_success = 0;
|
|
|
|
int send_fail = 0;
|
|
|
|
int delay_classify[5] = { 0 };
|
2017-03-20 05:43:04 +00:00
|
|
|
|
2017-03-21 12:06:32 +00:00
|
|
|
#endif /*ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO*/
|
2017-03-20 05:43:04 +00:00
|
|
|
|
|
|
|
//this task establish a TCP connection and receive data from TCP
|
|
|
|
static void tcp_conn(void *pvParameters)
|
|
|
|
{
|
2017-03-21 12:06:32 +00:00
|
|
|
ESP_LOGI(TAG, "task tcp_conn start.");
|
|
|
|
/*wating for connecting to AP*/
|
|
|
|
do
|
|
|
|
{
|
|
|
|
vTaskDelay(100);
|
|
|
|
}
|
|
|
|
while (!connectedflag);
|
|
|
|
ESP_LOGI(TAG, "sta has connected to ap.");
|
|
|
|
|
|
|
|
/*create tcp socket*/
|
|
|
|
int socret;
|
|
|
|
|
2017-03-20 05:43:04 +00:00
|
|
|
#if ESP_TCP_MODE_SEVER
|
2017-03-21 12:06:32 +00:00
|
|
|
ESP_LOGI(TAG, "creat_tcp_sever.");
|
|
|
|
socret=creat_tcp_sever();
|
|
|
|
#else /*ESP_TCP_MODE_SEVER*/
|
|
|
|
ESP_LOGI(TAG, "creat_tcp_client.");
|
|
|
|
socret = creat_tcp_client();
|
2017-03-20 05:43:04 +00:00
|
|
|
#endif
|
2017-03-21 12:06:32 +00:00
|
|
|
if(-1 == socret) {
|
|
|
|
ESP_LOGI(TAG, "creat tcp socket error,stop.");
|
|
|
|
vTaskDelete(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*create a task to tx/rx data*/
|
|
|
|
TaskHandle_t tx_rx_task;
|
2017-03-20 05:43:04 +00:00
|
|
|
#if ESP_TCP_PERF_TX
|
2017-03-21 12:06:32 +00:00
|
|
|
xTaskCreate(&send_data, "send_data", 4096, NULL, 4, &tx_rx_task);
|
|
|
|
#else /*ESP_TCP_PERF_TX*/
|
|
|
|
xTaskCreate(&recv_data, "recv_data", 4096, NULL, 4, &tx_rx_task);
|
2017-03-20 05:43:04 +00:00
|
|
|
#endif
|
2017-03-21 12:06:32 +00:00
|
|
|
int pps;
|
|
|
|
while (1) {
|
|
|
|
totle_data = 0;
|
|
|
|
vTaskDelay(3000 / portTICK_RATE_MS);//every 3s
|
|
|
|
pps = totle_data / 3;
|
|
|
|
|
2017-03-20 05:43:04 +00:00
|
|
|
#if ESP_TCP_PERF_TX
|
2017-03-21 12:06:32 +00:00
|
|
|
ESP_LOGI(TAG, "tcp send %d byte per sec!", pps);
|
2017-03-20 05:43:04 +00:00
|
|
|
#if ESP_TCP_DELAY_INFO
|
2017-03-21 12:06:32 +00:00
|
|
|
ESP_LOGI(TAG, "tcp send packet totle:%d succeed:%d failed:%d\n"
|
|
|
|
"time(ms):0-30:%d 30-100:%d 100-300:%d 300-1000:%d 1000+:%d\n",
|
|
|
|
totle_pack, send_success, send_fail, delay_classify[0],
|
|
|
|
delay_classify[1], delay_classify[2], delay_classify[3], delay_classify[4]);
|
|
|
|
#endif /*ESP_TCP_DELAY_INFO*/
|
2017-03-20 05:43:04 +00:00
|
|
|
#else
|
2017-03-21 12:06:32 +00:00
|
|
|
ESP_LOGI(TAG, "tcp recv %d byte per sec!\n", pps);
|
|
|
|
#endif /*ESP_TCP_PERF_TX*/
|
|
|
|
}
|
|
|
|
close_socket();
|
|
|
|
vTaskDelete(tx_rx_task);
|
|
|
|
vTaskDelete(NULL);
|
2017-03-20 05:43:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void app_main(void)
|
|
|
|
{
|
|
|
|
nvs_flash_init();
|
|
|
|
#if ESP_WIFI_MODE_AP
|
|
|
|
ESP_LOGI(TAG, "ESP_WIFI_MODE_AP\n");
|
|
|
|
wifi_init_softap();
|
2017-03-21 12:06:32 +00:00
|
|
|
#else /*ESP_WIFI_MODE_AP*/
|
2017-03-20 05:43:04 +00:00
|
|
|
ESP_LOGI(TAG, "ESP_WIFI_MODE_STA\n");
|
|
|
|
wifi_init_sta();
|
|
|
|
#endif
|
2017-03-21 12:06:32 +00:00
|
|
|
xTaskCreate(&tcp_conn, "tcp_conn", 4096, NULL, 5, NULL);
|
2017-03-20 05:43:04 +00:00
|
|
|
}
|