wifi performance: add wifi performance examples including TCP&UDP TX/RX

This commit is contained in:
yudongchen95 2017-03-13 19:29:24 +08:00 committed by chenyudong
parent 85e709705b
commit 99578f1bd0
12 changed files with 704 additions and 0 deletions

View file

@ -0,0 +1,9 @@
#
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
# project subdirectory.
#
PROJECT_NAME := TCP_recv
include $(IDF_PATH)/make/project.mk

View file

@ -0,0 +1,153 @@
/*
TCP_recv example
this example will receive data as much as it can,
and calculate how much data it has received per second.
*/
#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"
/*AP info and tcp_sever info*/
//#define DEFAULTSSID "wifi-12"
//#define DEFAULTPWD "sumof1+1=2"
#define DEFAULTSSID "tp_wifi_test"
#define DEFAULTPWD "1234567890"
#define DEFAULTPORT 4567
#define DEFAULTSEVER "192.168.2.183"
#define BUFFSIZE 1024
static int sizepersec=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 clientsocket;
static struct sockaddr_in serverAddr;
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");
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 recvdata(void *pvParameters)
{
int len=0;
char buffrev[BUFFSIZE];
memset(buffrev,0,sizeof(buffrev));
while(1)
{
len=recv(clientsocket, buffrev, BUFFSIZE, 0);
if(len>0)
{
sizepersec+=len;
}
}
}
//this task establish a tcp connection and recv data from tcp
static void tcp_client(void *pvParameters)
{
TaskHandle_t tasksend;
do
{
vTaskDelay(100);
}
while(!connectedflag);
//wating for connecting to AP
clientsocket = socket(AF_INET, SOCK_STREAM, 0);
if(clientsocket < 0)
{
printf("socket failed!\n");
vTaskDelete(NULL);
}
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(DEFAULTPORT);
serverAddr.sin_addr.s_addr = inet_addr(DEFAULTSEVER);
printf("connecting to sever...\n");
if(connect(clientsocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0)
{
printf("connect to sever error!\n");
vTaskDelete(NULL);
}
printf("connect succeed!now can recv&send!\n");
//create a new task...
send(clientsocket, "hello", 6, 0);
xTaskCreate(&recvdata,"recvdata",4096,NULL,5,&tasksend);
while(1)
{
sizepersec=0;
//calc every 3s
vTaskDelay(3000/ portTICK_RATE_MS);
printf("tcp recv %d byte per sec!\n",sizepersec/3);
}
}
void app_main(void)
{
nvs_flash_init();
wifi_init();
xTaskCreate(&tcp_client,"tcp_client",4096,NULL,5,NULL);
}

View file

@ -0,0 +1,8 @@
#
# Main component makefile.
#
# This Makefile can be left empty. By default, it will take the sources in the
# src/ directory, compile them and link them into lib(subdirectory_name).a
# in the build directory. This behaviour is entirely configurable,
# please read the ESP-IDF documents if you need to do this.
#

View file

@ -0,0 +1,9 @@
#
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
# project subdirectory.
#
PROJECT_NAME := TCP_send
include $(IDF_PATH)/make/project.mk

View file

@ -0,0 +1,137 @@
/*
TCP_send example
this example will creat a TCP connect,
and send data constantly.
*/
#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"
/*AP info and tcp_sever info*/
//#define DEFAULTSSID "wifi-12"
//#define DEFAULTPWD "sumof1+1=2"
#define DEFAULTSSID "tp_wifi_test1"
#define DEFAULTPWD "1234567890"
#define DEFAULTPORT 4567
#define DEFAULTSEVER "192.168.0.102"
#define BUFFSIZE 1024
/* 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 clientsocket;
static struct sockaddr_in serverAddr;
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");
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");
}
//this task establish a tcp connection and send data
static void tcp_client(void *pvParameters)
{
do
{
vTaskDelay(100);
}
while(!connectedflag);
//wating for connecting to AP
clientsocket = socket(AF_INET, SOCK_STREAM, 0);
if(clientsocket < 0)
{
printf("socket failed!\n");
vTaskDelete(NULL);
}
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(DEFAULTPORT);
serverAddr.sin_addr.s_addr = inet_addr(DEFAULTSEVER);
printf("connecting to sever...\n");
if(connect(clientsocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0)
{
printf("connect to sever error!\n");
vTaskDelete(NULL);
}
printf("connect succeed!\n");
//create a new task...
//send(clientsocket, "hello", 6, 0);
vTaskDelay(1000/portTICK_RATE_MS);
char buffsend[BUFFSIZE];
memset(buffsend,97,BUFFSIZE);//'a'=97
//send as much as it can
while(1)
{
send(clientsocket, buffsend, BUFFSIZE, 0);
//vTaskDelay(3000/portTICK_RATE_MS);
}
}
void app_main(void)
{
nvs_flash_init();
wifi_init();
xTaskCreate(&tcp_client,"tcp_client",4096,NULL,5,NULL);
}

View file

@ -0,0 +1,8 @@
#
# Main component makefile.
#
# This Makefile can be left empty. By default, it will take the sources in the
# src/ directory, compile them and link them into lib(subdirectory_name).a
# in the build directory. This behaviour is entirely configurable,
# please read the ESP-IDF documents if you need to do this.
#

View file

@ -0,0 +1,9 @@
#
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
# project subdirectory.
#
PROJECT_NAME := UDP_recv
include $(IDF_PATH)/make/project.mk

View file

@ -0,0 +1,173 @@
/*
UDP_Client RECV
This Demo use esp32 as station,
when esp32 start,it will connect to an AP and will create a UDP socket and receive data.
And calculate speed of 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 "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 "tp_wifi_test1"
#define DEFAULTPWD "1234567890"
#define DEFAULTPORT 4567
#define DEFAULTSEVER "192.168.0.102"
#define DEFAULTLOCALPORT 7654 //bind local port so it can be easy to debug
#define BUFFSIZE 1024
static int sizepersec=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 clientsocket;
static struct sockaddr_in serverAddr;
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 recvdata(void *pvParameters)
{
int len=0;
char buffrev[BUFFSIZE];
unsigned int socklen;
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(DEFAULTPORT);
serverAddr.sin_addr.s_addr = inet_addr(DEFAULTSEVER);
printf("recvdata!\n");
socklen=sizeof(serverAddr);
if(sendto(clientsocket, "hello", 6, 0,(struct sockaddr *)&serverAddr,socklen)<0)
{
perror("sendto hello:");
}
memset(buffrev,0,sizeof(buffrev));
while(1)
{
len=recvfrom(clientsocket, buffrev, BUFFSIZE, 0,(struct sockaddr *)&serverAddr,&socklen);
//printf("len= %d\n",len);
if(len>0)
{
sizepersec+=len;
//printf("%s\n",buffrev);
}
else if(len<0)
{
perror("recvfrom:\n");
vTaskDelay(100/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
clientsocket = socket(AF_INET, SOCK_DGRAM, 0);
if(clientsocket < 0)
{
printf("socket failed!\n");
vTaskDelete(NULL);
}
struct sockaddr_in myaddr;
myaddr.sin_family=AF_INET;
myaddr.sin_port=htons(DEFAULTLOCALPORT);
if(bind(clientsocket,(struct sockaddr *)&myaddr,sizeof(myaddr))<0)
{
printf("bind local port error!\n");
vTaskDelete(NULL);
}
vTaskDelay(2000/portTICK_RATE_MS);
//create a new task...
xTaskCreate(&recvdata,"recvdata",4096,NULL,5,&tasksend);
while(1)
{
sizepersec=0;
vTaskDelay(3000/ portTICK_RATE_MS);
printf("udp recv %d byte per sec!\n",sizepersec/3);
}
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,8 @@
#
# Main component makefile.
#
# This Makefile can be left empty. By default, it will take the sources in the
# src/ directory, compile them and link them into lib(subdirectory_name).a
# in the build directory. This behaviour is entirely configurable,
# please read the ESP-IDF documents if you need to do this.
#

View file

@ -0,0 +1,9 @@
#
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
# project subdirectory.
#
PROJECT_NAME := UDP_send
include $(IDF_PATH)/make/project.mk

View file

@ -0,0 +1,173 @@
/*
UDP_Client send
This Demo use esp32 as station,
when esp32 start,it will connect to an AP and will create a UDP socket,
and send data constantly.
*/
#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"
/*AP info and tcp_sever info*/
//#define DEFAULTSSID "wifi-12"
//#define DEFAULTPWD "sumof1+1=2"
#define DEFAULTSSID "tp_wifi_test1"
#define DEFAULTPWD "1234567890"
#define DEFAULTPORT 4567
#define DEFAULTSEVER "192.168.0.102"
#define DEFAULTLOCALPORT 7654 //bind local port so it can be easy to debug
#define BUFFSIZE 1024
static int sizepersec=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 clientsocket;
static struct sockaddr_in serverAddr;
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 senddata(void *pvParameters)
{
int len=0;
char buffsend[BUFFSIZE];
unsigned int socklen;
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(DEFAULTPORT);
serverAddr.sin_addr.s_addr = inet_addr(DEFAULTSEVER);
printf("senddata!\n");
socklen=sizeof(serverAddr);
if(sendto(clientsocket, "hello", 6, 0,(struct sockaddr *)&serverAddr,socklen)<0)
{
perror("sendto hello:");
}
memset(buffsend,97,sizeof(buffsend));
while(1)
{
len=sendto(clientsocket, buffsend, sizeof(buffsend), 0,(struct sockaddr *)&serverAddr,socklen);
//printf("len= %d\n",len);
if(len>0)
{
sizepersec+=len;
//printf("%s\n",buffrev);
}
else if(len<0)//the error code will be "not enough space" if send data constantly.
{
//perror("sendto:\n");
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
clientsocket = socket(AF_INET, SOCK_DGRAM, 0);
if(clientsocket < 0)
{
printf("socket failed!\n");
vTaskDelete(NULL);
}
struct sockaddr_in myaddr;
myaddr.sin_family=AF_INET;
myaddr.sin_port=htons(DEFAULTLOCALPORT);
if(bind(clientsocket,(struct sockaddr *)&myaddr,sizeof(myaddr))<0)
{
printf("bind local port error!\n");
vTaskDelete(NULL);
}
vTaskDelay(3000/portTICK_RATE_MS);
//create a new task...
xTaskCreate(&senddata,"senddata",4096,NULL,5,&tasksend);
while(1)
{
sizepersec=0;
vTaskDelay(3000/ portTICK_RATE_MS);
printf("udp send %d byte per sec!\n",sizepersec/3);
}
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,8 @@
#
# Main component makefile.
#
# This Makefile can be left empty. By default, it will take the sources in the
# src/ directory, compile them and link them into lib(subdirectory_name).a
# in the build directory. This behaviour is entirely configurable,
# please read the ESP-IDF documents if you need to do this.
#