Saving preferences doesn't work
Posted: Tue Oct 06, 2020 3:02 pm
Hi,
I'm new to ESP32 and Arduino, but I know a few bits about programing Atmel micros.
In my ESP32 project I'm trying to retrieve some data from NVS using the preferences library. If the data (or rather the key for the data) doesn't exist, I set a default value and save that value to preferences.
When I reboot, I expect the key and the associated data to exist on NVS and to load without problems. But that doesn't happen.
Here's my code (I left out the statements which are irrelevant to using preferences):
Here is the corresponding output on Serial Monitor:
which tells me that my default values didn't make it to NVS.
I'm new to ESP32 and Arduino, but I know a few bits about programing Atmel micros.
In my ESP32 project I'm trying to retrieve some data from NVS using the preferences library. If the data (or rather the key for the data) doesn't exist, I set a default value and save that value to preferences.
When I reboot, I expect the key and the associated data to exist on NVS and to load without problems. But that doesn't happen.
Here's my code (I left out the statements which are irrelevant to using preferences):
Code: Select all
char * registration;
char * pin;
char * uuid;
prefs.begin("Auth", false); // ESP32 Flash storage, Auth namespace
if(prefs.getString("Reg", registration, 8) == 0) { // get bike registration ID
registration = registration_DEFAULT; // assign default if not defined
}
if(prefs.getString("PIN", pin, 8) == 0) { // get PIN code if it exists
Serial.println("Assigning default PIN: 123456");
pin = "123456"; // assign default if not defined
if(prefs.putString("PIN", pin) == strlen(pin)) { // save to NVS
Serial.println("Success saving PIN to NVS");
}
}
else {
Serial.print("Loaded PIN from NVS - PIN = ");
Serial.println(pin);
}
if(prefs.getString("UUID", uuid, 40) == 0) { // get device UUID if it exists
Serial.println("Assigning default UUID: 00000000-0000-0000-0000-000000000000");
uuid = "00000000-0000-0000-0000-000000000000"; // assign Nil/Empty UUID if not defined
if(prefs.putString("UUID", uuid) == strlen(uuid)) {
Serial.println("Success saving UUID to NVS");
}
}
else {
Serial.print("Loaded UUID from NVS - UUID = ");
Serial.println(uuid);
}
prefs.end(); // close Auth namespace.
After rebooting, I expect that serial monitor shows like "Loaded PIN from NVS - PIN = ...", etc, but that doesn't happen,ets Jun 8 2016 00:22:57
rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1044
load:0x40078000,len:8896
load:0x40080400,len:5816
entry 0x400806ac
Assigning default PIN: 123456
Success saving PIN to NVS
Assigning default UUID: 00000000-0000-0000-0000-000000000000
Success saving UUID to NVS
Registration = MABC123
Success to initialize controller
Success to initialize bluedroid
Success to enable bluedroid
Success to initialize SerialBT using bt_name
Bluetooth interface enabled, please pair with your smartphone and connect
Device address = FC:F5:C4:01:C0:6A
which tells me that my default values didn't make it to NVS.