eg/perf:modify udp_perf and some mistakes

modify udp_perf to be similar with tcp_perf.
change indentation and some mistakes of codes.
edit README.
This commit is contained in:
chenyudong 2017-03-21 20:06:32 +08:00
parent 5ecc88cb33
commit 8a163e3db0
13 changed files with 730 additions and 526 deletions

View File

@ -4,21 +4,33 @@ Some simple codes help to test the wifi performance.
Including TCP/UDP TX/RX throughput.
#tcp_perf
# tcp_perf
Using tcp.
This example is used to test tcp throughput and delay time. First you should set options in menuconfig.
This example is used to test tcp throughput and delay time.
You can set esp32 as AP/STA, client/sever, sender/receiver. Make sure that STAcan connect to AP with your configuration in menuconfig.
First you should set menuconfig.
So the tcp_perf can be used in following scenes:
You can set esp32 will be use as AP/STA, client/sever, sender/receiver in menuconfig. Also some config such as SSID, PASSWORD, SEVER_IP can be set in menuconfig.
* esp32 to Router (using esp32 as STA)
* esp32 to Wifi adaptor (using esp32 as AP)
* esp32 to esp32 (using one of them as AP, the other STA)
Open AP, then open STA, when they make a connect, they will send/receive data.You will see the calc result in com output. Make sure that your set can let them connect.
Explaining more in [main.c](tcp_perf/main/main.c).
After connection established, TCP sever and TCP client can send data to each other. The result of throughput will show by COM.
Explaining more in [main.c](./tcp_perf/main/main.c).
# udp_perf
Similar with tcp_perf.
There are some points need to notice.
* A packet will be send from client to sever.So the sever can konw the ip&port of client.
* To easy use this example, it's better to use udp sever as recviver.
# More
See the [README.md](../README.md) file in the upper level [examples](../) directory for more information about examples.

View File

@ -1,25 +1,25 @@
menu "Example Configuration"
choice
prompt "TCP_PERF_MODE "
default MODE_TCP_SHIELDBOX
help
This option performance mode.
- for "Performance in shieldbox" setting,it will receive data by tcp.
- for "Performance in air" setting, it will send data by tcp.
- for "Performance in long distance" setting, it will send data by tcp.
config MODE_TCP_SHIELDBOX
bool "Performance in shieldbox"
config MODE_TCP_AIR
bool "Performance in air"
config MODE_TCP_LONG_DISTANCE
bool "Performance in long distance"
endchoice
#choice
# prompt "TCP_PERF_MODE "
# default MODE_TCP_SHIELDBOX
# help
# This option set performance mode.
#
# - for "Performance in shieldbox" setting,it will receive data by tcp.
#
# - for "Performance in air" setting, it will send data by tcp.
#
# - for "Performance in long distance" setting, it will send data by tcp.
#
#
#config MODE_TCP_SHIELDBOX
# bool "Performance in shieldbox"
#config MODE_TCP_AIR
# bool "Performance in air"
#config MODE_TCP_LONG_DISTANCE
# bool "Performance in long distance"
#endchoice
config TCP_PERF_WIFI_MODE_AP
bool "softap mode enable"

View File

