Page 1 of 1

nvs_get_str(my_handle, "server_name", NULL, &required_size);

Posted: Thu Jan 26, 2017 1:52 pm
by resinba
I there,
I have a problem with the "nvs.h" library. I can't understanding the mining of "my_hadle".

I need to save two strings with 50 to 100 bytes each, and read them.
I'm trying to understanding how...

"""
size_t required_size;
nvs_get_str((my_handle, "server_name", NULL, &required_size);
char* server_name = malloc(required_size);
nvs_get_str((my_handle, "server_name", server_name, &required_size);
"""
works, can some one explain to me

thanks

Re: nvs_get_str(my_handle, "server_name", NULL, &required_size);

Posted: Thu Jan 26, 2017 3:49 pm
by kolban
When you want to save a value in NVS storage ... you save it as a name/value pair. For example:

name: password
value: passw0rd

I think that is pretty clear. However, imagine you have multiple libraries that are linked together to build an application. If these libraries need to read and write NVS, they could easily "step on each other". For example, if I have a library called "webserver" that saves "password" key for access to the webserver and another library called "WiFi" that saves the network password ... then we are in trouble because these two independently written libraries don't know about each other and then collide on the name.

This is where a "namespace" comes into play. A namespace is a "logical" grouping of name/value pairs. When we write libraries, we can allocate to that library a unique/distinct namespace and then within that namespace, the library can be assured that there will be no collisions. For example, the webserver library might have a namespace of "webserver" while the WiFi might have a namespace of "WiFi". What this means is that a name/value pair doesn't exist by itself, they are further qualified by a namespace. So now the data becomes:

namspace: webserver
name: password
value: passw0rd

Because it is common to work with one namespace at a time, rather than continually having to supply the namespace on each NVS API call, we have an API called "nvs_open" that opens/creates a named namespace and then returns a handle. That handle is then supplied to further API calls and the name space associated with the handle (as supplied to nvs_open) is then used.

See also:
http://esp-idf.readthedocs.io/en/latest ... flash.html

Re: nvs_get_str(my_handle, "server_name", NULL, &required_size);

Posted: Fri Jan 27, 2017 2:37 pm
by resinba
Is there any example regards this particular topic?
I'm starting with esp32, and is language.
I used to use "Arduino C", for arduino family. Now I'm trying too test the ESP32, but panic is my middle name..
:( :(

Thanks

Re: nvs_get_str(my_handle, "server_name", NULL, &required_size);

Posted: Sat Jan 28, 2017 3:54 am
by ESP_igrr
There are two examples, both are mentioned in the documentation page which Mr. Kolban has mentioned above.

https://github.com/espressif/esp-idf/tr ... s_rw_value

https://github.com/espressif/esp-idf/tr ... vs_rw_blob

(That's inside examples/storage directory of your ESP-IDF copy)

Re: nvs_get_str(my_handle, "server_name", NULL, &required_size);

Posted: Sat Jan 28, 2017 12:31 pm
by resinba
Thanks everyone for the help, it was this kind of information that I needed to realize how it works.