NVS
On this page
If you have any data that you want to store in kublet permanently, you can save that in the kublet’s Non-Volatile Storage Library (NVS). This data is preserved in the event of a power reset, but erased if a new app is flashed.
Non-volatile storage (NVS) library is designed to store key-value pairs in flash. They are useful for storing sensitive information such as API keys, certs.
NVS is a simpler way of storing a small amount of data, like e.g. a struct, instead of a full-blown filesystem.
How to use
We use the Preferences.h
library that comes with Arduino to store data in NVS. The basic flow of using NVS looks like this:
All key value pairs are stored in namespaces. For published Krates on Kublet, all key value pairs must be stored in the namespace app
. To store key value pairs, you would need to first open/create the storage space with the namespace app
:
The false
opens the namespace in read/write mode. Use true to open the namespace in read-only mode.
After reading or writing to the namespace, close it with:
The code for writing to the namespace looks like this:
string
is not the only data type you can save. NVS supports multiple data types, and you would write to NVS like this:
Type | Function |
---|---|
Char | putChar(const char* key, int8_t value) |
Unsigned Char | putUChar(const char* key, int8_t value) |
Short | putShort(const char* key, int16_t value) |
Unsigned Short | putUShort(const char* key, uint16_t value) |
Int | putInt(const char* key, int32_t value) |
Unsigned Int | putUInt(const char* key, uint32_t value) |
Long | putLong(const char* key, int32_t value) |
Unsigned Long | putULong(const char* key, uint32_t value) |
Long64 | putLong64(const char* key, int64_t value) |
Unsigned Long64 | putULong64(const char* key, uint64_t value) |
Float | putFloat(const char* key, const float_t value) |
Double | putDouble(const char* key, const double_t value) |
Bool | putBool(const char* key, const bool value) |
String | putString(const char* key, const String value |
Bytes | putBytes(const char* key, const void* value, size_t len) |
To read a value from the namespace, after opening the namespace, you would write:
Similarly, you would call different methods depending on the data type you want to read:
Type | Function |
---|---|
Char | getChar(const char* key, const int8_t defaultValue) |
Unsigned Char | getUChar(const char* key, const uint8_t defaultValue) |
Short | getShort(const char* key, const int16_t defaultValue |
Unsigned Short | getUShort(const char* key, const uint16_t defaultValue) |
Int | getInt(const char* key, const int32_t defaultValue) |
Unsigned Int | getUInt(const char* key, const uint32_t defaultValue) |
Long | getLong(const char* key, const int32_t defaultValue) |
Unsigned Long | getULong(const char* key, const uint32_t defaultValue) |
Long64 | getLong64(const char* key, const int64_t defaultValue) |
Unsigned Long64 | getULong64(const char* key, const uint64_t defaultValue) |
Float | getFloat(const char* key, const float_t defaultValue) |
Double | getDouble(const char* key, const double_t defaultValue) |
Bool | getBool(const char* key, const bool defaultValue) |
String | getString(const char* key, const String defaultValue) |
String | getString(const char* key, char* value, const size_t maxLen) |
Bytes | getBytes(const char* key, void * buf, size_t maxLen) |
To clear all key value pairs within a namespace, call:
To remove a key from the namespace: