Volodymyr Shymanskyy Brings Espressif's ESP32 Preferences Library to the ESP8266, Particle Family
Built to be as compatible as possible with the original, this library brings the ESP32's Preferences functionality to a broader audience.
Ukrainian developer Volodymyr Shymanskyy, co-founder of the Blynk IoT platform, has released an open-source Preferences library for the Espressif ESP8266 and third-generation Particle devices — offering an application programming interface (API) largely equivalent to Espressif's original ESP32 Preferences library for ease of project porting and development.
"The Preferences library is unique to arduino-esp32," Espressif wrote of the original Preferences library, in a statement which is now not quite as correct as it was at the start of this week. "It should be considered as the replacement for the Arduino EEPROM library. It uses a portion of the on-board non-volatile memory (NVS) of the ESP32 to store data. This data is retained across restarts and loss of power events to the system."
Designed primarily for storing and retrieving many small values, rather than large data chunks, the Preferences library is now available for ESP8266 and third-generation Particle devices — the Argon, Boron, Xenon, Tracker, and BSOM — albeit in unofficial form, and with a couple of caveats.
Shymanskyy's version of the library aims to offer an API roughly equivalent with Espressif's original, making it easier to port applications that use the library from the ESP32 and for developers with experience of the library to pick up the new version. Preferences defined in code are stored in the devices' internal flash filesystem using either LittleFS or SPIFFS, with automatic handling of bad sectors, flash wear leveling, and an atomic rename file operation.
It's not a direct equivalent to the ESP32 version, however: The partition_label
argument is not supported in begin()
, Shymanskyy notes, and the getType()
and freeEntries()
methods are not supported — returning dummy values rather than halting the program, at least.
The source code for the project, published under the permissive MIT license, is available on Shymanskyy's GitHub repository; the library is also usable from within the Arduino IDE's Library Manager, PlatformIO, and Particle Build.