From 4de5b7b30dc6d2e79b6020024d9a5985d38a31a7 Mon Sep 17 00:00:00 2001 From: Bob Date: Thu, 4 Dec 2025 12:10:33 -0800 Subject: [PATCH] more on bandwidth --- components/wifi_cfg/wifi_cfg.c | 61 +++++++++++++++++++++++++--------- components/wifi_cfg/wifi_cfg.h | 15 +++++---- main/main.c | 28 +++++++++++++++- 3 files changed, 82 insertions(+), 22 deletions(-) diff --git a/components/wifi_cfg/wifi_cfg.c b/components/wifi_cfg/wifi_cfg.c index e59256a..1a3ad96 100644 --- a/components/wifi_cfg/wifi_cfg.c +++ b/components/wifi_cfg/wifi_cfg.c @@ -136,11 +136,22 @@ esp_err_t wifi_ensure_inited(void) static void apply_ip_static(const char* ip, const char* mask, const char* gw){ if (!sta_netif) return; + + // Validate IP strings are not empty + if (!ip || !ip[0] || !mask || !mask[0] || !gw || !gw[0]) { + ESP_LOGW(TAG, "Invalid static IP config: IP=%s MASK=%s GW=%s", + ip ? ip : "NULL", mask ? mask : "NULL", gw ? gw : "NULL"); + return; + } + esp_netif_ip_info_t info = {0}; esp_netif_dhcpc_stop(sta_netif); info.ip.addr = esp_ip4addr_aton(ip); info.netmask.addr = esp_ip4addr_aton(mask); info.gw.addr = esp_ip4addr_aton(gw); + + ESP_LOGI(TAG, "Setting static IP: %s, netmask: %s, gateway: %s", ip, mask, gw); + ESP_ERROR_CHECK( esp_netif_set_ip_info(sta_netif, &info) ); } @@ -148,8 +159,6 @@ 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"); @@ -160,11 +169,8 @@ bool wifi_cfg_apply_from_nvs(void) { ESP_LOGW(TAG, "SSID in NVS is empty; treating as no Wi-Fi config"); return false; } - ESP_LOGI(TAG, "Applying Wi-Fi config: SSID=%s DHCP=%d IP=%s Band=%s BW=%s PowerSave=%s", - ssid, dhcp, ip, band, bw, powersave); - - ESP_LOGI(TAG, "Applying Wi‑Fi config: SSID=%s DHCP=%d IP=%s Band=%s BW=%s PowerSave=%s", - ssid, dhcp, ip, band, bw, powersave); + ESP_LOGI(TAG, "Applying Wi-Fi config: SSID=%s DHCP=%d IP=%s MASK=%s GW=%s Band=%s BW=%s PowerSave=%s", + ssid, dhcp, ip, mask, gw, band, bw, powersave); static bool inited = false; if (!inited){ @@ -249,7 +255,7 @@ bool wifi_cfg_apply_from_nvs(void) { if (sta_netif) esp_netif_dhcpc_start(sta_netif); } - // Set bandwidth + // Set bandwidth BEFORE WiFi is started #if CONFIG_IDF_TARGET_ESP32C5 || CONFIG_IDF_TARGET_ESP32C6 // Dual-band chips use struct-based API wifi_bandwidths_t bandwidths = { @@ -270,9 +276,9 @@ bool wifi_cfg_apply_from_nvs(void) { ESP_LOGI(TAG, "Setting bandwidth to HT20 (20MHz) for both bands"); } - err2 = esp_wifi_set_bandwidths(WIFI_IF_STA, &bandwidths); - if (err2 != ESP_OK) { - ESP_LOGW(TAG, "Failed to set bandwidths: %s", esp_err_to_name(err2)); + esp_err_t bw_err = esp_wifi_set_bandwidths(WIFI_IF_STA, &bandwidths); + if (bw_err != ESP_OK) { + ESP_LOGW(TAG, "Failed to set bandwidths: %s", esp_err_to_name(bw_err)); } #else // Single-band chips (2.4GHz only) use legacy API @@ -288,13 +294,13 @@ bool wifi_cfg_apply_from_nvs(void) { ESP_LOGI(TAG, "Setting bandwidth to HT20 (20MHz) for 2.4GHz"); } - err2 = esp_wifi_set_bandwidth(WIFI_IF_STA, bandwidth); - if (err2 != ESP_OK) { - ESP_LOGW(TAG, "Failed to set bandwidth: %s", esp_err_to_name(err2)); + esp_err_t bw_err = esp_wifi_set_bandwidth(WIFI_IF_STA, bandwidth); + if (bw_err != ESP_OK) { + ESP_LOGW(TAG, "Failed to set bandwidth: %s", esp_err_to_name(bw_err)); } #endif - err2 = esp_wifi_start(); + esp_err_t err2 = esp_wifi_start(); if (err2 != ESP_OK && err2 != ESP_ERR_INVALID_STATE) { ESP_ERROR_CHECK(err2); } // Set power save mode based on configuration @@ -479,3 +485,28 @@ wifi_ps_type_t wifi_cfg_get_power_save_mode(void) { return WIFI_PS_NONE; } } + +bool wifi_cfg_get_bandwidth(char *buf, size_t buf_size) { + if (!buf || buf_size < 1) { + return false; + } + + nvs_handle_t h; + if (nvs_open("netcfg", NVS_READONLY, &h) != ESP_OK) { + strncpy(buf, "Unknown", buf_size - 1); + buf[buf_size - 1] = '\0'; + return false; + } + + size_t len = buf_size; + esp_err_t err = nvs_get_str(h, "bw", buf, &len); + nvs_close(h); + + if (err != ESP_OK) { + strncpy(buf, "HT20", buf_size - 1); // Default + buf[buf_size - 1] = '\0'; + return false; + } + + return true; +} diff --git a/components/wifi_cfg/wifi_cfg.h b/components/wifi_cfg/wifi_cfg.h index 7308bef..af649a8 100644 --- a/components/wifi_cfg/wifi_cfg.h +++ b/components/wifi_cfg/wifi_cfg.h @@ -8,12 +8,6 @@ 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 * @@ -43,6 +37,15 @@ void wifi_cfg_force_dhcp(bool enable); */ wifi_ps_type_t wifi_cfg_get_power_save_mode(void); +/** + * @brief Get the configured bandwidth from NVS + * + * @param buf Buffer to store bandwidth string (e.g., "HT20", "HT40", "VHT80") + * @param buf_size Size of buffer + * @return true if bandwidth was retrieved, false otherwise + */ +bool wifi_cfg_get_bandwidth(char *buf, size_t buf_size); + /** * @brief Ensure WiFi driver is initialized (thread-safe, idempotent) * diff --git a/main/main.c b/main/main.c index ea0627f..2cb6473 100644 --- a/main/main.c +++ b/main/main.c @@ -329,7 +329,33 @@ static void event_handler(void* arg, esp_event_base_t event_base, 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, " Bandwidth: %s", bw_str); + + // Get configured bandwidth from NVS + char configured_bw[16] = {0}; + wifi_cfg_get_bandwidth(configured_bw, sizeof(configured_bw)); + + // Show both configured and requested bandwidth, look for wifi_connect in logs to get negotiated + if (bw_detected) { + ESP_LOGI(TAG, " Bandwidth: %s (requested) - NVS configured: %s", bw_str, configured_bw); + + // Warn if mismatch + bool mismatch = false; + if (strcmp(configured_bw, "VHT80") == 0 && bw != WIFI_BW80) { + mismatch = true; + } else if (strcmp(configured_bw, "HT40") == 0 && bw != WIFI_BW_HT40) { + mismatch = true; + } else if (strcmp(configured_bw, "HT20") == 0 && bw != WIFI_BW_HT20) { + mismatch = true; + } + + if (mismatch) { + ESP_LOGW(TAG, " ⚠ Bandwidth mismatch! Configured %s but negotiated %s", configured_bw, bw_str); + ESP_LOGW(TAG, " Check: router channel width setting, channel selection, RF interference"); + } + } else { + ESP_LOGI(TAG, " Bandwidth: Unknown (configured: %s)", configured_bw); + } + ESP_LOGI(TAG, " Channel: %d", ap_info.primary); ESP_LOGI(TAG, " RSSI: %d dBm", ap_info.rssi);