I have more details, and I think I got trapped by a bug in the preferences lib:
When I use the default nvs to store my pref variables, all works fine - values are maintained over reboots as well as over uploads.
But when I use a custom nvs as a 2nd nvs, my values are still maintained over reboots, but randomly lost from none to all over an upload.
Here my configuration and situation with a single, default nvs (using platformio in vscode):
Code: Select all
File 'partitions.csv' (only default is active)
# Name, Type, SubType, Offset, Size, Flags
# nvs default
nvs, data, nvs, , 0x3000,
# nvs custom
# nvscustom, data, nvs, , 0x2000,
Code initiating nvs (default is used)
#define AMBIOMON "ambiomon"
#define NVSPARTITION "nvs" // using default nvs
// #define NVSPARTITION "nvscustom" // using custom nvs
bool mynvs = prefs.begin(AMBIOMON, false, NVSPARTITION); // false => allow both read and write
Flash Storage Partitions: (Chip Size: 4 MiB)
Type : SubT Label Address Bytes MiB Blk
SecureBoot 0x000000 0x0001000 0.004 1
Bootloader 0x001000 0x0007000 0.027 7
Partition Table 0x008000 0x0001000 0.004 1
0x01 Data : 0x00 otadata 0x009000 0x0002000 0.008 2
0x01 Data : 0x02 nvs 0x00b000 0x0003000 0.012 3
0x00 App : 0x10 app0 0x010000 0x0160000 1.375 352
0x00 App : 0x11 app1 0x170000 0x0160000 1.375 352
0x01 Data : 0x03 coredump 0x2d0000 0x0010000 0.062 16
0x01 Data : 0x82 spiffs 0x2e0000 0x0120000 1.125 288
Flash Size Total 0x03fe000 3.992 1022
Flash Size Free 0x0002000 0.008 2
From the code log:
Initializing NVS
Success initializing namespace: 'ambiomon'
Free Entries : 231
NVS key amcfg : Using: 136 Bytes (Type Bytes)
NVS key crc : Using: 40 Bytes (Type Bytes)
NVS key syslogNo : Using: 2 Bytes (Type Short)
So far my past and current experience with a single, default nvs.
Now I activate a 2nd nvs as custom nvs, and I store my pref values in this custom nvs:
Code: Select all
File 'partitions.csv' (both default and custom are active)
# Name, Type, SubType, Offset, Size, Flags
# nvs default
nvs, data, nvs, , 0x3000,
# nvs custom
nvscustom, data, nvs, , 0x2000,
Code initiating nvs (custom is used)
#define AMBIOMON "ambiomon"
// #define NVSPARTITION "nvs" // using default nvs
#define NVSPARTITION "nvscustom" // using custom nvs
bool mynvs = prefs.begin(AMBIOMON, false, NVSPARTITION); // false => allow both read and write
Flash Storage Partitions: (Chip Size: 4 MiB)
Type : SubT Label Address Bytes MiB Blk
SecureBoot 0x000000 0x0001000 0.004 1
Bootloader 0x001000 0x0007000 0.027 7
Partition Table 0x008000 0x0001000 0.004 1
0x01 Data : 0x00 otadata 0x009000 0x0002000 0.008 2
0x01 Data : 0x02 nvs 0x00b000 0x0003000 0.012 3
0x01 Data : 0x02 nvscustom 0x00e000 0x0002000 0.008 2
0x00 App : 0x10 app0 0x010000 0x0160000 1.375 352
0x00 App : 0x11 app1 0x170000 0x0160000 1.375 352
0x01 Data : 0x03 coredump 0x2d0000 0x0010000 0.062 16
0x01 Data : 0x82 spiffs 0x2e0000 0x0120000 1.125 288
Flash Size Total 0x0400000 4.000 1024
Flash Size Free 0x0000000 0.000 0
From the log:
Initializing NVS
Success initializing namespace: 'ambiomon'
Free Entries : 231
NVS key amcfg : MISSING resetNVSkey: amcfg
NVS key crc : MISSING resetNVSkey: crc
NVS key syslogNo : MISSING - has now been created
You see that all three pref variables (2 type Bytes, 1 type Short) are missing after an upload (same when type String is used, not shown).
If this is not a bug, please help and explain what I made wrong!