Page 1 of 1

Need help with Preferences.h

Posted: Tue Jun 08, 2021 8:47 pm
by butchalline
/*
Using this page as a guide https://randomnerdtutorials.com/esp32-s ... eferences/
the following code prints the following to the serial monitor:

data saved
5
5

why does it not print?
hello
world
*/

#include <Preferences.h>
Preferences preferences;

const char* line1 = "hello";
const char* line2 = "world";

//String line1 = "hello"; // this produces same results
//String line2 = "world"; // this produces same results

void setup() {
Serial.begin(115200);
Serial.println();

preferences.begin("any_name_here", false);
preferences.putString("line1", line1);
preferences.putString("line2", line2);

Serial.println("data saved ");
Serial.println(preferences.putString("line1", line1));
Serial.println(preferences.putString("line2", line2));
preferences.end();
}
void loop() {
}

As always, any and all help appreciated

Re: Need help with Preferences.h

Posted: Tue Jun 08, 2021 9:00 pm
by lbernstone
putString returns the number of characters saved to the key. This can be compared to the expected string length to ensure success. Perhaps you were looking for getString?

Re: Need help with Preferences.h

Posted: Tue Jun 08, 2021 10:21 pm
by butchalline
Thank you lbernstone
Yes, it is so obvious now.
I cut and pasted from the lines above and did not change putString to getString!

Thanks again.

Case closed