ESP32-WROOM-32D 和电脑连接到同一个Wifi路由器上,ping命令平均返回时间222ms,测试结果是不是有问题?

ningzb
Posts: 4
Joined: Wed Aug 21, 2019 2:31 am

ESP32-WROOM-32D 和电脑连接到同一个Wifi路由器上,ping命令平均返回时间222ms,测试结果是不是有问题?

Postby ningzb » Wed Sep 18, 2019 8:09 am

ESP32-WROOM-32D 和电脑连接到同一个Wifi路由器上,ping命令测试
发送1683,最短2ms,最长2119ms,平均222ms,
测试结果是不是有问题?
有人测试过么?

程序代码如下:
main.c

/* WiFi sta+ap


*/
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include "nvs_flash.h"

#include "lwip/err.h"
#include "lwip/sys.h"


#define SET_REMOTE_WIFI_SSID CONFIG_REMOTE_WIFI_SSID
#define SET_REMOTE_WIFI_PASS CONFIG_REMOTE_WIFI_PASSWORD
#define SET_ESP_MAXIMUM_RETRY CONFIG_ESP_MAXIMUM_RETRY


#define SET_LOCAL_WIFI_SSID CONFIG_LOCAL_WIFI_SSID
#define SET_LOCAL_WIFI_PASS CONFIG_LOCAL_WIFI_PASSWORD


/* FreeRTOS event group to signal when we are connected*/
EventGroupHandle_t s_wifi_event_group;

/* The event group allows multiple bits for each event, but we only care about one event
* - are we connected to the AP with an IP? */
const int WIFI_CONNECTED_BIT = BIT0;

static const char *TAG = "wifiApStaMain";

static int s_retry_num = 0;

static void event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
esp_wifi_connect();
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
if (s_retry_num < SET_ESP_MAXIMUM_RETRY) {
esp_wifi_connect();
xEventGroupClearBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
s_retry_num++;
ESP_LOGI(TAG, "retry to connect to the AP");
}
ESP_LOGI(TAG,"connect to the AP fail");
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
ESP_LOGI(TAG, "got ip:%s",
ip4addr_ntoa(&event->ip_info.ip));
s_retry_num = 0;
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
} else if (event_id == WIFI_EVENT_AP_STACONNECTED) {
wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*) event_data;
ESP_LOGI(TAG, "station "MACSTR" join, AID=%d",
MAC2STR(event->mac), event->aid);
} else if (event_id == WIFI_EVENT_AP_STADISCONNECTED) {
wifi_event_ap_stadisconnected_t* event = (wifi_event_ap_stadisconnected_t*) event_data;
ESP_LOGI(TAG, "station "MACSTR" leave, AID=%d",
MAC2STR(event->mac), event->aid);
}
}

void wifi_init(void)
{
s_wifi_event_group = xEventGroupCreate();

tcpip_adapter_init();

ESP_ERROR_CHECK(esp_event_loop_create_default());

wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));

ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL));

wifi_config_t wifi_config_sta = {
.sta = {
.ssid = SET_REMOTE_WIFI_SSID,
.password = SET_REMOTE_WIFI_PASS
},

};

wifi_config_t wifi_config_ap = {

.ap = {
.ssid = SET_LOCAL_WIFI_SSID,
.ssid_len = strlen(SET_LOCAL_WIFI_SSID),
.password = SET_LOCAL_WIFI_PASS,
.max_connection = SET_ESP_MAXIMUM_RETRY,
.authmode = WIFI_AUTH_WPA_WPA2_PSK
},
};

if (strlen(SET_LOCAL_WIFI_PASS) == 0) {
wifi_config_ap.ap.authmode = WIFI_AUTH_OPEN;
}
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA) ); //ap+ata模式
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config_sta) );
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config_ap) );

//esp_err_t tcpip_adapter_set_ip_info(tcpip_adapter_if_t tcpip_if, const tcpip_adapter_ip_info_t *ip_info);

tcpip_adapter_ip_info_t ipInfo;

ipInfo.ip.addr=ipaddr_addr("192.168.5.2");
ipInfo.netmask.addr=ipaddr_addr("255.255.255.0");
ipInfo.gw.addr=ipaddr_addr("192.168.5.1");


ESP_ERROR_CHECK(tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP) );
ESP_ERROR_CHECK(tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_AP,&ipInfo) );

ESP_ERROR_CHECK(esp_wifi_start() );

ESP_LOGI(TAG, "sta connect to remote ap SSID:%s password:%s",SET_REMOTE_WIFI_SSID, SET_REMOTE_WIFI_SSID);

ESP_LOGI(TAG, "wifi_init_softap finished. SSID:%s password:%s",SET_LOCAL_WIFI_SSID, SET_LOCAL_WIFI_PASS);

}



