2017-04-13 11:14:01 +00:00
|
|
|
/* udp_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-21 12:06:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
udp_perf example
|
|
|
|
|
|
|
|
Using this example to test udp throughput performance.
|
|
|
|
esp<->esp or esp<->ap
|
|
|
|
|
|
|
|
step1:
|
|
|
|
init wifi as AP/STA using config SSID/PASSWORD.
|
|
|
|
|
|
|
|
step2:
|
2017-04-10 12:39:04 +00:00
|
|
|
create a udp server/client socket using config PORT/(IP).
|
|
|
|
if server: wating for the first message of client.
|
|
|
|
if client: sending a packet to server first.
|
2017-03-21 12:06:32 +00:00
|
|
|
|
|
|
|
step3:
|
|
|
|
send/receive data to/from each other.
|
2017-04-10 12:39:04 +00:00
|
|
|
you can see the info in serial output.
|
2017-03-21 12:06:32 +00:00
|
|
|
*/
|
|
|
|
|
2017-04-10 12:39:04 +00:00
|
|
|
|
2017-04-13 11:14:01 +00:00
|
|
|
#include <errno.h>
|
2017-03-31 08:34:12 +00:00
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
2017-04-13 11:14:01 +00:00
|
|
|
#include "freertos/event_groups.h"
|
2017-03-31 08:34:12 +00:00
|
|
|
#include "esp_log.h"
|
|
|
|
#include "esp_err.h"
|
2017-11-16 03:52:55 +00:00
|
|
|
#include "nvs_flash.h"
|
2017-03-31 08:34:12 +00:00
|
|
|
|
2017-03-21 12:06:32 +00:00
|
|
|
#include "udp_perf.h"
|
|
|
|
|
|
|
|
|
|
|
|
//this task establish a UDP connection and receive data from UDP
|
|
|
|
static void udp_conn(void *pvParameters)
|
|
|
|
{
|
|
|
|
ESP_LOGI(TAG, "task udp_conn start.");
|
|
|
|
/*wating for connecting to AP*/
|
2017-04-13 11:14:01 +00:00
|
|
|
xEventGroupWaitBits(udp_event_group, WIFI_CONNECTED_BIT,false, true, portMAX_DELAY);
|
2017-03-21 12:06:32 +00:00
|
|
|
ESP_LOGI(TAG, "sta has connected to ap.");
|
|
|
|
|
|
|
|
/*create udp socket*/
|
2017-04-10 12:39:04 +00:00
|
|
|
int socket_ret;
|
2017-03-21 12:06:32 +00:00
|
|
|
|
2017-04-13 11:14:01 +00:00
|
|
|
#if EXAMPLE_ESP_UDP_MODE_SERVER
|
|
|
|
ESP_LOGI(TAG, "create udp server after 3s...");
|
2017-03-23 08:36:32 +00:00
|
|
|
vTaskDelay(3000 / portTICK_RATE_MS);
|
2017-04-10 12:39:04 +00:00
|
|
|
ESP_LOGI(TAG, "create_udp_server.");
|
|
|
|
socket_ret=create_udp_server();
|
2017-04-13 11:14:01 +00:00
|
|
|
#else /*EXAMPLE_ESP_UDP_MODE_SERVER*/
|
|
|
|
ESP_LOGI(TAG, "create udp client after 20s...");
|
2017-03-23 08:36:32 +00:00
|
|
|
vTaskDelay(20000 / portTICK_RATE_MS);
|
2017-04-10 12:39:04 +00:00
|
|
|
ESP_LOGI(TAG, "create_udp_client.");
|
|
|
|
socket_ret = create_udp_client();
|
2017-03-21 12:06:32 +00:00
|
|
|
#endif
|
2017-04-13 11:14:01 +00:00
|
|
|
if(socket_ret == ESP_FAIL) {
|
2017-04-10 12:39:04 +00:00
|
|
|
ESP_LOGI(TAG, "create udp socket error,stop.");
|
2017-03-21 12:06:32 +00:00
|
|
|
vTaskDelete(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*create a task to tx/rx data*/
|
|
|
|
TaskHandle_t tx_rx_task;
|
|
|
|
xTaskCreate(&send_recv_data, "send_recv_data", 4096, NULL, 4, &tx_rx_task);
|
|
|
|
|
2017-04-13 11:14:01 +00:00
|
|
|
/*waiting udp connected success*/
|
|
|
|
xEventGroupWaitBits(udp_event_group, UDP_CONNCETED_SUCCESS,false, true, portMAX_DELAY);
|
|
|
|
int bps;
|
2017-03-21 12:06:32 +00:00
|
|
|
while (1) {
|
2017-04-10 12:39:04 +00:00
|
|
|
total_data = 0;
|
2017-03-21 12:06:32 +00:00
|
|
|
vTaskDelay(3000 / portTICK_RATE_MS);//every 3s
|
2017-04-13 11:14:01 +00:00
|
|
|
bps = total_data / 3;
|
|
|
|
|
|
|
|
if (total_data <= 0) {
|
|
|
|
int err_ret = check_connected_socket();
|
|
|
|
if (err_ret == -1) { //-1 reason: low level netif error
|
|
|
|
ESP_LOGW(TAG, "udp send & recv stop.\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if EXAMPLE_ESP_UDP_PERF_TX
|
|
|
|
ESP_LOGI(TAG, "udp send %d byte per sec! total pack: %d \n", bps, success_pack);
|
2017-03-21 12:06:32 +00:00
|
|
|
#else
|
2017-04-13 11:14:01 +00:00
|
|
|
ESP_LOGI(TAG, "udp recv %d byte per sec! total pack: %d \n", bps, success_pack);
|
|
|
|
#endif /*EXAMPLE_ESP_UDP_PERF_TX*/
|
2017-03-21 12:06:32 +00:00
|
|
|
}
|
|
|
|
close_socket();
|
|
|
|
vTaskDelete(tx_rx_task);
|
|
|
|
vTaskDelete(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void app_main(void)
|
|
|
|
{
|
2017-11-20 05:07:52 +00:00
|
|
|
esp_err_t ret = nvs_flash_init();
|
|
|
|
if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
|
|
|
|
ESP_ERROR_CHECK(nvs_flash_erase());
|
|
|
|
ret = nvs_flash_init();
|
|
|
|
}
|
|
|
|
ESP_ERROR_CHECK( ret );
|
|
|
|
|
2017-04-13 11:14:01 +00:00
|
|
|
#if EXAMPLE_ESP_WIFI_MODE_AP
|
|
|
|
ESP_LOGI(TAG, "EXAMPLE_ESP_WIFI_MODE_AP");
|
2017-03-21 12:06:32 +00:00
|
|
|
wifi_init_softap();
|
2017-04-13 11:14:01 +00:00
|
|
|
#else /*EXAMPLE_ESP_WIFI_MODE_AP*/
|
|
|
|
ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
|
2017-03-21 12:06:32 +00:00
|
|
|
wifi_init_sta();
|
|
|
|
#endif
|
|
|
|
xTaskCreate(&udp_conn, "udp_conn", 4096, NULL, 5, NULL);
|
|
|
|
}
|