Need help with Preferences.h

butchalline
Posts: 14
Joined: Sat Oct 14, 2017 2:05 pm

Need help with Preferences.h

Postby butchalline » Tue Jun 08, 2021 8:47 pm

/*
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

lbernstone
Posts: 828
Joined: Mon Jul 22, 2019 3:20 pm

Re: Need help with Preferences.h

Postby lbernstone » Tue Jun 08, 2021 9:00 pm

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?

butchalline
Posts: 14
Joined: Sat Oct 14, 2017 2:05 pm

Re: Need help with Preferences.h

Postby butchalline » Tue Jun 08, 2021 10:21 pm

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

Who is online

Users browsing this forum: No registered users and 32 guests