NVS Storage not retrieving strings, but ints work fine

frostyowned
Posts: 20
Joined: Wed Jun 28, 2017 1:22 pm

NVS Storage not retrieving strings, but ints work fine

Postby frostyowned » Thu Aug 03, 2017 6:49 pm

So I have code that saves json text so the esp32 can store collected data.

The problem is that although I can save it properly ( the print reads the exact string it should ) it can not load it, and gives me a blank space

Here is the saving code:

Code: Select all

        printf("wOS: Saving collected data into NVS...\n");
		const char* data = GetDataCollected();
		printf("%s\n", data ); //Prints the correct value
        err = nvs_set_str(storage_device, "DataCollected", data );
        printf((err != ESP_OK) ? "wOS: Failed to save Collected Data\n" : "wOS: Successfully saved Collected Data!\n");
Here is the loading code:

Code: Select all

		size_t required_size;
		err = nvs_get_str(storage_device, "DataCollected", NULL, &required_size );
        switch (err) {
            case ESP_OK:
                printf("wOS: Successfully grabbed size, loading in value..\n");
				char* SavedData = malloc(required_size);
				err = nvs_get_str( storage_device, "DataCollected", &SavedData, &required_size );
				printf( "Downloaded Data: %s\n", SavedData );
				ApplyLoadedData( &SavedData );
				int timr = 0;
				nvs_get_i32( storage_device, "CurrentTime", &timr );
				printf( "Timer: %d", timr );
				SetTimeKeeper( timr );
				printf("wOS: Successfully loaded data!\n");
                break;
            case ESP_ERR_NVS_NOT_FOUND:
                printf("wOS: There is no saved data on file.\n");
                break;
            default :
                printf("wOS: Error (%d) reading!\n", err);
        }		size_t required_size;
		err = nvs_get_str(storage_device, "DataCollected", NULL, &required_size );
        switch (err) {
            case ESP_OK:
                printf("wOS: Successfully grabbed size, loading in value..\n");
				char* SavedData = malloc(required_size);
				err = nvs_get_str( storage_device, "DataCollected", &SavedData, &required_size );
				printf( "Downloaded Data: %s\n", SavedData ); //Prints completely blank
				ApplyLoadedData( &SavedData );
				int timr = 0;
				nvs_get_i32( storage_device, "CurrentTime", &timr );
				printf( "Timer: %d", timr );
				SetTimeKeeper( timr );
				printf("wOS: Successfully loaded data!\n");
                break;
            case ESP_ERR_NVS_NOT_FOUND:
                printf("wOS: There is no saved data on file.\n");
                break;
            default :
                printf("wOS: Error (%d) reading!\n", err);
        }
The timer loads perfectly fine, it's just as soon as the string is attempted to be read that it breaks. Also, the size_required actually gives you the proper size of the string. Somehow it still gives a blank text

User avatar
martinayotte
Posts: 141
Joined: Fri Nov 13, 2015 4:27 pm

Re: NVS Storage not retrieving strings, but ints work fine

Postby martinayotte » Thu Aug 03, 2017 7:26 pm

You seems to pass the pointer of the pointer ...
Use that instead (ampersand removed) :

Code: Select all

err = nvs_get_str( storage_device, "DataCollected", SavedData, &required_size );

frostyowned
Posts: 20
Joined: Wed Jun 28, 2017 1:22 pm

Re: NVS Storage not retrieving strings, but ints work fine

Postby frostyowned » Thu Aug 03, 2017 9:18 pm

Oh wow, that's silly of me. Thank you!

Who is online

Users browsing this forum: No registered users and 117 guests