extern void tcp_server_init(void);
extern void tcp_client_init(void);

void app_main(void)
{
//Initialize NVS
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);

ESP_LOGI(TAG, "ESP_WIFI_MODE_APSTA 000");
wifi_init();

tcp_server_init();

tcp_client_init();




}




//tcpClinet.c

#include <string.h>

#include <sys/param.h>

#include "freertos/FreeRTOS.h"

#include "freertos/task.h"

#include "freertos/event_groups.h"

#include "esp_system.h"

#include "esp_wifi.h"

#include "esp_event.h"

#include "esp_log.h"

#include "nvs_flash.h"

#include "tcpip_adapter.h"

//#include "protocol_examples_common.h"



#include "lwip/err.h"

#include "lwip/sockets.h"

#include "lwip/sys.h"

#include <lwip/netdb.h>



#define HOST_IP_ADDR "192.168.212.168"

#define PORT 60000

static const char *TAG = "tcpClinet";

static const char *payload = "Message from ESP32 ";



static void tcp_client_task(void *pvParameters)

{

char rx_buffer[128];

char addr_str[128];

int addr_family;

int ip_protocol;



while (1) {

struct sockaddr_in dest_addr;

dest_addr.sin_addr.s_addr = inet_addr(HOST_IP_ADDR);

dest_addr.sin_family = AF_INET;

dest_addr.sin_port = htons(PORT);

addr_family = AF_INET;

ip_protocol = IPPROTO_IP;

inet_ntoa_r(dest_addr.sin_addr, addr_str, sizeof(addr_str) - 1);





int sock = socket(addr_family, SOCK_STREAM, ip_protocol);

if (sock < 0) {

ESP_LOGE(TAG, "Unable to create socket: errno %d", errno);

break;

}

ESP_LOGI(TAG, "Socket created, connecting to %s:%d", HOST_IP_ADDR, PORT);



int err = connect(sock, (struct sockaddr *)&dest_addr, sizeof(dest_addr));

if (err != 0) {

ESP_LOGE(TAG, "Socket unable to connect: errno %d", errno);

break;

}

ESP_LOGI(TAG, "Successfully connected");



while (1) {

int err = send(sock, payload, strlen(payload), 0);

if (err < 0) {

ESP_LOGE(TAG, "Error occurred during sending: errno %d", errno);

break;

}



int len = recv(sock, rx_buffer, sizeof(rx_buffer) - 1, 0);

// Error occurred during receiving

if (len < 0) {

ESP_LOGE(TAG, "recv failed: errno %d", errno);

break;

}

// Data received

else {

rx_buffer[len] = 0; // Null-terminate whatever we received and treat like a string

ESP_LOGI(TAG, "Received %d bytes from %s:", len, addr_str);

ESP_LOGI(TAG, "%s", rx_buffer);

}



vTaskDelay(2000 / portTICK_PERIOD_MS);

}



if (sock != -1) {

ESP_LOGE(TAG, "Shutting down client socket and restarting...");

shutdown(sock, 0);

close(sock);

}

}

vTaskDelete(NULL);

}





extern EventGroupHandle_t s_wifi_event_group;

extern const int WIFI_CONNECTED_BIT ;


void tcp_client_init(void)

{

const TickType_t xTicksToWait = 100 / portTICK_PERIOD_MS;

EventBits_t Bits;


while (1)
{

Bits=xEventGroupWaitBits(s_wifi_event_group, WIFI_CONNECTED_BIT,pdFALSE,pdTRUE,xTicksToWait );

if( (Bits&WIFI_CONNECTED_BIT)==WIFI_CONNECTED_BIT){

break;

}

}

xTaskCreate(tcp_client_task, "tcp_client", 4096, NULL, 5, NULL);

}


///////tcpServer.c



#include <string.h>

#include <sys/param.h>

#include "freertos/FreeRTOS.h"

#include "freertos/task.h"

#include "esp_system.h"

#include "esp_wifi.h"

#include "esp_event.h"

#include "esp_log.h"

#include "nvs_flash.h"

#include "tcpip_adapter.h"

//#include "protocol_examples_common.h"



#include "lwip/err.h"

#include "lwip/sockets.h"

#include "lwip/sys.h"

#include <lwip/netdb.h>





#define PORT 60000



static const char *TAG = "tcpServer";





static void tcp_server_task(void *pvParameters)

