2017-04-13 07:14:01 -04:00
|
|
|
/* tcp_perf Example
|
|
|
|
|
|
|
|
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, this
|
|
|
|
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
|
|
CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
*/
|
2017-03-20 01:43:04 -04:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
tcp_perf example
|
|
|
|
|
|
|
|
Using this example to test tcp throughput performance.
|
|
|
|
esp<->esp or esp<->ap
|
|
|
|
|
|
|
|
step1:
|
2017-03-21 08:06:32 -04:00
|
|
|
init wifi as AP/STA using config SSID/PASSWORD.
|
2017-03-20 01:43:04 -04:00
|
|
|
|
|
|
|
step2:
|
2017-04-10 08:39:04 -04:00
|
|
|
create a tcp server/client socket using config PORT/(IP).
|
|
|
|
if server: wating for connect.
|
|
|
|
if client connect to server.
|
2017-03-20 01:43:04 -04:00
|
|
|
step3:
|
2017-03-21 08:06:32 -04:00
|
|
|
send/receive data to/from each other.
|
|
|
|
if the tcp connect established. esp will send or receive data.
|
2017-04-10 08:39:04 -04:00
|
|
|
you can see the info in serial output.
|
2017-03-20 01:43:04 -04:00
|
|
|
*/
|
|
|
|
|
2017-03-31 04:34:12 -04:00
|
|
|
#include <errno.h>
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
2017-04-13 07:14:01 -04:00
|
|
|
#include "freertos/event_groups.h"
|
2017-03-31 04:34:12 -04:00
|
|
|
#include "esp_log.h"
|
|
|
|
#include "esp_err.h"
|
|
|
|
|
2017-03-20 01:43:04 -04:00
|
|
|
#include "tcp_perf.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//this task establish a TCP connection and receive data from TCP
|
|
|
|
static void tcp_conn(void *pvParameters)
|
|
|
|
{
|
2017-04-13 07:14:01 -04:00
|
|
|
ESP_LOGI(TAG, "task tcp_conn.");
|
2017-03-21 08:06:32 -04:00
|
|
|
/*wating for connecting to AP*/
|
2017-04-13 07:14:01 -04:00
|
|
|
xEventGroupWaitBits(tcp_event_group, WIFI_CONNECTED_BIT,false, true, portMAX_DELAY);
|
|
|
|
|
2017-03-21 08:06:32 -04:00
|
|
|
ESP_LOGI(TAG, "sta has connected to ap.");
|
|
|
|
|
|
|
|
/*create tcp socket*/
|
2017-04-10 08:39:04 -04:00
|
|
|
int socket_ret;
|
2017-03-21 08:06:32 -04:00
|
|
|
|
2017-04-13 07:14:01 -04:00
|
|
|
#if EXAMPLE_ESP_TCP_MODE_SERVER
|
|
|
|
ESP_LOGI(TAG, "tcp_server will start after 3s...");
|
2017-03-23 04:36:32 -04:00
|
|
|
vTaskDelay(3000 / portTICK_RATE_MS);
|
2017-04-10 08:39:04 -04:00
|
|
|
ESP_LOGI(TAG, "create_tcp_server.");
|
|
|
|
socket_ret=create_tcp_server();
|
2017-04-13 07:14:01 -04:00
|
|
|
#else /*EXAMPLE_ESP_TCP_MODE_SERVER*/
|
|
|
|
ESP_LOGI(TAG, "tcp_client will start after 20s...");
|
2017-03-23 04:36:32 -04:00
|
|
|
vTaskDelay(20000 / portTICK_RATE_MS);
|
2017-04-10 08:39:04 -04:00
|
|
|
ESP_LOGI(TAG, "create_tcp_client.");
|
|
|
|
socket_ret = create_tcp_client();
|
2017-03-20 01:43:04 -04:00
|
|
|
#endif
|
2017-04-13 07:14:01 -04:00
|
|
|
if(socket_ret == ESP_FAIL) {
|
2017-04-10 08:39:04 -04:00
|
|
|
ESP_LOGI(TAG, "create tcp socket error,stop.");
|
2017-03-21 08:06:32 -04:00
|
|
|
vTaskDelete(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*create a task to tx/rx data*/
|
|
|
|
TaskHandle_t tx_rx_task;
|
2017-04-13 07:14:01 -04:00
|
|
|
#if EXAMPLE_ESP_TCP_PERF_TX
|
2017-03-21 08:06:32 -04:00
|
|
|
xTaskCreate(&send_data, "send_data", 4096, NULL, 4, &tx_rx_task);
|
2017-04-13 07:14:01 -04:00
|
|
|
#else /*EXAMPLE_ESP_TCP_PERF_TX*/
|
2017-03-21 08:06:32 -04:00
|
|
|
xTaskCreate(&recv_data, "recv_data", 4096, NULL, 4, &tx_rx_task);
|
2017-03-20 01:43:04 -04:00
|
|
|
#endif
|
2017-04-13 07:14:01 -04:00
|
|
|
int bps;
|
2017-03-21 08:06:32 -04:00
|
|
|
while (1) {
|
2017-04-10 08:39:04 -04:00
|
|
|
total_data = 0;
|
2017-03-21 08:06:32 -04:00
|
|
|
vTaskDelay(3000 / portTICK_RATE_MS);//every 3s
|
2017-04-13 07:14:01 -04:00
|
|
|
bps = total_data / 3;
|
2017-04-10 08:39:04 -04:00
|
|
|
if (total_data <= 0) {
|
2017-04-13 07:14:01 -04:00
|
|
|
int err_ret = check_working_socket();
|
|
|
|
if (err_ret == ECONNRESET || ECONNABORTED) {
|
|
|
|
ESP_LOGW(TAG, "tcp disconnected... stop.\n");
|
2017-03-23 04:36:32 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-03-21 08:06:32 -04:00
|
|
|
|
2017-04-13 07:14:01 -04:00
|
|
|
#if EXAMPLE_ESP_TCP_PERF_TX
|
|
|
|
ESP_LOGI(TAG, "tcp send %d byte per sec!", bps);
|
|
|
|
#if EXAMPLE_ESP_TCP_DELAY_INFO
|
2017-04-10 08:39:04 -04:00
|
|
|
ESP_LOGI(TAG, "tcp send packet total:%d succeed:%d failed:%d\n"
|
2017-03-21 08:06:32 -04:00
|
|
|
"time(ms):0-30:%d 30-100:%d 100-300:%d 300-1000:%d 1000+:%d\n",
|
2017-04-10 08:39:04 -04:00
|
|
|
total_pack, send_success, send_fail, delay_classify[0],
|
2017-03-21 08:06:32 -04:00
|
|
|
delay_classify[1], delay_classify[2], delay_classify[3], delay_classify[4]);
|
2017-04-13 07:14:01 -04:00
|
|
|
#endif /*EXAMPLE_ESP_TCP_DELAY_INFO*/
|
2017-03-20 01:43:04 -04:00
|
|
|
#else
|
2017-04-13 07:14:01 -04:00
|
|
|
ESP_LOGI(TAG, "tcp recv %d byte per sec!\n", bps);
|
|
|
|
#endif /*EXAMPLE_ESP_TCP_PERF_TX*/
|
2017-03-21 08:06:32 -04:00
|
|
|
}
|
|
|
|
close_socket();
|
|
|
|
vTaskDelete(tx_rx_task);
|
|
|
|
vTaskDelete(NULL);
|
2017-03-20 01:43:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void app_main(void)
|
|
|
|
{
|
2017-04-13 07:14:01 -04:00
|
|
|
#if EXAMPLE_ESP_WIFI_MODE_AP
|
|
|
|
ESP_LOGI(TAG, "EXAMPLE_ESP_WIFI_MODE_AP");
|
2017-03-20 01:43:04 -04:00
|
|
|
wifi_init_softap();
|
2017-04-13 07:14:01 -04:00
|
|
|
#else
|
|
|
|
ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
|
2017-03-20 01:43:04 -04:00
|
|
|
wifi_init_sta();
|
2017-04-13 07:14:01 -04:00
|
|
|
#endif /*EXAMPLE_ESP_WIFI_MODE_AP*/
|
2017-03-21 08:06:32 -04:00
|
|
|
xTaskCreate(&tcp_conn, "tcp_conn", 4096, NULL, 5, NULL);
|
2017-03-20 01:43:04 -04:00
|
|
|
}
|