esp_wifi_set_storage(WIFI_STORAGE_FLASH)

zliudr
Posts: 360
Joined: Thu Oct 03, 2019 5:15 am

esp_wifi_set_storage(WIFI_STORAGE_FLASH)

Postby zliudr » Sat Mar 06, 2021 9:50 pm

In an esp-azure example there is a line that reads:

Code: Select all

 esp_wifi_set_storage(WIFI_STORAGE_RAM) 
I wonder how this affects WIFI. I read the function has a default value of FLASH. Does storing WIFI data in FLASH speed up connection? What data? Credentials? Thanks.

boarchuz
Posts: 606
Joined: Tue Aug 21, 2018 5:28 am

Re: esp_wifi_set_storage(WIFI_STORAGE_FLASH)

Postby boarchuz » Sun Mar 07, 2021 3:56 am

When you initialise WiFi with esp_wifi_init, there is a "nvs_enable" field (most of the time this is determined by CONFIG_ESP32_WIFI_NVS_ENABLED).

If enabled, the WiFi driver will restore a lot of data on initialisation and save as things change, including the current mode, STA config, and AP config.

You can change this later - without having to deinit/reinit with a different nvs_enable value - by using esp_wifi_set_storage, but it will have no effect if WiFi was originally initialised with nvs_enable==0.

It significantly speeds up connection times (a bit over my head but I think it does this by storing the crypto stuff, which can take a long time to compute).
I guess the examples typically set this to RAM so that, regardless of the config, nothing will be unexpectedly saved to flash.

zliudr
Posts: 360
Joined: Thu Oct 03, 2019 5:15 am

Re: esp_wifi_set_storage(WIFI_STORAGE_FLASH)

Postby zliudr » Sun Mar 07, 2021 7:24 am

boarchuz wrote:
Sun Mar 07, 2021 3:56 am
When you initialise WiFi with esp_wifi_init, there is a "nvs_enable" field (most of the time this is determined by CONFIG_ESP32_WIFI_NVS_ENABLED).

If enabled, the WiFi driver will restore a lot of data on initialisation and save as things change, including the current mode, STA config, and AP config.

You can change this later - without having to deinit/reinit with a different nvs_enable value - by using esp_wifi_set_storage, but it will have no effect if WiFi was originally initialised with nvs_enable==0.

It significantly speeds up connection times (a bit over my head but I think it does this by storing the crypto stuff, which can take a long time to compute).
I guess the examples typically set this to RAM so that, regardless of the config, nothing will be unexpectedly saved to flash.
Thanks. I'll enable flash and see if there's enough difference. So if I understand correctly, this information is stored as blobs etc. with the nvs_set_str() etc. It would be nice to know how much space this takes. I use NVS for some storage as well. Is there a function to return available space on nvs partition?

boarchuz
Posts: 606
Joined: Tue Aug 21, 2018 5:28 am

Re: esp_wifi_set_storage(WIFI_STORAGE_FLASH)

Postby boarchuz » Sun Mar 07, 2021 7:58 am

Yep, the NVS function addresses are given to WiFi via esp_wifi_init.

Mostly little things, and a "sta.apinfo" (700b) for a total of about 1kb.

Try nvs_get_stats

zliudr
Posts: 360
Joined: Thu Oct 03, 2019 5:15 am

Re: esp_wifi_set_storage(WIFI_STORAGE_FLASH)

Postby zliudr » Sun Mar 07, 2021 6:51 pm

Great! Thanks. I tried the FLASH option but didn't see any improvements. 4.5 seconds is what it takes to get connected to a router. I don't see change of NVS entry changes either maybe I only already used it for WiFi. I had the command after esp_wifi_init() and before esp_wifi_start().

RichardFalk
Posts: 2
Joined: Mon Oct 11, 2021 7:17 pm

Re: esp_wifi_set_storage(WIFI_STORAGE_FLASH)

Postby RichardFalk » Mon Oct 11, 2021 9:03 pm

The NVS key ap.sndchan get set on every esp_wifi_init but since the value isn't normally changing, it does not write to Flash (low-level Flash code reads the value and compares before doing a write or erasure).

However, the NVS keys sta.chan and sta.apinfo are set on every esp_wifi_connect and these values will change if you connect to a different AP in which case they will get written to Flash. This means that if you are frequently connecting and disconnecting Wi-Fi, such as with a battery-operated device doing Wi-Fi bursts every hour, then if the device is in between two APs with the same SSID (or if you are intentionally connecting to different SSIDs), it can alternate between them and cause frequent Flash writes. While NVS has some wear leveling, this is nevertheless inefficient.

So unless you intend to store/save your Wi-Fi credentials in NVS, it is probably better to just use RAM for Wi-Fi storage.

Note that it is better to not set the CONFIG_ESP32_WIFI_NVS_ENABLED configuration flag as that will also prevent the ap.sndchan from esp_wifi_init whereas esp_wifi_set_storage cannot be called before esp_wifi_init.

Who is online

Users browsing this forum: atx823, Bing [Bot], Google [Bot], Majestic-12 [Bot] and 236 guests