{

char rx_buffer[128];

char addr_str[128];

int addr_family;

int ip_protocol;



while (1) {



#ifdef CONFIG_EXAMPLE_IPV4

struct sockaddr_in dest_addr;

dest_addr.sin_addr.s_addr = htonl(INADDR_ANY);

dest_addr.sin_family = AF_INET;

dest_addr.sin_port = htons(PORT);

addr_family = AF_INET;

ip_protocol = IPPROTO_IP;

inet_ntoa_r(dest_addr.sin_addr, addr_str, sizeof(addr_str) - 1);

#else // IPV6

struct sockaddr_in6 dest_addr;

bzero(&dest_addr.sin6_addr.un, sizeof(dest_addr.sin6_addr.un));

dest_addr.sin6_family = AF_INET6;

dest_addr.sin6_port = htons(PORT);

addr_family = AF_INET6;

ip_protocol = IPPROTO_IPV6;

inet6_ntoa_r(dest_addr.sin6_addr, addr_str, sizeof(addr_str) - 1);

#endif



int listen_sock = socket(addr_family, SOCK_STREAM, ip_protocol);

if (listen_sock < 0) {

ESP_LOGE(TAG, "Unable to create socket: errno %d", errno);

break;

}

ESP_LOGI(TAG, "Socket created");



int err = bind(listen_sock, (struct sockaddr *)&dest_addr, sizeof(dest_addr));

if (err != 0) {

ESP_LOGE(TAG, "Socket unable to bind: errno %d", errno);

break;

}

ESP_LOGI(TAG, "Socket bound, port %d", PORT);



err = listen(listen_sock, 1);

if (err != 0) {

ESP_LOGE(TAG, "Error occurred during listen: errno %d", errno);

break;

}

ESP_LOGI(TAG, "Socket listening");



struct sockaddr_in6 source_addr; // Large enough for both IPv4 or IPv6

uint addr_len = sizeof(source_addr);

int sock = accept(listen_sock, (struct sockaddr *)&source_addr, &addr_len);

if (sock < 0) {

ESP_LOGE(TAG, "Unable to accept connection: errno %d", errno);

break;

}

ESP_LOGI(TAG, "Socket accepted");



while (1) {

int len = recv(sock, rx_buffer, sizeof(rx_buffer) - 1, 0);

// Error occurred during receiving

if (len < 0) {

ESP_LOGE(TAG, "recv failed: errno %d", errno);

break;

}

// Connection closed

else if (len == 0) {

ESP_LOGI(TAG, "Connection closed");

break;

}

// Data received

else {

// Get the sender's ip address as string

if (source_addr.sin6_family == PF_INET) {

inet_ntoa_r(((struct sockaddr_in *)&source_addr)->sin_addr.s_addr, addr_str, sizeof(addr_str) - 1);

} else if (source_addr.sin6_family == PF_INET6) {

inet6_ntoa_r(source_addr.sin6_addr, addr_str, sizeof(addr_str) - 1);

}



rx_buffer[len] = 0; // Null-terminate whatever we received and treat like a string

ESP_LOGI(TAG, "Received %d bytes from %s:", len, addr_str);

ESP_LOGI(TAG, "%s", rx_buffer);



int err = send(sock, rx_buffer, len, 0);

if (err < 0) {

ESP_LOGE(TAG, "Error occurred during sending: errno %d", errno);

break;

}

}

}



if (sock != -1) {

ESP_LOGE(TAG, "Shutting down Server socket and restarting...");

shutdown(sock, 0);

close(sock);
}
}

vTaskDelete(NULL);
}



void tcp_server_init(void)
{
xTaskCreate(tcp_server_task, "tcp_server", 4096, NULL, 5, NULL);
}



串口返回日志如下:


rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:6440
load:0x40078000,len:11368
load:0x40080400,len:6636
entry 0x40080768
I (28) boot: ESP-IDF v4.1-dev-58-g02c7c3885 2nd stage bootloader
I (28) boot: compile time 17:18:57
I (28) boot: Enabling RNG early entropy source...
I (34) boot: SPI Speed : 40MHz
I (38) boot: SPI Mode : DIO
I (42) boot: SPI Flash Size : 2MB
I (46) boot: Partition Table:
I (50) boot: ## Label Usage Type ST Offset Length
I (57) boot: 0 nvs WiFi data 01 02 00009000 00006000
I (65) boot: 1 phy_init RF data 01 01 0000f000 00001000
I (72) boot: 2 factory factory app 00 00 00010000 00100000
I (80) boot: End of partition table
I (84) esp_image: segment 0: paddr=0x00010020 vaddr=0x3f400020 size=0x156d0 ( 87760) map
I (124) esp_image: segment 1: paddr=0x000256f8 vaddr=0x3ffb0000 size=0x031f8 ( 12792) load
I (129) esp_image: segment 2: paddr=0x000288f8 vaddr=0x40080000 size=0x00400 ( 1024) load
0x40080000: _WindowOverflow4 at /home/work/esp/esp-idf/components/freertos/xtensa_vectors.S:1778

