Wifi on ESP32 with credentials stored in non-volatile storage

For my first projects I placed the Wifi SSID and access key in the application source code like this:

static wifi_config_t wifi_config = { .sta = { .ssid = "SSID", .password = "passwd"}};
this isn't a long-term solution as I had to flash the application separately for every environment it was run in, and if you publish source code it's also a bit of a security issue....

I don't want to have to create a temporary Access point with a webserver to select SSID and capture credentials, because there are a limited set of networks my work connects to, but also, sometimes it needs to run in more than one location.

Placing a list of wifi networks I know about in NV storage separate to my source code or .bin image works, I can flash a 'standard' file to every project's non-volatile storage (NVS) partition allowing the application to find and connect to an appropriate wifi network where possible.

It is possible that having this

summarised as a video
might help.

Prepare NVS (create partition)

The ESP IDF creates an NVS partition as standard.

Create an empty application and flash the ESP32

esp32]# idf.py create-project wifi
esp32]# cd wifi
esp32/wifi]# idf.py set-target esp32c3
esp32/wifi]# idf.py -p /dev/ttyACM0 flash

We should now have a partition table, and an nvs partition we can write to:

esp32/wifi]# esptool.py -p /dev/ttyACM0 read_flash 0x8000 0xc00 partitions.img
esp32/wifi]# gen_esp32part.py partitions.img 
Parsing binary partition input...
Verifying table...
# ESP-IDF Partition Table
# Name, Type, SubType, Offset, Size, Flags
nvs,data,nvs,0x9000,24K,
phy_init,data,phy,0xf000,4K,
factory,app,factory,0x10000,1M,

Create a wifi.csv file and flash that to the nvs

wifi.csv should be stored separately from any specific application, I keep it in my esp32 projects folder.

esp32/wifi]# cd ..
esp32]# joe wifi.csv
key,type,encoding,value
wifi,namespace,,
SKYXXXXX,data,string,r5w9wokdcnkf
SKYXXXXX,data,string,yuXWfV8j89sd

Each time it's changed a new .bin file needs to be created

esp32]# nvs_partition_gen.py generate wifi.csv wifi.bin 0x3000

Creating NVS binary with version: V2 - Multipage Blob Support Enabled

Created NVS binary: ===> wifi.bin
esp32]# parttool.py -p /dev/ttyACM0 write_partition --partition-name=nvs --input "wifi.bin"
...
Written contents of file 'wifi.bin' at offset 0x9000
esp32]#

Application

Add .c code for the wifi functions and app_main

esp32]# cd wifi 
esp32/wifi]# joe ../wifi.lib.c
esp32/wifi]# joe main/wifi.c

Compile and flash this to the ESP32

esp32/wifi]# idf.py -p /dev/ttyACM0 flash monitor

It should start up and connect to an AP that is listsed in wifi.csv. From esp_wifi_scan_get_ap_records() "The returned AP list is sorted in descending order based on RSSI"

To better use this set up (of multiple network credentials), this code (wifi2.c) will re-scan for available networks and re-connect (possibly to a different) network in the event of a disconnection event.

 


last modified: 2025-09-13 22:01:08