more on serial setting of wifi and IP

This commit is contained in:
Bob 2025-11-10 09:52:45 -08:00
parent dec314ce6b
commit a623e4e1e6
1 changed files with 20 additions and 15 deletions

View File

@ -1,9 +1,12 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <ctype.h> #include <ctype.h>
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/task.h" #include "freertos/task.h"
#include "esp_system.h" #include "esp_system.h"
#include "esp_log.h" #include "esp_log.h"
#include "nvs_flash.h" #include "nvs_flash.h"
@ -11,16 +14,20 @@
#include "esp_netif.h" #include "esp_netif.h"
#include "esp_wifi.h" #include "esp_wifi.h"
#include "esp_event.h" #include "esp_event.h"
#include "driver/uart.h"
#include "esp_vfs_dev.h"
#include "driver/usb_serial_jtag.h" #include "driver/usb_serial_jtag.h"
#include "esp_vfs_dev.h"
#include "wifi_cfg.h"
static const char *TAG = "wifi_cfg"; static const char *TAG = "wifi_cfg";
static void trim(char *s){ static void trim(char *s){
int n = strlen(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(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){ 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 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){ 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; 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; 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; nvs_handle_t h;
if (nvs_open("netcfg", NVS_READONLY, &h) != ESP_OK) return false; if (nvs_open("netcfg", NVS_READONLY, &h) != ESP_OK) return false;
size_t len; size_t len;
@ -62,8 +71,6 @@ 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; } 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){ static void apply_ip_static(const char* ip, const char* mask, const char* gw){
if (!sta_netif) return; if (!sta_netif) return;
esp_netif_ip_info_t info = {0}; esp_netif_ip_info_t info = {0};
@ -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}; char ssid[64]={0}, pass[64]={0}, ip[32]={0}, mask[32]={0}, gw[32]={0};
bool dhcp = true; bool dhcp = true;
if (!load_cfg(ssid,sizeof(ssid), pass,sizeof(pass), ip,sizeof(ip), mask,sizeof(mask), gw,sizeof(gw), &dhcp)){ if (!load_cfg(ssid,sizeof(ssid), pass,sizeof(pass), ip,sizeof(ip), mask,sizeof(mask), gw,sizeof(gw), &dhcp)){
ESP_LOGW(TAG, "No WiFi config in NVS"); ESP_LOGW(TAG, "No Wi-Fi config in NVS");
return false; return false;
} }
ESP_LOGI(TAG, "Applying WiFi 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; static bool inited = false;
if (!inited){ if (!inited){
@ -114,14 +121,12 @@ bool wifi_cfg_apply_from_nvs(void){
} }
static void cfg_listener_task(void *arg){ 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(); usb_serial_jtag_driver_config_t dcfg = USB_SERIAL_JTAG_DRIVER_CONFIG_DEFAULT();
ESP_ERROR_CHECK(usb_serial_jtag_driver_install(&dcfg)); 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(); esp_vfs_usb_serial_jtag_use_driver();
// Now stdio is safe to use
setvbuf(stdin, NULL, _IONBF, 0); setvbuf(stdin, NULL, _IONBF, 0);
setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stdout, NULL, _IONBF, 0);
@ -135,7 +140,6 @@ static void cfg_listener_task(void *arg){
vTaskDelay(pdMS_TO_TICKS(50)); vTaskDelay(pdMS_TO_TICKS(50));
continue; continue;
} }
// ... rest unchanged ...
trim(line); trim(line);
if (!in_cfg){ if (!in_cfg){
if (strcmp(line, "CFG")==0){ if (strcmp(line, "CFG")==0){
@ -163,5 +167,6 @@ static void cfg_listener_task(void *arg){
void wifi_cfg_init(void){ void wifi_cfg_init(void){
nvs_flash_init(); 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);
} }