I (131) esp_image: segment 3: paddr=0x00028d00 vaddr=0x40080400 size=0x07310 ( 29456) load
I (152) esp_image: segment 4: paddr=0x00030018 vaddr=0x400d0018 size=0x6d088 (446600) map
0x400d0018: _stext at ??:?

I (312) esp_image: segment 5: paddr=0x0009d0a8 vaddr=0x40087710 size=0x08ffc ( 36860) load
0x40087710: ppCalTxAMPDULength at ??:?

I (338) boot: Loaded app from partition at offset 0x10000
I (338) boot: Disabling RNG early entropy source...
I (339) cpu_start: Pro cpu up.
I (342) cpu_start: Application information:
I (347) cpu_start: Project name: wifi_station_softAP
I (353) cpu_start: App version: 1
I (358) cpu_start: Compile time: Sep 16 2019 15:55:13
I (364) cpu_start: ELF file SHA256: 2031555a16607440...
I (370) cpu_start: ESP-IDF: v4.1-dev-58-g02c7c3885-dirty
I (376) cpu_start: Starting app cpu, entry point is 0x4008128c
0x4008128c: call_start_cpu1 at /home/work/esp/esp-idf/components/esp32/cpu_start.c:281

I (0) cpu_start: App cpu up.
I (387) heap_init: Initializing. RAM available for dynamic allocation:
I (394) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (400) heap_init: At 3FFB90E0 len 00026F20 (155 KiB): DRAM
I (406) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (412) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (419) heap_init: At 4009070C len 0000F8F4 (62 KiB): IRAM
I (425) cpu_start: Pro cpu start user code
I (443) spi_flash: detected chip: generic
I (444) spi_flash: flash io: dio
W (444) spi_flash: Detected size(4096k) larger than the size in the binary image header(2048k). Using the size in the binary image header.
I (454) cpu_start: Chip Revision: 1
W (459) cpu_start: Chip revision is higher than the one configured in menuconfig. Suggest to upgrade it.
I (469) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
I (538) wifiApStaMain: ESP_WIFI_MODE_APSTA 000
I (548) wifi: wifi driver task: 3ffc0b68, prio:23, stack:3584, core=0
I (548) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
I (548) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
I (578) wifi: wifi firmware version: 6236ba1
I (578) wifi: config NVS flash: enabled
I (578) wifi: config nano formating: disabled
I (578) wifi: Init dynamic tx buffer num: 32
I (578) wifi: Init data frame dynamic rx buffer num: 32
I (588) wifi: Init management frame dynamic rx buffer num: 32
I (588) wifi: Init management short buffer num: 32
I (598) wifi: Init static rx buffer size: 1600
I (598) wifi: Init static rx buffer num: 10
I (608) wifi: Init dynamic rx buffer num: 32
I (698) phy: phy_version: 4102, 2fa7a43, Jul 15 2019, 13:06:06, 0, 0
I (698) wifi: mode : sta (4c:11:ae:71:16:dc) + softAP (4c:11:ae:71:16:dd)
I (708) wifi: Total power save buffer number: 16
I (708) wifi: Init max length of beacon: 752/752
I (708) wifi: Init max length of beacon: 752/752
I (718) wifiApStaMain: sta connect to remote ap SSID:HLXX-yfzx password:HLXX-yfzx
I (728) wifiApStaMain: wifi_init_softap finished. SSID:ESP32-AP password:0000aaaa
I (728) tcpServer: Socket created
I (738) tcpServer: Socket bound, port 60000
I (738) tcpServer: Socket listening
I (838) wifi: ap channel adjust o:1,1 n:6,2
I (838) wifi: new:<6,0>, old:<1,0>, ap:<6,2>, sta:<6,0>, prof:1
I (838) wifi: state: init -> auth (b0)
I (848) wifi: state: auth -> assoc (0)
I (848) wifi: state: assoc -> run (10)
I (1058) wifi: connected with HLXX-yfzx, channel 6, BW20, bssid = f4:ec:38:66:61:c8
I (1058) wifi: pm start, type: 1

I (3038) tcpip_adapter: sta ip: 192.168.212.107, mask: 255.255.255.0, gw: 192.168.212.1
I (3038) wifiApStaMain: got ip:192.168.212.107
I (3038) tcpClinet: Socket created, connecting to 192.168.212.168:60000
I (4578) tcpClinet: Successfully connected
I (10108) tcpClinet: Received 0 bytes from 192.168.212.168:

Who is online

Users browsing this forum: Bing [Bot] and 47 guests