From a623e4e1e6d207d65da8744b666a8685ac110a75 Mon Sep 17 00:00:00 2001 From: Bob Date: Mon, 10 Nov 2025 09:52:45 -0800 Subject: [PATCH] more on serial setting of wifi and IP --- main/wifi_cfg.c | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/main/wifi_cfg.c b/main/wifi_cfg.c index 50b6d65..c20e0bf 100644 --- a/main/wifi_cfg.c +++ b/main/wifi_cfg.c @@ -1,9 +1,12 @@ + #include #include #include #include + #include "freertos/FreeRTOS.h" #include "freertos/task.h" + #include "esp_system.h" #include "esp_log.h" #include "nvs_flash.h" @@ -11,16 +14,20 @@ #include "esp_netif.h" #include "esp_wifi.h" #include "esp_event.h" -#include "driver/uart.h" -#include "esp_vfs_dev.h" + #include "driver/usb_serial_jtag.h" +#include "esp_vfs_dev.h" + +#include "wifi_cfg.h" static const char *TAG = "wifi_cfg"; static void trim(char *s){ int n = strlen(s); while(n>0 && (s[n-1]=='\r' || s[n-1]=='\n' || isspace((unsigned char)s[n-1]))) s[--n]=0; - while(*s && isspace((unsigned char)*s)) memmove(s, s+1, strlen(s)); + while(*s && isspace((unsigned char)*s)){ + memmove(s, s+1, strlen(s)); + } } static esp_err_t nvs_set_str2(nvs_handle_t h, const char *key, const char *val){ @@ -28,6 +35,7 @@ static esp_err_t nvs_set_str2(nvs_handle_t h, const char *key, const char *val){ } static bool cfg_dhcp = true; +static esp_netif_t *sta_netif = NULL; static void save_cfg(const char* ssid, const char* pass, const char* ip, const char* mask, const char* gw, bool dhcp){ nvs_handle_t h; @@ -43,7 +51,8 @@ static void save_cfg(const char* ssid, const char* pass, const char* ip, const c cfg_dhcp = dhcp; } -static bool load_cfg(char* ssid, size_t ssz, char* pass, size_t psz, char* ip, size_t isz, char* mask, size_t msz, char* gw, size_t gsz, bool* dhcp){ +static bool load_cfg(char* ssid, size_t ssz, char* pass, size_t psz, + char* ip, size_t isz, char* mask, size_t msz, char* gw, size_t gsz, bool* dhcp){ nvs_handle_t h; if (nvs_open("netcfg", NVS_READONLY, &h) != ESP_OK) return false; size_t len; @@ -62,15 +71,13 @@ static bool load_cfg(char* ssid, size_t ssz, char* pass, size_t psz, char* ip, s void wifi_cfg_force_dhcp(bool enable){ cfg_dhcp = enable; } -static esp_netif_t *sta_netif = NULL; - static void apply_ip_static(const char* ip, const char* mask, const char* gw){ if (!sta_netif) 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); + info.gw.addr = esp_ip4addr_aton(gw); ESP_ERROR_CHECK( esp_netif_set_ip_info(sta_netif, &info) ); } @@ -78,10 +85,10 @@ bool wifi_cfg_apply_from_nvs(void){ char ssid[64]={0}, pass[64]={0}, ip[32]={0}, mask[32]={0}, gw[32]={0}; bool dhcp = true; if (!load_cfg(ssid,sizeof(ssid), pass,sizeof(pass), ip,sizeof(ip), mask,sizeof(mask), gw,sizeof(gw), &dhcp)){ - ESP_LOGW(TAG, "No Wi‑Fi config in NVS"); + ESP_LOGW(TAG, "No Wi-Fi config in NVS"); return false; } - ESP_LOGI(TAG, "Applying Wi‑Fi config: SSID=%s DHCP=%d IP=%s", ssid, dhcp, ip); + ESP_LOGI(TAG, "Applying Wi-Fi config: SSID=%s DHCP=%d IP=%s", ssid, dhcp, ip); static bool inited = false; if (!inited){ @@ -114,16 +121,14 @@ bool wifi_cfg_apply_from_nvs(void){ } static void cfg_listener_task(void *arg){ - // Install low-level USB-Serial/JTAG driver usb_serial_jtag_driver_config_t dcfg = USB_SERIAL_JTAG_DRIVER_CONFIG_DEFAULT(); ESP_ERROR_CHECK(usb_serial_jtag_driver_install(&dcfg)); - // Bind stdio to that driver (deprecated name, but available on IDF v5.3.1) + // Deprecated but functional - causes only a warning esp_vfs_usb_serial_jtag_use_driver(); - // Now stdio is safe to use setvbuf(stdin, NULL, _IONBF, 0); - setvbuf(stdout, NULL, _IONBF, 0); + setvbuf(stdout, NULL, _IONBF, 0); char line[160]; char ssid[64]={0}, pass[64]={0}, ip[32]={0}, mask[32]={0}, gw[32]={0}; @@ -135,7 +140,6 @@ static void cfg_listener_task(void *arg){ vTaskDelay(pdMS_TO_TICKS(50)); continue; } - // ... rest unchanged ... trim(line); if (!in_cfg){ if (strcmp(line, "CFG")==0){ @@ -163,5 +167,6 @@ static void cfg_listener_task(void *arg){ void wifi_cfg_init(void){ nvs_flash_init(); - xTaskCreatePinnedToCore(cfg_listener_task, "cfg_listener", 6144, NULL, 5, NULL, tskNO_AFFINITY); + xTaskCreatePinnedToCore(cfg_listener_task, "cfg_listener", + 6144, NULL, 5, NULL, tskNO_AFFINITY); }