主要是这个函数 ESP_ERROR_CHECK(example_connect());比较耗时,大概4s。
完整代码如下:
void app_main(void)
{
{
// 配置输出引脚3
// zero-initialize the config structure.
gpio_config_t io_conf = {};
// disable interrupt
io_conf.intr_type = GPIO_INTR_DISABLE;
// set as output mode
io_conf.mode = GPIO_MODE_OUTPUT;
// bit mask of the pins that you want to set,e.g.21
io_conf.pin_bit_mask = 1ULL << BLINK_LED_PIN;
// disable pull-down mode
io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
// disable pull-up mode
io_conf.pull_up_en = GPIO_PULLUP_ENABLE;
// configure GPIO with the given settings
gpio_config(&io_conf);
}
//通过串口发送数据,来观察系统运行状态
{
gpio_set_level(BLINK_LED_PIN, 1);
vTaskDelay(BLINK_LED_BLINK / portTICK_PERIOD_MS);
gpio_set_level(BLINK_LED_PIN, 0);
// vTaskDelay(BLINK_LED_BLINK / portTICK_PERIOD_MS);
}
ESP_ERROR_CHECK(nvs_flash_init());
ESP_LOGE(TAG, "nvs_flash_init over ...\n");
//通过串口发送数据,来观察系统运行状态
{
gpio_set_level(BLINK_LED_PIN, 1);
vTaskDelay(BLINK_LED_BLINK / portTICK_PERIOD_MS);
gpio_set_level(BLINK_LED_PIN, 0);
// vTaskDelay(BLINK_LED_BLINK / portTICK_PERIOD_MS);
}
ESP_ERROR_CHECK(esp_netif_init());
ESP_LOGE(TAG, "esp_netif_init over...\n");
//通过串口发送数据,来观察系统运行状态
{
gpio_set_level(BLINK_LED_PIN, 1);
vTaskDelay(BLINK_LED_BLINK / portTICK_PERIOD_MS);
gpio_set_level(BLINK_LED_PIN, 0);
// vTaskDelay(BLINK_LED_BLINK / portTICK_PERIOD_MS);
}
ESP_ERROR_CHECK(esp_event_loop_create_default());
ESP_LOGE(TAG, "esp_event_loop_create_default over...\n");
//通过串口发送数据,来观察系统运行状态
{
gpio_set_level(BLINK_LED_PIN, 1);
vTaskDelay(BLINK_LED_BLINK / portTICK_PERIOD_MS);
gpio_set_level(BLINK_LED_PIN, 0);//串口开始拉低
// vTaskDelay(BLINK_LED_BLINK / portTICK_PERIOD_MS);
}
/* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
* Read "Establishing Wi-Fi or Ethernet Connection" section in
* examples/protocols/README.md for more information about this function.
*/
ESP_ERROR_CHECK(example_connect());
ESP_LOGE(TAG, "example_connect over...\n");
//通过串口发送数据,来观察系统运行状态,通过串口观察,串口为0的状态保持了4.2s,这个函数为什么会消耗这么长时间,如何降低?
{
gpio_set_level(BLINK_LED_PIN, 1);
vTaskDelay(BLINK_LED_BLINK / portTICK_PERIOD_MS);
gpio_set_level(BLINK_LED_PIN, 0);
// vTaskDelay(BLINK_LED_BLINK / portTICK_PERIOD_MS);
}
xTaskCreate(udp_client_task, "udp_client", 4096, NULL, 2, NULL);
ESP_LOGE(TAG, "create udp_client_task over...\n");
// xTaskCreate(rx_task, "uart_rx_task", 1024*2, NULL, 1, NULL);
// gpio_set_level(BLINK_LED_PIN, 1);
// vTaskDelay(BLINK_LED_BLINK / portTICK_PERIOD_MS);
// gpio_set_level(BLINK_LED_PIN, 0);
}
static void udp_client_task(void *pvParameters)
{
char rx_buffer[128];
char host_ip[] = HOST_IP_ADDR;
int addr_family = 0;
int ip_protocol = 0;
while (1)
{
#if defined(CONFIG_EXAMPLE_IPV4)
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;
#elif defined(CONFIG_EXAMPLE_IPV6)
struct sockaddr_in6 dest_addr = {0};
inet6_aton(HOST_IP_ADDR, &dest_addr.sin6_addr);
dest_addr.sin6_family = AF_INET6;
dest_addr.sin6_port = htons(PORT);
dest_addr.sin6_scope_id = esp_netif_get_netif_impl_index(EXAMPLE_INTERFACE);
addr_family = AF_INET6;
ip_protocol = IPPROTO_IPV6;
#elif defined(CONFIG_EXAMPLE_SOCKET_IP_INPUT_STDIN)
struct sockaddr_storage dest_addr = {0};
ESP_ERROR_CHECK(get_addr_from_stdin(PORT, SOCK_DGRAM, &ip_protocol, &addr_family, &dest_addr));
#endif
int sock = socket(addr_family, SOCK_DGRAM, ip_protocol);
if (sock < 0)
{
ESP_LOGE(TAG, "Unable to create socket: errno %d", errno);
break;
}
// Set timeout
struct timeval timeout;
timeout.tv_sec = 10;
timeout.tv_usec = 0;
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof timeout);
// ESP_LOGI(TAG, "Socket created, sending to %s:%d", HOST_IP_ADDR, PORT);
int err = 0;
while (1)
{
// send init message
// ESP_LOGE(TAG, "the time before sending: %s", esp_log_system_timestamp());
{
gpio_set_level(BLINK_LED_PIN, 1);
vTaskDelay(BLINK_LED_BLINK / portTICK_PERIOD_MS);
gpio_set_level(BLINK_LED_PIN, 0);
}
err = sendto(sock, payload, strlen(payload), 0, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
if (err < 0)
{
ESP_LOGE(TAG, "Error occurred during sending: errno %d", errno);
break;
}
// ESP_LOGE(TAG, "the time after sending: %s", esp_log_system_timestamp());
ESP_LOGI(TAG, "Message sent");
// GPIO
// gpio_set_level(LED_GPIO, 0);
gpio_set_level(BLINK_LED_PIN, 1);
vTaskDelay(BLINK_LED_BLINK / portTICK_PERIOD_MS);
gpio_set_level(BLINK_LED_PIN, 0);
vTaskDelay(BLINK_LED_BLINK / portTICK_PERIOD_MS);
ESP_LOGE(TAG, "in task ....\n");
vTaskDelay(2000 / portTICK_PERIOD_MS);
}
// finish sending
if (sock != -1)
{
ESP_LOGE(TAG, "Shutting down socket and restarting...");
shutdown(sock, 0);
close(sock);
}
}
vTaskDelete(NULL);
}
ESP32 UDP_CLIENT 连接wifi达到4s,如何减少?
Jump to
- English Forum
- Explore
- News
- General Discussion
- FAQ
- Documentation
- Documentation
- Sample Code
- Discussion Forum
- Hardware
- ESP-IDF
- ESP-BOX
- ESP-ADF
- ESP-MDF
- ESP-WHO
- ESP-SkaiNet
- ESP32 Arduino
- IDEs for ESP-IDF
- ESP-AT
- ESP IoT Solution
- ESP RainMaker
- Rust
- ESP8266
- Report Bugs
- Showcase
- Chinese Forum 中文社区
- 活动区
- 乐鑫活动专区
- 讨论区
- 全国大学生物联网设计竞赛乐鑫答疑专区
- ESP-IDF 中文讨论版
- 《ESP32-C3 物联网工程开发实战》书籍讨论版
- 中文文档讨论版
- ESP-AT 中文讨论版
- ESP-BOX 中文讨论版
- ESP IoT Solution 中文讨论版
- ESP-ADF 中文讨论版
- ESP Mesh 中文讨论版
- ESP Cloud 中文讨论版
- ESP-WHO 中文讨论版
- ESP-SkaiNet 中文讨论版
- ESP 生产支持讨论版
- 硬件问题讨论
- 项目展示
Who is online
Users browsing this forum: No registered users and 31 guests
- All times are UTC
- Top
- Delete cookies
About Us
Espressif Systems is a fabless semiconductor company providing cutting-edge low power WiFi SoCs and wireless solutions for wireless communications and Internet of Things applications. ESP8266EX and ESP32 are some of our products.