From 64b3443085ccffea5d3301506b79f1c2a6050959 Mon Sep 17 00:00:00 2001 From: Bob Date: Wed, 3 Dec 2025 16:09:12 -0800 Subject: [PATCH] more on 5G and 80MHz --- components/wifi_cfg/wifi_cfg.c | 24 ++++++++++---- components/wifi_cfg/wifi_cfg.h | 6 ++++ main/main.c | 60 ++++++++++++++++++++++++++++++---- 3 files changed, 78 insertions(+), 12 deletions(-) diff --git a/components/wifi_cfg/wifi_cfg.c b/components/wifi_cfg/wifi_cfg.c index 25aba97..e59256a 100644 --- a/components/wifi_cfg/wifi_cfg.c +++ b/components/wifi_cfg/wifi_cfg.c @@ -4,6 +4,8 @@ // - USB path uses driver API (usb_serial_jtag_read_bytes / write_bytes) — works with /dev/ttyACM* // - Tolerates ESP_ERR_INVALID_STATE on repeated inits // - Supports DHCP or static IP, persists to NVS, applies immediately +// - Bandwidth options: HT20 (20MHz), HT40 (40MHz), VHT80 (80MHz, 5GHz only) +// - Power save modes: NONE (default, best for CSI), MIN/MIN_MODEM, MAX/MAX_MODEM #include #include @@ -146,6 +148,8 @@ bool wifi_cfg_apply_from_nvs(void) { char ssid[64]={0}, pass[64]={0}, ip[32]={0}, mask[32]={0}, gw[32]={0}; char band[16]={0}, bw[16]={0}, powersave[16]={0}; bool dhcp = true; + esp_err_t err2; + if (!load_cfg(ssid,sizeof(ssid), pass,sizeof(pass), ip,sizeof(ip), mask,sizeof(mask), gw,sizeof(gw), band,sizeof(band), bw,sizeof(bw), powersave,sizeof(powersave), &dhcp)){ ESP_LOGW(TAG, "No Wi‑Fi config in NVS"); @@ -245,10 +249,7 @@ bool wifi_cfg_apply_from_nvs(void) { if (sta_netif) esp_netif_dhcpc_start(sta_netif); } - esp_err_t err2 = esp_wifi_start(); - if (err2 != ESP_OK && err2 != ESP_ERR_INVALID_STATE) { ESP_ERROR_CHECK(err2); } - - // Set bandwidth AFTER WiFi is started but BEFORE connect + // Set bandwidth #if CONFIG_IDF_TARGET_ESP32C5 || CONFIG_IDF_TARGET_ESP32C6 // Dual-band chips use struct-based API wifi_bandwidths_t bandwidths = { @@ -256,7 +257,12 @@ bool wifi_cfg_apply_from_nvs(void) { .ghz_5g = WIFI_BW_HT20 }; - if (strcmp(bw, "HT40") == 0) { + if (strcmp(bw, "VHT80") == 0) { + // 80MHz only supported on 5GHz + bandwidths.ghz_2g = WIFI_BW_HT40; // 2.4GHz fallback to 40MHz + bandwidths.ghz_5g = WIFI_BW80; // Use WIFI_BW80, not WIFI_BW_HT80 + ESP_LOGI(TAG, "Setting bandwidth to VHT80 (80MHz) for 5GHz, HT40 (40MHz) for 2.4GHz"); + } else if (strcmp(bw, "HT40") == 0) { bandwidths.ghz_2g = WIFI_BW_HT40; bandwidths.ghz_5g = WIFI_BW_HT40; ESP_LOGI(TAG, "Setting bandwidth to HT40 (40MHz) for both bands"); @@ -272,7 +278,10 @@ bool wifi_cfg_apply_from_nvs(void) { // Single-band chips (2.4GHz only) use legacy API wifi_bandwidth_t bandwidth = WIFI_BW_HT20; - if (strcmp(bw, "HT40") == 0) { + if (strcmp(bw, "VHT80") == 0) { + ESP_LOGW(TAG, "VHT80 (80MHz) not supported on 2.4GHz-only chips - using HT40 (40MHz)"); + bandwidth = WIFI_BW_HT40; + } else if (strcmp(bw, "HT40") == 0) { bandwidth = WIFI_BW_HT40; ESP_LOGI(TAG, "Setting bandwidth to HT40 (40MHz) for 2.4GHz"); } else { @@ -285,6 +294,9 @@ bool wifi_cfg_apply_from_nvs(void) { } #endif + err2 = esp_wifi_start(); + if (err2 != ESP_OK && err2 != ESP_ERR_INVALID_STATE) { ESP_ERROR_CHECK(err2); } + // Set power save mode based on configuration wifi_ps_type_t ps_mode = WIFI_PS_NONE; // Default: no power save (best for CSI) diff --git a/components/wifi_cfg/wifi_cfg.h b/components/wifi_cfg/wifi_cfg.h index e848850..7308bef 100644 --- a/components/wifi_cfg/wifi_cfg.h +++ b/components/wifi_cfg/wifi_cfg.h @@ -8,6 +8,12 @@ extern "C" { #endif +// Bandwidth constants - Define WIFI_BW_HT80 if not already defined by ESP-IDF +// ESP-IDF defines WIFI_BW_HT20 (0) and WIFI_BW_HT40 (1), but WIFI_BW_HT80 (2) may not exist yet +#ifndef WIFI_BW_HT80 +#define WIFI_BW_HT80 WIFI_BW80 // 80MHz bandwidth (VHT80) for ESP32-C5/C6 on 5GHz +#endif + /** * @brief Initialize the WiFi configuration system * diff --git a/main/main.c b/main/main.c index 2e68c5e..11c968b 100644 --- a/main/main.c +++ b/main/main.c @@ -265,12 +265,13 @@ static void event_handler(void* arg, esp_event_base_t event_base, wifi_connected = true; current_led_state = LED_STATE_CONNECTED; - // Log connection details: SSID, band, channel, RSSI + // Log connection details: SSID, band, channel, bandwidth, RSSI wifi_config_t wifi_cfg; wifi_ap_record_t ap_info; if (esp_wifi_get_config(WIFI_IF_STA, &wifi_cfg) == ESP_OK && esp_wifi_sta_get_ap_info(&ap_info) == ESP_OK) { + // Determine band from channel const char *band_str = "Unknown"; if (ap_info.primary >= 1 && ap_info.primary <= 14) { band_str = "2.4GHz"; @@ -278,13 +279,60 @@ static void event_handler(void* arg, esp_event_base_t event_base, band_str = "5GHz"; } + // Get bandwidth - try dual-band API first, fallback to single-band + wifi_bandwidth_t bw = WIFI_BW_HT20; // Default to 20MHz + const char *bw_str = "Unknown"; + bool bw_detected = false; + + // Try esp_wifi_get_bandwidths() first (works on dual-band chips in auto mode) + wifi_bandwidths_t bandwidths = {0}; + esp_err_t err = esp_wifi_get_bandwidths(WIFI_IF_STA, &bandwidths); + + if (err == ESP_OK) { + // Dual-band API succeeded - select bandwidth based on current band + if (ap_info.primary >= 1 && ap_info.primary <= 14) { + // 2.4GHz band + bw = (wifi_bandwidth_t)bandwidths.ghz_2g; + bw_detected = true; + } else if (ap_info.primary >= 36) { + // 5GHz band + bw = (wifi_bandwidth_t)bandwidths.ghz_5g; + bw_detected = true; + } + } else { + // Dual-band API failed - try single-band API (ESP32-S3, older IDF) + err = esp_wifi_get_bandwidth(WIFI_IF_STA, &bw); + if (err == ESP_OK) { + bw_detected = true; + } + } + + // Convert bandwidth enum to string + if (bw_detected) { + switch (bw) { + case WIFI_BW_HT20: + bw_str = "20MHz (HT20)"; + break; + case WIFI_BW_HT40: + bw_str = "40MHz (HT40)"; + break; + case WIFI_BW80: + bw_str = "80MHz (VHT80)"; + break; + default: + bw_str = "Unknown"; + break; + } + } + ESP_LOGI(TAG, "========================================"); ESP_LOGI(TAG, "WiFi CONNECTED - BLUE LED solid"); - ESP_LOGI(TAG, " SSID: '%s'", wifi_cfg.sta.ssid); - ESP_LOGI(TAG, " Band: %s", band_str); - ESP_LOGI(TAG, " Channel: %d", ap_info.primary); - ESP_LOGI(TAG, " RSSI: %d dBm", ap_info.rssi); - ESP_LOGI(TAG, " BSSID: %02x:%02x:%02x:%02x:%02x:%02x", + ESP_LOGI(TAG, " SSID: '%s'", wifi_cfg.sta.ssid); + ESP_LOGI(TAG, " Band: %s", band_str); + ESP_LOGI(TAG, " Bandwidth: %s", bw_str); + ESP_LOGI(TAG, " Channel: %d", ap_info.primary); + ESP_LOGI(TAG, " RSSI: %d dBm", ap_info.rssi); + ESP_LOGI(TAG, " BSSID: %02x:%02x:%02x:%02x:%02x:%02x", ap_info.bssid[0], ap_info.bssid[1], ap_info.bssid[2], ap_info.bssid[3], ap_info.bssid[4], ap_info.bssid[5]); ESP_LOGI(TAG, "========================================");