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");

    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

    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 (arduino-esp32/libraries/Preferences at master · espressif/arduino-esp32 · GitHub) 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

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 )

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.

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 (Support for the latest Arduino v2.0 · Issue #619 · platformio/platform-espressif32 · GitHub), 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

board = esp32dev
framework = arduino
platform  =
platform_packages =
   framework-arduinoespressif32 @

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

1 Like