PlatformIO Community

Problem with Preferences

First off. is there a way to use Serial print to show the current state of the Preferences…
i.e. what keys and their values… assuming one doesn’t know what may have been saved before… assuming we do not know what keys were used.

Second is there a way to remove all the preferences including namespace keys and values without erasing the entire flash?

Then comes the bug I have…
This is the terminal output:

EEMem Read Global Door Info
Key: DOOR001
[E][Preferences.cpp:504] getBytes(): not enough space in buffer: 4 < 68
EEMem Read Global Door Info
Key: DOOR002
[E][Preferences.cpp:504] getBytes(): not enough space in buffer: 4 < 68
EEMem Read Global Door Info
Key: DOOR003
[E][Preferences.cpp:504] getBytes(): not enough space in buffer: 4 < 68
EEMem Read Global Door Info
Key: DOOR004
[E][Preferences.cpp:504] getBytes(): not enough space in buffer: 4 < 68
EEMem Read Global Door Info

Here is the code:

    uint8_t i;

    struct DOOR_INFO info;
    uint8_t infoasbytes[4];
    char key[10] = {'D','O','O','R',0,0,0,0,0,0};

    if(doorNumber>0 && doorNumber <= EEpromGetGlobalDoorN())
    {


    Serial.println("EEMem Read Global Door Info");

    preferences.begin("MYAPP",false);
    byte_to_text (doorNumber,  &key[4] );  // make key string
    Serial.print("Key: "); 
    Serial.println (key) ; 
    preferences.getBytes (key ,infoasbytes, 4 ) ; //4 Bytes specific to selected door
    preferences.end();

    info.columnLinkAddress = infoasbytes[0];
    info.door = infoasbytes[1];
    info.columnIndexFromLeft = infoasbytes[2];
    info.rowIndexFromTop  = infoasbytes[3];

    }

I can’t see where the ‘not enough space…’ error comes from. In Preferences.cpp the same text is all in caps.
I only want to read 4 bytes, so don’t know what the 4 < 68 means as regards the error.

The code you’ve shown doesn’t even show the datatype of the preferences variable of how it’s implemented, so we can’t say anything about that.

See above

Aha with some googling around it seems the crucucial info you’ve omitted is that you’re on an ESP32 and you’re using the built-in Preferences library (https://github.com/espressif/arduino-esp32/tree/master/libraries/Preferences) is that correct?

Yes… ESP32, built in preferences. Thanks My BAd

The Preferences class does not directly expose this functionality. However, since all that the preferences class does it use the ESP-IDF NVS (non-volatile-storage) functions, you can do the same. And there are functions to iterate through all available keys, see

https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/storage/nvs_flash.html#_CPPv414nvs_entry_findPKcPKc10nvs_type_t

From my understandign the .clear() function already exposes this functionality, only erasing all NVS entries from the namespace (that was given in the .begin() call)? See code).

It seems you’ve saved a 68 byte big object under that key but attempt to retrieve it again using a 4 byte buffer. That won’t work. The error message comes from

Review the buffer and buffer size for retrieving the object again or double-check the size of what you write in that key.

Thankyou very much Max :slight_smile:

A further question (occasional coder )

When I attempt to include the code to iterate as per example the symbols are not found ( underscored )

image

When I include

#include <nvs.h>
#include <nvs_flash.h>

The headers are found but do not resolve the issue
However when I include

#include <ArduinoNvs.h>

The issues are resolved ( The underscores vanish, so presumably the symbols were found) but it fails to compile with error
src\startup.cpp:12:24: fatal error: ArduinoNvs.h: No such file or directory

I would ask what / how / why / is going on and how to resolve.
Thanks

Oh my bad, actually the

does not apply because that’s for the latest ESP-IDF. PlatformIO is using Arduino-ESP32 version 1.0.6 which uses 3.3.5 (source), and that ESP-IDF version does not have these functions.

So until PlatformIO updates to Arduino-ESP32 v2.0.0 which is based on a very recent ESP-IDF v4.4 version (https://github.com/platformio/platform-espressif32/issues/619), you can’t officially use these functions in PlatformIO.

The issue describes a way to get a “tech preview” for Arduino-ESP32 via the platformio.ini

[env:esp32]
board = esp32dev
framework = arduino
platform  = https://github.com/platformio/platform-espressif32.git#feature/arduino-idf-master
platform_packages =
   framework-arduinoespressif32 @ https://github.com/espressif/arduino-esp32.git#2.0.0

which makes the code example I refered to earlier compile successfully within PlatformIO.

grafik

1 Like