[ESP] ESP32开机自动配网运行iperf

发布时间 2023-07-30 11:03:19作者: 空水
  • esp-idf版本
    • tag-V4.4.2
  • 示例路径
    • examples/wifi/iperf

iperf_example_main.c(修改后的版本)

/* Wi-Fi iperf 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.
*/

#include <iperf.h>
#include <errno.h>
#include <string.h>
#include <sys/unistd.h>
#include "esp_wifi.h"
#include "esp_log.h"
#include "esp_err.h"
#include "nvs_flash.h"
#include "esp_console.h"
#include "cmd_decl.h"

void app_main(void) {
    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);

    initialise_wifi();

    esp_console_repl_t *repl = NULL;
    esp_console_repl_config_t repl_config = ESP_CONSOLE_REPL_CONFIG_DEFAULT();
    repl_config.prompt = "iperf>";

    // init console REPL environment
#if CONFIG_ESP_CONSOLE_UART
    esp_console_dev_uart_config_t uart_config = ESP_CONSOLE_DEV_UART_CONFIG_DEFAULT();
    ESP_ERROR_CHECK(esp_console_new_repl_uart(&uart_config, &repl_config, &repl));
#elif CONFIG_ESP_CONSOLE_USB_CDC
    esp_console_dev_usb_cdc_config_t cdc_config = ESP_CONSOLE_DEV_CDC_CONFIG_DEFAULT();
    ESP_ERROR_CHECK(esp_console_new_repl_usb_cdc(&cdc_config, &repl_config, &repl));
#elif CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG
    esp_console_dev_usb_serial_jtag_config_t usbjtag_config = ESP_CONSOLE_DEV_USB_SERIAL_JTAG_CONFIG_DEFAULT();
    ESP_ERROR_CHECK(esp_console_new_repl_usb_serial_jtag(&usbjtag_config, &repl_config, &repl));
#endif

    /* Register commands */
    register_system();
    register_wifi();

    printf("\n ==================================================\n");
    printf(" |       Steps to test WiFi throughput            |\n");
    printf(" |                                                |\n");
    printf(" |  1. Print 'help' to gain overview of commands  |\n");
    printf(" |  2. Configure device to station or soft-AP     |\n");
    printf(" |  3. Setup WiFi connection                      |\n");
    printf(" |  4. Run iperf to test UDP/TCP RX/TX throughput |\n");
    printf(" |                                                |\n");
    printf(" =================================================\n\n");

    /*
     * .sta.ssid    : wifi ssid name
     * .sta.password: wifi password
     */
    wifi_config_t wifi_config = {.sta.ssid = "sharkrobot",
            .sta.password = "xxxxxxx"};

    esp_wifi_set_mode(WIFI_MODE_STA);
    esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config);
    esp_wifi_connect();

    ESP_LOGI(__func__ ,"Start wifi connect");
    // start console REPL
    ESP_ERROR_CHECK(esp_console_start_repl(repl));
    ESP_LOGI(__func__ ,"esp_console_start_repl");
    sleep(10);
    ESP_LOGI(__func__ ,"Start iperf_start 1");

    iperf_cfg_t iperf = {.type = IPERF_IP_TYPE_IPV4};
    iperf.flag |= IPERF_FLAG_SERVER;
//    iperf.source_ip4 = wifi_get_local_ip();
    iperf.flag |= IPERF_FLAG_SERVER;
    iperf.len_send_buf = 0;
    iperf.sport = IPERF_DEFAULT_PORT;
    iperf.dport = IPERF_DEFAULT_PORT;
    iperf.interval = 1;
    iperf.time = IPERF_DEFAULT_TIME;
    iperf.bw_lim = IPERF_DEFAULT_NO_BW_LIMIT;

    iperf_start(&iperf);
    ESP_LOGI(__func__ ,"Start iperf_start 2");

}

测试效果

  • 以下ESP32作为iperf的服务端,笔记本作为客户端。

1. 示例esp32开机自动连接的wifi ssid是sharkrobot所以笔记本也需要连接到相同的wifi ssid。

2. ESP32上电,等待5秒。

3. 终端显示连接到sharkrobot,同时复制ESP32的IP地址。

4. 终端显示iperf启动成功。

5. 在笔记本上启动iperf和ESP32上的ESP32通信。

  1. ESP32终端显示。
  2. 笔记本终端显示。