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

resinba
Posts: 3
Joined: Sun Jan 01, 2017 1:51 pm

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

Postby resinba » Thu Jan 26, 2017 1:52 pm

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

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

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

Postby kolban » Thu Jan 26, 2017 3:49 pm

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
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

resinba
Posts: 3
Joined: Sun Jan 01, 2017 1:51 pm

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

Postby resinba » Fri Jan 27, 2017 2:37 pm

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

ESP_igrr
Posts: 2067
Joined: Tue Dec 01, 2015 8:37 am

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

Postby ESP_igrr » Sat Jan 28, 2017 3:54 am

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)

resinba
Posts: 3
Joined: Sun Jan 01, 2017 1:51 pm

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

Postby resinba » Sat Jan 28, 2017 12:31 pm

Thanks everyone for the help, it was this kind of information that I needed to realize how it works.

Who is online

Users browsing this forum: No registered users and 264 guests