Preferences.h, Why won't the 'get/put' functions allow String?

I'm changing from an Arduino Mega to an M5Stack Core2. I found that the EEPROM write/read didn't work so I'm changing over to using the Preferences.h (found the hint on this site).

In the Mega code, I had the Eeprom addresses as declared variables:

const int i160m = 0;  //Value is Eeprom address
const int i80m = 2;
const int i40m = 4;

So in my subroutine, I tried to create a String variable appended with the Value that is passed in. Anyway, I can't get the functions to parse with string variables:

void NvWrite(int p_address, int p_value) {
  String NV = "NV" + p_address;
  NvStorage.putUInt(NV, p_value);
}

I get the error:
Compilation error: no matching function for call to 'Preferences::putUInt(String&, int&)'

But if I change to:

void NvWrite(int p_address, int p_value) {
  //String NV = "NV" + p_address;
  NvStorage.putUInt("SomeString", p_value);
}

That will parse OK.

I tried:

NvStorage.putUInt(*NV, p_value);

but that didn't work either. I don't know what else to try.

In my research on Preferences.h, all I can find is examples that use a "string".

Sir Michael

Because the get/put routines take a const char* as the key name, not a String.

Try:

void NvWrite(int p_address, int p_value) {
  char NV[10];
  snprintf(NV, sizeof NV, "NV%u", p_address);
  NvStorage.putUInt(NV, p_value);
}

Thanks!

I changed the way that I searched for the problem and found:

void NvWrite(int p_address, int p_value) {
  String NV = "NV" + p_address;
  NvStorage.putUInt(NV.c_str(), p_value);
}

worked also (adding the "NV.c_str()").

Sir Michael

You really should not be using C++ String (also can't store in flash) but rather use C char-array string.

That doesn't do what you intend. If you try

  int p = 3;
  String NV = "abcdefg" + p;
  Serial.println(NV);

and see that it prints defg, does that help explain why?

The reference is here;

https://docs.espressif.com/projects/arduino-esp32/en/latest/tutorials/preferences.html

Preferences.h will absolutely let you use a String. It converts the String object to a .c_str(().

size_t Preferences::putString(const char *key, const String value) {
  return putString(key, value.c_str());
}
#include <Preferences.h>
Preferences prefs;

String MyEmail    = "myEmail@myProvider.com";
String MyPassWord = "thereisnopassword";
String MySecret   = "NUX007";

String MyEmailR    = "";
String MyPassWordR = "";
String MySecretR   = "";


void setup() {
  Serial.begin(115200);
  prefs.begin("Settings"); //namespace
  
  //key/value pairs within namespace
  prefs.putString("MyEmail", MyEmail);
  prefs.putString("MyPassWord", MyPassWord);
  prefs.putString("MySecret", MySecret);

  MyEmailR = prefs.getString("MyEmail");
  Serial.println(MyEmailR);
  MyPassWordR = prefs.getString("MyPassWord");
  Serial.println(MyPassWordR);
  MySecretR = prefs.getString("MySecret");
  Serial.println(MySecretR);

}

void loop() {}
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.