@ -13,80 +13,77 @@ step2:
creat a tcp sever/client socket using config PORT/(IP).
if sever: wating for connect.
if client connect to sever.
step3:
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.
*/
#include "tcp_perf.h"
int connectedflag = 0;
int totle_data=0;
int totle_data = 0;
#if ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO
int totle_pack=0;
int send_success=0;
int send_fail=0;
int delay_classify[5]={0};
int totle_pack = 0;
int send_success = 0;
int send_fail = 0;
int delay_classify[5] = { 0 };
#endif
#endif /*ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO*/
//this task establish a TCP connection and receive data from TCP
static void tcp_conn(void *pvParameters)
{
ESP_LOGI(TAG, "task tcp_conn start.");
//wating for connecting to AP
/*wating for connecting to AP*/
do
{
vTaskDelay(100);
}
while(!connectedflag);
while (!connectedflag);
ESP_LOGI(TAG, "sta has connected to ap.");
//create tcp socket
/*create tcp socket*/
int socret;
#if ESP_TCP_MODE_SEVER
ESP_LOGI(TAG, "creat_tcp_sever.");
socret=creat_tcp_sever();
#else
#else /*ESP_TCP_MODE_SEVER*/
ESP_LOGI(TAG, "creat_tcp_client.");
socret=creat_tcp_client();
socret = creat_tcp_client();
#endif
if(-1==socret)
{
if(-1 == socret) {
ESP_LOGI(TAG, "creat tcp socket error,stop.");
vTaskDelete(NULL);
}
//create a task to tx/rx data
/*create a task to tx/rx data*/
TaskHandle_t tx_rx_task;
#if ESP_TCP_PERF_TX
xTaskCreate(&send_data,"send_data",4096,NULL,4,&tx_rx_task);
#else
xTaskCreate(&recv_data,"recv_data",4096,NULL,4,&tx_rx_task);
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);
#endif
int pps;
while(1)
{
totle_data=0;
//calc every 3s
vTaskDelay(3000/ portTICK_RATE_MS);
pps=totle_data/3;
while (1) {
totle_data = 0;
vTaskDelay(3000 / portTICK_RATE_MS);//every 3s
pps = totle_data / 3;
#if ESP_TCP_PERF_TX
ESP_LOGI(TAG, "tcp send %d byte per sec!",pps);
ESP_LOGI(TAG, "tcp send %d byte per sec!", pps);
#if ESP_TCP_DELAY_INFO
ESP_LOGI(TAG, "tcp send packet totle:%d succeed:%d failed:%d\n0-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_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*/
#else
ESP_LOGI(TAG, "tcp recv %d byte per sec!\n",pps);
#endif
ESP_LOGI(TAG, "tcp recv %d byte per sec!\n", pps);
#endif /*ESP_TCP_PERF_TX*/
}
close_socket();
vTaskDelete(tx_rx_task);
@ -101,9 +98,9 @@ void app_main(void)
#if ESP_WIFI_MODE_AP
ESP_LOGI(TAG, "ESP_WIFI_MODE_AP\n");
wifi_init_softap();
#else
#else /*ESP_WIFI_MODE_AP*/
ESP_LOGI(TAG, "ESP_WIFI_MODE_STA\n");
wifi_init_sta();
#endif
xTaskCreate(&tcp_conn,"tcp_conn",4096,NULL,5,NULL);
xTaskCreate(&tcp_conn, "tcp_conn", 4096, NULL, 5, NULL);
}

View File

@ -1,18 +1,6 @@
#include "tcp_perf.h"
extern int connectedflag;
extern int totle_data;
#if ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO
extern int totle_pack;
extern int send_success;
extern int send_fail;
extern int delay_classify[5];
#endif
/* FreeRTOS event group to signal when we are connected & ready to make a request */
static EventGroupHandle_t wifi_event_group;
/*socket*/
@ -34,8 +22,9 @@ static esp_err_t event_handler(void *ctx, system_event_t *event)
case SYSTEM_EVENT_STA_CONNECTED:
break;
case SYSTEM_EVENT_STA_GOT_IP:
ESP_LOGI(TAG, "event_handler:SYSTEM_EVENT_STA_GOT_IP!\n");
ESP_LOGI(TAG, "ip:%s\n",ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
ESP_LOGI(TAG, "event_handler:SYSTEM_EVENT_STA_GOT_IP!");
ESP_LOGI(TAG, "got ip:%s\n",
ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
connectedflag=1;
break;
case SYSTEM_EVENT_AP_STACONNECTED:
@ -58,54 +47,59 @@ static esp_err_t event_handler(void *ctx, system_event_t *event)
//send data
void send_data(void *pvParameters)
{
int len=0;
int len = 0;
char databuff[DEFAULT_PKTSIZE];
memset(databuff,97,DEFAULT_PKTSIZE);
memset(databuff, PACK_BYTE_IS, DEFAULT_PKTSIZE);
vTaskDelay(100/portTICK_RATE_MS);
ESP_LOGI(TAG,"start sending...");
while(1)
{
ESP_LOGI(TAG, "start sending...");
#if ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO
//delaytime
struct timeval tv_start;
struct timeval tv_finish;
unsigned int send_delay_ms;
totle_pack++;
gettimeofday(&tv_start,NULL);
#endif
len=send(connect_soc, databuff, DEFAULT_PKTSIZE, 0);
unsigned long send_delay_ms;
#endif /*ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO*/
while(1) {
#if ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO
gettimeofday(&tv_finish,NULL);
#endif
if(len>0)
{
totle_data+=len;
//vTaskDelay(1000/portTICK_RATE_MS);
totle_pack++;
gettimeofday(&tv_start, NULL);
#endif /*ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO*/
//send function
len = send(connect_soc, databuff, DEFAULT_PKTSIZE, 0);
#if ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO
gettimeofday(&tv_finish, NULL);
#endif /*ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO*/
if(len > 0) {
totle_data += len;
#if ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO
send_success++;
send_delay_ms=(tv_finish.tv_sec-tv_start.tv_sec)*1000
+(tv_finish.tv_usec-tv_start.tv_usec)/1000;
//ESP_LOGI(TAG, "send_delay_ms=%d",send_delay_ms);
if(send_delay_ms<30)
send_delay_ms = (tv_finish.tv_sec - tv_start.tv_sec) * 1000
+ (tv_finish.tv_usec - tv_start.tv_usec) / 1000;
//ESP_LOGI(TAG, "send_delay_ms=%ld",send_delay_ms);
if(send_delay_ms < 30)
delay_classify[0]++;
else if(send_delay_ms<100)
else if(send_delay_ms < 100)
delay_classify[1]++;
else if(send_delay_ms<300)
else if(send_delay_ms < 300)
delay_classify[2]++;
else if(send_delay_ms<1000)
else if(send_delay_ms < 1000)
delay_classify[3]++;
else
delay_classify[4]++;
#endif
}
else
{
#endif /*ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO*/
}/*if(len > 0)*/
else {
#if ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO
send_fail++;
#endif
/*for faster send&receive,don't show error code.
#endif /*ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO*/
/*for faster sending,don't show error code
*if it can't work as expectations,unnote the two lines here
**/
//perror("data_count error");
@ -113,54 +107,48 @@ void send_data(void *pvParameters)
}
}
}
//send data
//receive data
void recv_data(void *pvParameters)
{
int len=0;
int len = 0;
char databuff[DEFAULT_PKTSIZE];
while(1)
{
len=recv(connect_soc, databuff, DEFAULT_PKTSIZE, 0);
if(len>0)
{
totle_data+=len;
while (1) {
len = recv(connect_soc, databuff, DEFAULT_PKTSIZE, 0);
if (len > 0) {
totle_data += len;
}
else
{
perror("data_count error");
vTaskDelay(500/portTICK_RATE_MS);
else {
perror("recv_data error");
vTaskDelay(500 / portTICK_RATE_MS);
}
}
}
//use this esp32 as a tcp sever. return 0:success -1:error
int creat_tcp_sever()
{
ESP_LOGI(TAG, "sever socket....port=%d\n",DEFAULTPORT);
ESP_LOGI(TAG, "sever socket....port=%d\n", DEFAULT_PORT);
sever_socket = socket(AF_INET, SOCK_STREAM, 0);
if(sever_socket < 0)
{
if (sever_socket < 0) {
perror("socket() error:");
return -1;
}
sever_addr.sin_family = AF_INET;
sever_addr.sin_port = htons(DEFAULTPORT);
sever_addr.sin_port = htons(DEFAULT_PORT);
sever_addr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(sever_socket,(struct sockaddr*)&sever_addr,sizeof(sever_addr))<0)
{
if (bind(sever_socket, (struct sockaddr*)&sever_addr, sizeof(sever_addr)) < 0) {
perror("bind() error");
close(sever_socket);
return -1;
}
if(listen(sever_socket,5)<0)
{
if (listen(sever_socket, 5) < 0) {
perror("listen() error");
close(sever_socket);
return -1;
}
connect_soc = accept(sever_socket,(struct sockaddr*)&client_addr,&socklen);
if(connect_soc<0)
{
connect_soc = accept(sever_socket, (struct sockaddr*)&client_addr, &socklen);
if (connect_soc<0) {
perror("accept() error");
close(sever_socket);
return -1;
@ -172,23 +160,22 @@ int creat_tcp_sever()
//use this esp32 as a tcp client. return 0:success -1:error
int creat_tcp_client()
{
ESP_LOGI(TAG, "client socket....severip:port=%s:%d\n",DEFAULTSEVERIP,DEFAULTPORT);
ESP_LOGI(TAG, "client socket....severip:port=%s:%d\n",
DEFAULT_SEVER_IP, DEFAULT_PORT);
connect_soc = socket(AF_INET, SOCK_STREAM, 0);
if(connect_soc < 0)
{
if (connect_soc < 0) {
perror("socket failed!");
return -1;
}
sever_addr.sin_family = AF_INET;
sever_addr.sin_port = htons(DEFAULTPORT);
sever_addr.sin_addr.s_addr = inet_addr(DEFAULTSEVERIP);
printf("connecting to sever...");
if(connect(connect_soc, (struct sockaddr *)&sever_addr, sizeof(sever_addr)) < 0)
{
sever_addr.sin_port = htons(DEFAULT_PORT);
sever_addr.sin_addr.s_addr = inet_addr(DEFAULT_SEVER_IP);
ESP_LOGI(TAG, "connecting to sever...");
if (connect(connect_soc, (struct sockaddr *)&sever_addr, sizeof(sever_addr)) < 0) {
perror("connect to sever error!");
return -1;
}
ESP_LOGI(TAG,"connect to sever success!");
ESP_LOGI(TAG, "connect to sever success!");
return 0;
}
@ -197,48 +184,50 @@ void wifi_init_sta()
{
tcpip_adapter_init();
wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL) );
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
wifi_config_t wifi_config = {
.sta = {
.ssid = DEFAULTSSID,
.password = DEFAULTPWD
.ssid = DEFAULT_SSID,
.password = DEFAULT_PWD
},
};
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
ESP_ERROR_CHECK( esp_wifi_start() );
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
ESP_ERROR_CHECK(esp_wifi_start() );
ESP_LOGI(TAG, "wifi_init_sta finished.");
ESP_LOGI(TAG, "connect to ap SSID:%s password:%s \n",DEFAULTSSID,DEFAULTPWD);
ESP_LOGI(TAG, "connect to ap SSID:%s password:%s \n",
DEFAULT_SSID,DEFAULT_PWD);
}
//wifi_init_softap
void wifi_init_softap()
{
tcpip_adapter_init();
wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
wifi_config_t wifi_config = {
.ap = {
.ssid = DEFAULTSSID,
.ssid = DEFAULT_SSID,
.ssid_len=0,
.max_connection=MAXSTACONN,
.password = DEFAULTPWD,
.max_connection=MAX_STA_CONN,
.password = DEFAULT_PWD,
.authmode=WIFI_AUTH_WPA_WPA2_PSK
},
};
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_AP) );
ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config) );
ESP_ERROR_CHECK( esp_wifi_start() );
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());
ESP_LOGI(TAG, "wifi_init_softap finished.SSID:%s password:%s \n",DEFAULTSSID,DEFAULTPWD);
ESP_LOGI(TAG, "wifi_init_softap finished.SSID:%s password:%s \n",
DEFAULT_SSID, DEFAULT_PWD);
}
void close_socket()

View File

@ -24,12 +24,12 @@ extern "C" {
#endif
/*AP info and tcp_sever info*/
#define DEFAULTSSID CONFIG_TCP_PERF_WIFI_SSID
#define DEFAULTPWD CONFIG_TCP_PERF_WIFI_PASSWORD
#define DEFAULTPORT CONFIG_TCP_PERF_SEVER_PORT
#define DEFAULTSEVERIP CONFIG_TCP_PERF_SERVER_IP
#define DEFAULT_SSID CONFIG_TCP_PERF_WIFI_SSID
#define DEFAULT_PWD CONFIG_TCP_PERF_WIFI_PASSWORD
#define DEFAULT_PORT CONFIG_TCP_PERF_SEVER_PORT
#define DEFAULT_SEVER_IP CONFIG_TCP_PERF_SERVER_IP
#define DEFAULT_PKTSIZE CONFIG_TCP_PERF_PKT_SIZE
#define MAXSTACONN 1 //how many sta can be connected(AP mode)
#define MAX_STA_CONN 1 //how many sta can be connected(AP mode)
/*test options*/
#define ESP_WIFI_MODE_AP CONFIG_TCP_PERF_WIFI_MODE_AP //TRUE:AP FALSE:STA
#define ESP_TCP_MODE_SEVER CONFIG_TCP_PERF_SEVER //TRUE:sever FALSE:client
@ -37,9 +37,20 @@ extern "C" {
#define ESP_TCP_DELAY_INFO CONFIG_TCP_PERF_DELAY_DEBUG //TRUE:show delay time info
#define PACK_BYTE_IS 97 //'a'
#define TAG "tcp_perf:"
extern int connectedflag;
extern int totle_data;
#if ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO
extern int totle_pack;
extern int send_success;
extern int send_fail;
extern int delay_classify[5];
#endif/*ESP_TCP_PERF_TX && ESP_TCP_DELAY_INFO*/
//using esp as station
void wifi_init_sta();
@ -67,5 +78,6 @@ void close_socket();
}
#endif
#endif
#endif /*#ifndef __TCP_PERF_H__*/

View File

@ -1,43 +0,0 @@
menu "Example Configuration"
config WIFI_SSID
string "WiFi SSID"
default "tp_wifi_test1"
help
SSID (network name) for the example to connect to.
config WIFI_PASSWORD
string "WiFi Password"
default "1234567890"
help
WiFi password (WPA or WPA2) for the example to use.
config SEVER_PORT
int "tcp sever port"
default 4567
help
which will the udp sever use.
config BUFF_SIZE
int "buff size"
default 1024
help
the data send&recv buff size.
choice
prompt "test mode"
default PERFORMANCE_MODE
help
This option performance mode.
- for "udp receive" setting,it will receive data by udp.
- for "udp send" setting, it will send data by udp.
config MODE_UDP_RECV
bool "udp receive"
config MODE_UDP_SEND
bool "udp send"
endchoice
endmenu

View File

@ -1,199 +0,0 @@
/*
udp_esp2ap test
This example use esp32 as station,
when esp32 start,it will connect to an AP and will create a UDP socket,
use a UDP client send a message first to let it know the client IP and port,
then it will send/receive data,(set work mode in menuconfig)
And calculate the speed of sending/receiving data.
*/
#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "esp_system.h"
#include "esp_task_wdt.h"
#include "nvs_flash.h"
#include <sys/socket.h>
#include <netinet/in.h>
#include "driver/uart.h"
#include "soc/uart_struct.h"
/*AP info and tcp_sever info*/
//#define DEFAULTSSID "wifi-12"
//#define DEFAULTPWD "sumof1+1=2"
#define DEFAULTSSID CONFIG_WIFI_SSID
#define DEFAULTPWD CONFIG_WIFI_PASSWORD
#define DEFAULTPORT CONFIG_SEVER_PORT
#define BUFFSIZE CONFIG_BUFF_SIZE
static int totle_data=0;
/* FreeRTOS event group to signal when we are connected & ready to make a request */
static EventGroupHandle_t wifi_event_group;
/**/
static int connectedflag = 0;
static int mysocket;
static esp_err_t event_handler(void *ctx, system_event_t *event)
{
switch(event->event_id) {
case SYSTEM_EVENT_STA_START:
esp_wifi_connect();
//printf("event_handler:SYSTEM_EVENT_STA_START\n");
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
//printf("event_handler:SYSTEM_EVENT_STA_DISCONNECTED\n");
esp_wifi_connect();
break;
case SYSTEM_EVENT_STA_CONNECTED:
//printf("event_handler:SYSTEM_EVENT_STA_CONNECTED!\n");
break;
case SYSTEM_EVENT_STA_GOT_IP:
printf("event_handler:SYSTEM_EVENT_STA_GOT_IP!\n");
printf("ip:%s\n",ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
connectedflag=1;
break;
default:
break;
}
return ESP_OK;
}
//wifi_init
static void wifi_init()
{
tcpip_adapter_init();
wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
wifi_config_t wifi_config = {
.sta = {
.ssid = DEFAULTSSID,
.password = DEFAULTPWD
},
};
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
ESP_ERROR_CHECK( esp_wifi_start() );
esp_wifi_connect();
printf("wifi_init over.\n");
}
static void data_count(void *pvParameters)
{
printf("task data_count start!\n");
int len;
char databuff[BUFFSIZE];
struct sockaddr_in client_addr;
unsigned int socklen = sizeof(client_addr);
len=recvfrom(mysocket, databuff, BUFFSIZE, 0,(struct sockaddr *)&client_addr,&socklen);
if(len<0)
{
perror("first recv error:");
close(mysocket);
vTaskDelete(NULL);
}
else
{
#ifdef CONFIG_MODE_TCP_SEND
printf("send data to %s:%u\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
memset(databuff,97,BUFFSIZE);
#else
printf("recv data from %s:%u\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
#endif
}
socklen=sizeof(client_addr);
printf("start calc!\n");
while(1)
{
#ifdef CONFIG_MODE_UDP_SEND
len=sendto(mysocket, databuff, BUFFSIZE, 0, (struct sockaddr *)&client_addr, sizeof(client_addr));
#else
len=recvfrom(mysocket, databuff, BUFFSIZE, 0,(struct sockaddr *)&client_addr,&socklen);
#endif
//printf("len= %d\n",len);
if(len>0)
{
totle_data+=len;
}
else
{
//perror("data_count:\n");
/*you'd better turn off watch dog in menuconfig
*Component config->ESP32-specific->Task watchdog.
**/
//vTaskDelay(1/portTICK_RATE_MS);
}
}
}
//this task establish a udp connection and send/recv data
static void udp_connect(void *pvParameters)
{
TaskHandle_t tasksend;
do
{
vTaskDelay(100);
}
while(!connectedflag);
//wating for connecting to AP
mysocket = socket(AF_INET, SOCK_DGRAM, 0);
if(mysocket < 0)
{
perror("socket failed:");
vTaskDelete(NULL);
}
struct sockaddr_in myaddr;
myaddr.sin_family=AF_INET;
myaddr.sin_port=htons(DEFAULTPORT);
if(bind(mysocket,(struct sockaddr *)&myaddr,sizeof(myaddr))<0)
{
perror("bind local port error:");
close(mysocket);
vTaskDelete(NULL);
}
vTaskDelay(2000/portTICK_RATE_MS);
//create a new task...
xTaskCreate(&data_count,"data_count",4096,NULL,5,&tasksend);
int pps;
while(1)
{
totle_data=0;
vTaskDelay(3000/ portTICK_RATE_MS);
pps = totle_data/3;
#ifdef CONFIG_MODE_UDP_SEND
printf("udp send %d byte per sec!(just reference)\n",pps);
#else
printf("udp recv %d byte per sec!\n",pps);
#endif
}
vTaskDelete(NULL);
}
void app_main(void)
{
nvs_flash_init();
wifi_init();
xTaskCreate(&udp_connect,"udp_connect",4096,NULL,5,NULL);
}

View File

@ -0,0 +1,75 @@
menu "Example Configuration"
#choice
# prompt "UCP_PERF_MODE "
# default MODE_UDP_SHIELDBOX
# help
# This option set performance mode.
#
# - for "Performance in shieldbox" setting,it will receive data by udp.
# - for "Performance in air" setting, it will send data by udp.
#
# - for "Performance in long distance" setting, it will send data by udp.
#
#
#config MODE_UDP_SHIELDBOX
# bool "Performance in shieldbox"
#config MODE_UDP_AIR
# bool "Performance in air"
#config MODE_UDP_LONG_DISTANCE
# bool "Performance in long distance"
#endchoice
#
config UDP_PERF_WIFI_MODE_AP
bool "softap mode enable"
default n
help
yes:ESP32 is softap. no:ESP32 is station.
config UDP_PERF_SEVER
bool "TCP performance sever enable"
default n
help
yes:ESP32 is UDP sever. no:ESP32 is UDP client.
We suggest to make this config be same with "Station mode".
config UDP_PERF_TX
bool "UDP performance TX test enable"
default y
help
yes:UDP TX test. no:UDP RX test.
config UDP_PERF_WIFI_SSID
string "WiFi SSID"
default "tp_wifi_test1"
help
SSID (network name) for the example to connect to.
config UDP_PERF_WIFI_PASSWORD
string "WiFi Password"
default "1234567890"
help
WiFi password (WPA or WPA2) for the example to use.
config UDP_PERF_SEVER_PORT
int "UDP sever port"
default 4567
help
Which will the udp sever use.
config UDP_PERF_SERVER_IP
string "UDP server ip"
default "192.168.4.1"
help
IP of UDP server.
Ignore in UDP sever.
config UDP_PERF_PKT_SIZE
int "Size of UDP packet"
default 1460
help
the data send&recv packet size.
endmenu

View File

@ -0,0 +1,90 @@
/*
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:
creat a udp sever/client socket using config PORT/(IP).
if sever: wating for the first message of client.
if client: sending a packet to sever first.
step3:
send/receive data to/from each other.
you can see the info in com port output.
*/
#include "udp_perf.h"
int connectedflag = 0;
int totle_data = 0;
int success_pack = 0;
//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*/
do
{
vTaskDelay(100);
}
while (!connectedflag);
ESP_LOGI(TAG, "sta has connected to ap.");
/*create udp socket*/
int socret;
#if ESP_UDP_MODE_SEVER
ESP_LOGI(TAG, "creat_udp_sever.");
socret=creat_udp_sever();
//vTaskDelay(1000/portTICK_RATE_MS);
#else /*ESP_UDP_MODE_SEVER*/
ESP_LOGI(TAG, "creat_udp_client.");
socret = creat_udp_client();
#endif
if(-1 == socret) {
ESP_LOGI(TAG, "creat udp socket error,stop.");
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);
int pps;
while (1) {
totle_data = 0;
vTaskDelay(3000 / portTICK_RATE_MS);//every 3s
pps = totle_data / 3;
#if ESP_UDP_PERF_TX
ESP_LOGI(TAG, "udp send %d byte per sec! totle pack: %d \n", pps, success_pack);
#else
ESP_LOGI(TAG, "udp recv %d byte per sec! totle pack: %d \n", pps, success_pack);
#endif /*ESP_UDP_PERF_TX*/
}
close_socket();
vTaskDelete(tx_rx_task);
vTaskDelete(NULL);
}
void app_main(void)
{
nvs_flash_init();
#if ESP_WIFI_MODE_AP
ESP_LOGI(TAG, "ESP_WIFI_MODE_AP\n");
wifi_init_softap();
#else /*ESP_WIFI_MODE_AP*/
ESP_LOGI(TAG, "ESP_WIFI_MODE_STA\n");
wifi_init_sta();
#endif
xTaskCreate(&udp_conn, "udp_conn", 4096, NULL, 5, NULL);
}

View File

@ -0,0 +1,198 @@
#include "udp_perf.h"
/* FreeRTOS event group to signal when we are connected & ready to make a request */
static EventGroupHandle_t wifi_event_group;
static int mysocket;
static struct sockaddr_in remote_addr;
static unsigned int socklen;
static esp_err_t event_handler(void *ctx, system_event_t *event)
{
switch(event->event_id) {
case SYSTEM_EVENT_STA_START:
esp_wifi_connect();
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
esp_wifi_connect();
break;
case SYSTEM_EVENT_STA_CONNECTED:
break;
case SYSTEM_EVENT_STA_GOT_IP:
ESP_LOGI(TAG, "event_handler:SYSTEM_EVENT_STA_GOT_IP!");
ESP_LOGI(TAG, "got ip:%s\n",
ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
connectedflag=1;
break;
case SYSTEM_EVENT_AP_STACONNECTED:
ESP_LOGI(TAG, "station:"MACSTR" join,AID=%d\n",
MAC2STR(event->event_info.sta_connected.mac),
event->event_info.sta_connected.aid);
connectedflag=1;
break;
case SYSTEM_EVENT_AP_STADISCONNECTED:
ESP_LOGI(TAG, "station:"MACSTR"leave,AID=%d\n",
MAC2STR(event->event_info.sta_disconnected.mac),
event->event_info.sta_disconnected.aid);
break;
default:
break;
}
return ESP_OK;
}
//wifi_init_sta
void wifi_init_sta()
{
tcpip_adapter_init();
wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL) );
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
wifi_config_t wifi_config = {
.sta = {
.ssid = DEFAULT_SSID,
.password = DEFAULT_PWD
},
};
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
ESP_ERROR_CHECK(esp_wifi_start() );
ESP_LOGI(TAG, "wifi_init_sta finished.");
ESP_LOGI(TAG, "connect to ap SSID:%s password:%s \n",
DEFAULT_SSID,DEFAULT_PWD);
}
//wifi_init_softap
void wifi_init_softap()
{
tcpip_adapter_init();
wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
wifi_config_t wifi_config = {
.ap = {
.ssid = DEFAULT_SSID,
.ssid_len=0,
.max_connection=MAX_STA_CONN,
.password = DEFAULT_PWD,
.authmode=WIFI_AUTH_WPA_WPA2_PSK
},
};
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());
ESP_LOGI(TAG, "wifi_init_softap finished.SSID:%s password:%s \n",
DEFAULT_SSID, DEFAULT_PWD);
}
//creat a udp sever socket. return 0:success -1:error
int creat_udp_sever()
{
ESP_LOGI(TAG, "creat_udp_sever()");
mysocket = socket(AF_INET, SOCK_DGRAM, 0);
if (mysocket < 0) {
perror("socket failed:");
return -1;
}
struct sockaddr_in sever_addr;
sever_addr.sin_family = AF_INET;
sever_addr.sin_port = htons(DEFAULT_PORT);
sever_addr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(mysocket, (struct sockaddr *)&sever_addr, sizeof(sever_addr)) < 0) {
perror("bind local port error:");
close(mysocket);
return -1;
}
return 0;
}
//creat a udp client socket. return 0:success -1:error
int creat_udp_client()
{
ESP_LOGI(TAG, "creat_udp_client()");
mysocket = socket(AF_INET, SOCK_DGRAM, 0);
if (mysocket < 0) {
perror("socket failed:");
return -1;
}
/*for client remote_addr is also sever_addr*/
remote_addr.sin_family = AF_INET;
remote_addr.sin_port = htons(DEFAULT_PORT);
remote_addr.sin_addr.s_addr = inet_addr(DEFAULT_SEVER_IP);
return 0;
}
//send or recv data task
void send_recv_data(void *pvParameters)
{
ESP_LOGI(TAG, "task send_recv_data start!\n");
int len;
char databuff[DEFAULT_PKTSIZE];
/*send&receive first packet*/
socklen = sizeof(remote_addr);
memset(databuff, PACK_BYTE_IS, DEFAULT_PKTSIZE);
#if ESP_UDP_MODE_SEVER
ESP_LOGI(TAG, "first recvfrom:");
len = recvfrom(mysocket, databuff, DEFAULT_PKTSIZE, 0, (struct sockaddr *)&remote_addr, &socklen);
#else
ESP_LOGI(TAG, "first sendto:");
len = sendto(mysocket, databuff, DEFAULT_PKTSIZE, 0, (struct sockaddr *)&remote_addr, sizeof(remote_addr));
#endif
if (len > 0) {
ESP_LOGI(TAG, "transfer data with %s:%u\n",
inet_ntoa(remote_addr.sin_addr), ntohs(remote_addr.sin_port));
} else {
perror("first recv&send error:");
close(mysocket);
vTaskDelete(NULL);
}
#if ESP_UDP_PERF_TX
vTaskDelay(500 / portTICK_RATE_MS);
#endif
ESP_LOGI(TAG, "start count!\n");
while(1)
{
/*you can add delay time for fixed-frequency*/
//vTaskDelay(5 / portTICK_RATE_MS);
#if ESP_UDP_PERF_TX
len = sendto(mysocket, databuff, DEFAULT_PKTSIZE, 0, (struct sockaddr *)&remote_addr, sizeof(remote_addr));
#else
len = recvfrom(mysocket, databuff, DEFAULT_PKTSIZE, 0, (struct sockaddr *)&remote_addr, &socklen);
#endif
//printf("%d\n",len);
//vTaskDelay(100/portTICK_RATE_MS);
if (len > 0) {
totle_data += len;
success_pack++;
} else {
//perror("data_count:\n");
/*you'd better turn off watch dog in menuconfig
*Component config->ESP32-specific->Task watchdog.
**/
//vTaskDelay(1/portTICK_RATE_MS);
}
}
}
void close_socket()
{
close(mysocket);
}

View File

@ -0,0 +1,73 @@
#ifndef __UDP_PERF_H__
#define __UDP_PERF_H__
#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "esp_system.h"
#include "nvs_flash.h"
#include <sys/socket.h>
#include <netinet/in.h>
#include "driver/uart.h"
#include "soc/uart_struct.h"
#include "esp_log.h"
#ifdef __cplusplus
extern "C" {
#endif
/*AP info and tcp_sever info*/
#define DEFAULT_SSID CONFIG_UDP_PERF_WIFI_SSID
#define DEFAULT_PWD CONFIG_UDP_PERF_WIFI_PASSWORD
#define DEFAULT_PORT CONFIG_UDP_PERF_SEVER_PORT
#define DEFAULT_SEVER_IP CONFIG_UDP_PERF_SERVER_IP
#define DEFAULT_PKTSIZE CONFIG_UDP_PERF_PKT_SIZE
#define MAX_STA_CONN 1 //how many sta can be connected(AP mode)
/*test options*/
#define ESP_WIFI_MODE_AP CONFIG_UDP_PERF_WIFI_MODE_AP //TRUE:AP FALSE:STA
#define ESP_UDP_MODE_SEVER CONFIG_UDP_PERF_SEVER //TRUE:sever FALSE:client
#define ESP_UDP_PERF_TX CONFIG_UDP_PERF_TX //TRUE:send FALSE:receive
#define PACK_BYTE_IS 97 //'a'
#define TAG "udp_perf:"
extern int connectedflag;
extern int totle_data;
extern int success_pack;
//using esp as station
void wifi_init_sta();
//using esp as softap
void wifi_init_softap();
//creat a udp sever socket. return 0:success -1:error
int creat_udp_sever();
//creat a udp client socket. return 0:success -1:error
int creat_udp_client();
//send or recv data task
void send_recv_data(void *pvParameters);
//close all socket
void close_socket();
#ifdef __cplusplus
}
#endif
#endif /*#ifndef __UDP_PERF_H__*/