how to use esp32.NVS?

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
jcolo
Posts: 8
Joined: Tue Dec 15, 2020 11:50 am

how to use esp32.NVS?

Post by jcolo » Tue Apr 06, 2021 4:42 pm

Hi,

I have a standard v1.14 mycropython installation on a spressif dev kit C.

reading the documentation I do not see any specific prerequisites to use this functionality, however dir (esp32) is not listing NVS.

I can't see a way to access this functionality.

Can somebody point me to the right direction ?

Many Thanks

JC

marcidy
Posts: 133
Joined: Sat Dec 12, 2020 11:07 pm

Re: how to use esp32.NVS?

Post by marcidy » Fri Apr 09, 2021 7:07 pm

It's a key value store partitioned into namespaces accessed via labels.

Code: Select all

>>> nvs_area = NVS('label')
>>> nvs_area.set_i32('key1', 100)
>>> nvs_are.set_blob('key2', b"binary data")
>>> nvs_area.commit()
>>> machine.reset()
>>> nvs_area = NVS('label')
>>> nvs_area.get_i32('key1')
100
>>> x = bytearray(len(b"binary_data"))
>>> nvs_area.get_blob('key2', x)
11
>>> x
b"binary data"
To use it effectively for binary data, you'd need to store the length of a blob in an i32.

Code: Select all

>>> nvs_area.set_i32('key_len', len(binary_data))
>>> nvs_area.set_blob('key', binary_data)
>>> nvs_area.commit()
>>> retrieved = bytearray(nvs_area.get_i32('key_len'))
>>> nvs_area.get_blob('key', retrieved)
I assume this is using the 'nvs' partition, and I'm not sure there's really a use case for it for general data given there's plenty of flash available with higher level abstractions for storing data on flash.

e.g. using btree is easier, or a json file, etc.

jcolo
Posts: 8
Joined: Tue Dec 15, 2020 11:50 am

Re: how to use esp32.NVS?

Post by jcolo » Wed Apr 14, 2021 3:07 pm

in my system...."module object has no attribute 'NVS'"

with 1.14
import esp32

nvs = esp32.NVS("label")

simply fails.


Which system/version are you using ??

marcidy
Posts: 133
Joined: Sat Dec 12, 2020 11:07 pm

Re: how to use esp32.NVS?

Post by marcidy » Wed Apr 14, 2021 8:19 pm

it's in later commits, available if you compile yourself. indicates it will be in future releases, but no guarantee.

jcolo
Posts: 8
Joined: Tue Dec 15, 2020 11:50 am

Re: how to use esp32.NVS?

Post by jcolo » Thu Apr 15, 2021 4:19 pm

I guess it will be on 1.15 soon to be released.

Thanks for clarifying.

Post Reply