ESP32-C6 接收速率测试对比 ESP32-Pico

基于 PlatformIO 的 ESP32 开发环境 中用小米手机13做热点,测试了下从 PC 端上传单个大文件到 ESP32-Pico 上的速率大概是 140KB/s,这里对比同样的硬件环境。对比 ESP32-C6 的速率大概为 250KB/s,有不小的提升,但是距离我认为的起码上 M 还是有点差距。。。
【当然应该把软件也全部弄成一样才更加严谨,不过有时间再弄吧 XD】

编写测试代码

借助 GPT 帮忙把之前的 PC 端上传代码修改为 IDF,聊天转换过程没有什么难度,中间去除了打印日志带来的影响。因为目前 ESP32-C6 仅支持 espidf 的框架,如下所示。

修改默认串口波特率

因为 PlatformIO Monitor 默认的波特率为 9600 ,而代码默认不修改则为 115200,所以需要修改下 PlatformIO Monitor 串口的波特率。添加 monitor_speed = 115200platformio.ini 的最后一行即可。不然打印出来的是乱码。

; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter
;   Upload options: custom upload port, speed and extra flags
;   Library options: dependencies, extra library storages
;   Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:esp32-c6-devkitc-1]
platform = espressif32
board = esp32-c6-devkitc-1
framework = espidf
monitor_speed = 115200

修改板子信息

因为 C6 板子比较新,对 C6 的 Flash 信息不知道为啥是 2M,需要手动再修改下,打开状态栏的终端图标操作如下:

输入 pio run -t memuconfig

选择 Serial flasher config

选择 Flash size,首次新建工程后应该是 2M,而不是目前显示的 8M,不过最终都需要修改为 8M。

选择 8 MB

修改完退出后,可以看 sdkconfig 文件,会由原来的 2M 修改为 8M:

不然会有如下编译生成 elf 时报的错误:



编译显示如下错误:RAM:   [=         ]   6.1% (used 32156 bytes from 524288 bytes)
Flash: [========  ]  79.8% (used 836332 bytes from 1048576 bytes)
Building .pio\build\esp32-c6-devkitc-1\firmware.bin
esptool.py v4.5.1
Creating esp32c6 image...
Merged 2 ELF sections

A fatal error occurred: Contents of segment at SHA256 digest offset 0xb0 are not all zero. Refusing to overwrite.
*** [.pio\build\esp32-c6-devkitc-1\firmware.bin] Error 2
====================================================== [FAILED] Took 3.14 seconds ======================================================

注意:修改后如果编译还是报错,需要删除 .pio 缓存目录再编译,如果还不行,则重启 VS Code!!!

不同网络

结论:无法进入 5G 网络频段;小米手机热点还不错,有256KB/s;小米路由器6000 的 2.4G 频段拉跨,才不到150KB/s。。。

小米 AX6000 2.4G 频段的热点:

小米 13 手机的热点:

小米 AX6000 5G 频段的热点:一直在尝试连接

参考链接

测试代码

#include <string.h>
#include <sys/param.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "esp_http_server.h"
#include "esp_timer.h" // Include esp_timer.h to resolve esp_timer_get_time
#include <inttypes.h> // Include inttypes.h for format specifier macros

#define EXAMPLE_ESP_WIFI_SSID      "Xiaomi13"
#define EXAMPLE_ESP_WIFI_PASS      "1234567890"
#define EXAMPLE_MAX_STA_CONN       1

static const char *TAG = "speed_test_server";

/* Event handler for catching system events */
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) {
        esp_wifi_connect();
        ESP_LOGI(TAG, "retry to connect to the AP");
    } 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:" IPSTR, IP2STR(&event->ip_info.ip));
    }
}

/* Initialize Wi-Fi as sta and set scan method */
static void wifi_init_sta() {
    ESP_ERROR_CHECK(esp_netif_init());
    ESP_ERROR_CHECK(esp_event_loop_create_default());
    esp_netif_create_default_wifi_sta();

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

    esp_event_handler_instance_t instance_any_id;
    esp_event_handler_instance_t instance_got_ip;
    ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
                                                        ESP_EVENT_ANY_ID,
                                                        &event_handler,
                                                        NULL,
                                                        &instance_any_id));
    ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
                                                        IP_EVENT_STA_GOT_IP,
                                                        &event_handler,
                                                        NULL,
                                                        &instance_got_ip));

    wifi_config_t wifi_config = {
        .sta = {
            .ssid = EXAMPLE_ESP_WIFI_SSID,
            .password = EXAMPLE_ESP_WIFI_PASS,
            .threshold.authmode = WIFI_AUTH_WPA2_PSK,
            .pmf_cfg = {
                .capable = true,
                .required = false
            },
        },
    };
    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
    ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
    ESP_ERROR_CHECK(esp_wifi_start() );

    ESP_LOGI(TAG, "wifi_init_sta finished.");
}

/* HTTP POST 处理函数 */
static esp_err_t upload_handler(httpd_req_t *req) {
    char buf[100]; // 缓冲区大小可根据需求调整
    int ret;
    int remaining = req->content_len;

    static uint32_t totalBytesReceived = 0;
    static uint64_t downloadStart = 0;

    if (remaining == req->content_len) {
        downloadStart = esp_timer_get_time(); // 获取开始时间(微秒)
    }

    while (remaining > 0) {
        // 读取请求数据
        if ((ret = httpd_req_recv(req, buf, MIN(remaining, sizeof(buf)))) <= 0) {
            if (ret == HTTPD_SOCK_ERR_TIMEOUT) {
                // 如果超时,则重试接收
                continue;
            }
            return ESP_FAIL;
        }

        remaining -= ret;
        totalBytesReceived += ret;
    }

    if (remaining == 0) {
        uint64_t downloadEnd = esp_timer_get_time(); // 获取结束时间(微秒)
        double downloadTime = (downloadEnd - downloadStart) / 1e6; // 转换为秒
        double speed = totalBytesReceived / downloadTime; // 计算速度(字节/秒)

        // 打印下载速度
        ESP_LOGI(TAG, "Download speed: %.2f bytes/s", speed);

        // 重置计数器以便下一次测量
        totalBytesReceived = 0;
    }

    // 发送响应
    httpd_resp_send_chunk(req, NULL, 0);
    return ESP_OK;
}

static const httpd_uri_t upload = {
    .uri       = "/upload",
    .method    = HTTP_POST,
    .handler   = upload_handler,
    .user_ctx  = NULL
};

/* Function for starting the webserver */
static httpd_handle_t start_webserver(void) {
    httpd_config_t config = HTTPD_DEFAULT_CONFIG();
    httpd_handle_t server = NULL;

    // Start the httpd server
    ESP_LOGI(TAG, "Starting server on port: '%d'", config.server_port);
    if (httpd_start(&server, &config) == ESP_OK) {
        // Set URI handlers
        ESP_LOGI(TAG, "Registering URI handlers");
        httpd_register_uri_handler(server, &upload);
        return server;
    }

    ESP_LOGI(TAG, "Error starting server!");
    return NULL;
}

/* Function for stopping the webserver */
static void stop_webserver(httpd_handle_t server) {
    // Stop the httpd server
    httpd_stop(server);
}

void app_main(void) {
    // Initialize NVS
    ESP_ERROR_CHECK(nvs_flash_init());
    // Initialize Wi-Fi
    wifi_init_sta();
    // Start the webserver
    start_webserver();
}