Page 1 of 1
how to use esp32.NVS?
Posted: Tue Apr 06, 2021 4:42 pm
by jcolo
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
Re: how to use esp32.NVS?
Posted: Fri Apr 09, 2021 7:07 pm
by marcidy
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.
Re: how to use esp32.NVS?
Posted: Wed Apr 14, 2021 3:07 pm
by jcolo
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 ??
Re: how to use esp32.NVS?
Posted: Wed Apr 14, 2021 8:19 pm
by marcidy
it's in later commits, available if you compile yourself. indicates it will be in future releases, but no guarantee.
Re: how to use esp32.NVS?
Posted: Thu Apr 15, 2021 4:19 pm
by jcolo
I guess it will be on 1.15 soon to be released.
Thanks for clarifying.