Preferences.h : data doesn't get stored

Hi all!
I'm a newbie in ESP32 programming and I've been trying to create an alarm clock with different times for every day of the week, using RemoteXY as web interface and the Preferences.h library to store the set hours & minutes of the alarm.
Unfortunately when I try to store the data it doesn't write it, or else when I read it it doesn't read it correctly.
Here's the code (only kept the line for Monday to make it shorter):

#define REMOTEXY_MODE__ESP32CORE_WIFI_POINT
#include <WiFi.h>
#include <RemoteXY.h>

// RemoteXY connection settings 
#define REMOTEXY_WIFI_SSID "RemoteXY"
#define REMOTEXY_WIFI_PASSWORD "12345678"
#define REMOTEXY_SERVER_PORT 6377

// library for storing in permanent flash memeory
#include <Preferences.h>

#include <analogWrite.h>


// Variables for Alarm 

  // variable sfor RemoteXY visualisation
String ActTimeString ;
String MonAlarmString ;
String TueAlarmString ;
String WedAlarmString ;
String ThuAlarmString ;
String FriAlarmString ;
String SatAlarmString ;
String SunAlarmString ;

//Storage name

Preferences preferences;

// variables for working with alarms that have been read from storage
int16_t MonAlarmSetHour; 
int16_t MonAlarmSetMin;
uint8_t MonAlarmSetValue;
uint8_t MonAlarmSetStatus; // =1 if switch ON and =0 if OFF 


// RemoteXY configurate  
#pragma pack(push, 1)
uint8_t RemoteXY_CONF[] =
  { 255,65,0,51,0,211,2,13,8,5,
  130,1,0,0,63,21,0,17,1,0,
  29,8,6,6,0,2,31,83,69,84,
  0,7,20,1,8,7,4,0,2,26,
  2,7,20,9,8,7,4,0,2,26,
  2,7,20,17,8,6,4,0,2,26,
  2,7,20,1,15,7,4,0,2,26,
  2,7,20,9,15,7,4,0,2,26,
  2,7,20,17,15,10,4,0,2,26,
  2,67,4,46,8,16,4,0,2,26,
  7,67,4,43,15,19,4,0,2,26,
  9,129,0,1,25,8,4,0,17,77,
  111,110,0,129,0,1,46,7,4,0,
  17,84,104,117,0,129,0,1,39,9,
  4,0,17,87,101,100,0,129,0,1,
  32,7,4,0,17,84,117,101,0,129,
  0,1,61,6,4,0,17,83,97,116,
  0,129,0,1,54,5,4,0,17,70,
  114,105,0,129,0,1,68,8,4,0,
  17,83,117,110,0,7,20,19,25,7,
  5,0,2,25,2,7,20,11,25,7,
  5,0,2,25,2,2,0,29,25,10,
  5,0,2,26,31,31,79,78,0,79,
  70,70,0,7,20,11,39,7,5,0,
  2,25,2,7,20,19,39,7,5,0,
  2,25,2,2,0,29,39,10,5,0,
  2,26,31,31,79,78,0,79,70,70,
  0,7,20,11,32,7,5,0,2,25,
  2,7,20,19,32,7,5,0,2,25,
  2,2,0,29,32,10,5,0,2,26,
  31,31,79,78,0,79,70,70,0,7,
  20,11,46,7,5,0,2,25,2,7,
  20,19,46,7,5,0,2,25,2,2,
  0,29,46,10,5,0,2,26,31,31,
  79,78,0,79,70,70,0,7,20,11,
  53,7,5,0,2,25,2,7,20,19,
  53,7,5,0,2,25,2,2,0,29,
  53,10,5,0,2,26,31,31,79,78,
  0,79,70,70,0,7,20,11,60,7,
  5,0,2,25,2,7,20,19,60,7,
  5,0,2,25,2,2,0,29,60,10,
  5,0,2,26,31,31,79,78,0,79,
  70,70,0,7,20,11,67,7,5,0,
  2,25,2,7,20,19,67,7,5,0,
  2,25,2,2,0,29,67,10,5,0,
  2,26,31,31,79,78,0,79,70,70,
  0,67,0,49,25,14,5,0,16,8,
  5,67,0,49,32,14,5,0,16,8,
  5,67,0,49,39,14,5,0,16,8,
  5,67,0,49,46,14,5,0,16,8,
  5,67,0,49,53,14,5,0,16,8,
  5,67,0,49,60,14,5,0,16,8,
  5,67,0,49,67,14,5,0,16,8,
  5,131,1,2,2,14,4,0,2,31,
  80,97,103,101,32,49,0,131,0,48,
  2,14,4,1,2,31,80,97,103,101,
  32,50,0,6,0,5,27,26,26,1,
  2,26,2,0,34,36,18,7,1,2,
  26,31,31,79,78,0,79,70,70,0,
  6,0,4,57,26,26,1,2,26,2,
  0,32,68,18,7,1,2,26,31,31,
  79,78,0,79,70,70,0,1,0,42,
  25,5,5,0,2,31,83,101,116,0,
  1,0,42,53,5,5,0,2,31,83,
  101,116,0,1,0,42,46,5,5,0,
  2,31,83,101,116,0,1,0,42,39,
  5,5,0,2,31,83,101,116,0,1,
  0,42,32,5,5,0,2,31,83,101,
  116,0,1,0,42,60,5,5,0,2,
  31,83,101,116,0,1,0,42,67,5,
  5,0,2,31,83,101,116,0,1,0,
  53,36,8,8,1,2,31,88,0,1,
  0,53,68,8,8,1,2,31,88,0 };
  
// this structure defines all the variables and events of your control interface 
struct {

    // input variables
  uint8_t ActTimeSet; // =1 if button pressed, else =0 
  int16_t ActTimeHour;  // 32767.. +32767 
  int16_t ActTimeMinits;  // 32767.. +32767 
  int16_t ActTimeSec;  // 32767.. +32767 
  int16_t ActDateDay;  // 32767.. +32767 
  int16_t ActDateMonth;  // 32767.. +32767 
  int16_t ActDateYear;  // 32767.. +32767 
  int16_t MonAlarmMin;  // 32767.. +32767 
  int16_t MonAlarmHour;  // 32767.. +32767 
  uint8_t MonAlarmStatus; // =1 if switch ON and =0 if OFF 
  int16_t WedAlarmHour;  // 32767.. +32767 
  int16_t WedAlarmMin;  // 32767.. +32767 
  uint8_t WedAlarmStatus; // =1 if switch ON and =0 if OFF 
  int16_t TueAlarmHour;  // 32767.. +32767 
  int16_t TueAlarmMin;  // 32767.. +32767 
  uint8_t TueAlarmStatus; // =1 if switch ON and =0 if OFF 
  int16_t ThuAlarmHour;  // 32767.. +32767 
  int16_t ThuAlarmMin;  // 32767.. +32767 
  uint8_t ThuAlarmStatus; // =1 if switch ON and =0 if OFF 
  int16_t FriAlarmHour;  // 32767.. +32767 
  int16_t FriAlarmMin;  // 32767.. +32767 
  uint8_t FriAlarmStatus; // =1 if switch ON and =0 if OFF 
  int16_t SatAlarmHour;  // 32767.. +32767 
  int16_t SatAlarmMin;  // 32767.. +32767 
  uint8_t SatAlarmStatus; // =1 if switch ON and =0 if OFF 
  int16_t SunAlarmHour;  // 32767.. +32767 
  int16_t SunAlarmMin;  // 32767.. +32767 
  uint8_t SunAlarmStatus; // =1 if switch ON and =0 if OFF 
  uint8_t RGBSunriseStart_r; // =0..255 Red color value 
  uint8_t RGBSunriseStart_g; // =0..255 Green color value 
  uint8_t RGBSunriseStart_b; // =0..255 Blue color value 
  uint8_t SunriseStartManual; // =1 if switch ON and =0 if OFF 
  uint8_t RGBSunriseEnd_r; // =0..255 Red color value 
  uint8_t RGBSunriseEnd_g; // =0..255 Green color value 
  uint8_t RGBSunriseEnd_b; // =0..255 Blue color value 
  uint8_t SunriseEndManual; // =1 if switch ON and =0 if OFF 
  uint8_t MonAlarmSet; // =1 if button pressed, else =0 
  uint8_t FriAlarmSet; // =1 if button pressed, else =0 
  uint8_t ThuAlarmSet; // =1 if button pressed, else =0 
  uint8_t WedAlarmSet; // =1 if button pressed, else =0 
  uint8_t TueAlarmSet; // =1 if button pressed, else =0 
  uint8_t SatAlarmSet; // =1 if button pressed, else =0 
  uint8_t SunAlarmSet; // =1 if button pressed, else =0 
  uint8_t SunriseStartSet; // =1 if button pressed, else =0 
  uint8_t SunriseEndSet; // =1 if button pressed, else =0 

    // output variables
  char SetTime[7];  // string UTF8 end zero 
  char SetDate[9];  // string UTF8 end zero 
  char MonAlarmSetTime[5];  // string UTF8 end zero 
  char TueAlarmSetTime[5];  // string UTF8 end zero 
  char WedAlarmSetTime[5];  // string UTF8 end zero 
  char ThuAlarmSetTime[5];  // string UTF8 end zero 
  char FriAlarmSetTime[5];  // string UTF8 end zero 
  char SatAlarmSetTime[5];  // string UTF8 end zero 
  char SunAlarmSetTime[5];  // string UTF8 end zero 

    // other variable
  uint8_t connect_flag;  // =1 if wire connected, else =0 

} RemoteXY;
#pragma pack(pop)

/////////////////////////////////////////////
//           END RemoteXY include          //
/////////////////////////////////////////////



void FlashRead()        //read what's been stored in the memory and print for debug
{
  
delay(2000); 
preferences.begin("Alarms",false);

// variables for working with alarms that have been read from storage
MonAlarmSetHour=preferences.getShort("MonAlarmHourStorage", 37);
Serial.println("Monday alarm read:");
Serial.println(MonAlarmSetHour);
MonAlarmSetMin=preferences.getShort("MonAlarmMinStorage", 65);
MonAlarmSetStatus=preferences.getUShort("MonAlarmStatusStorage", 0); // =1 if switch ON and =0 if OFF 

preferences.end();
  
  }

void ChangeStored()     //add data to the storage if the Monday's set button has been pressed 
{

preferences.begin("Alarms",false);

if (RemoteXY.MonAlarmSet==1){
  //store the data
MonAlarmSetHour=RemoteXY.MonAlarmHour; 
MonAlarmSetMin=RemoteXY.MonAlarmMin;
  
preferences.putShort("MonAlarmHourStorage", MonAlarmSetHour); 
preferences.putShort("MonAlarmMinStorage", MonAlarmSetMin);
//update the local variable for further use

Serial.println("monalarm set");
Serial.println(RemoteXY.MonAlarmHour);

//debug
// variables for working with alarms that have been read from storage
MonAlarmSetHour=preferences.getUShort("MonAlarmHourStorage",36); 
Serial.println("Monday alarm read:");
Serial.println(MonAlarmSetHour);
Serial.println(preferences.getUShort("MonAlarmHourStorage",36)); //added to check the direct stored value
 
  }


preferences.end();

}

void TimeToString()
{
  // transform all stored times to string to export it to RemoteXY interface
MonAlarmString=String(MonAlarmSetHour*100+MonAlarmSetMin);


  // export all times to RemoteXY as string
MonAlarmString.toCharArray(RemoteXY.MonAlarmSetTime,5);

}

void setup() 
{
  RemoteXY_Init (); 
  Serial.begin(9600);
  FlashRead();
  
}

void loop() 
{ 
  RemoteXY_Handler ();

  TimeToString(); // transform the set times to strings so they can be shown in the remoteXY interface

  ChangeStored();
    

}

And here's the output I get when I enter 7 in the RemoteXY hours for monday:

monalarm set
7
Monday alarm read:
36
36

I'd be grateful for any help I can get (and I hope I posted all the required info)

Welcome

I believe the key parameter is limited to 15 characters

Hi @nerull1976
The best way to find a problem is to simplify the area where the error occurs.
For example: Simplify , sketch that saves the data and then fetches the data and compares it with the saved ones.
Afterwards, make a sketch that only fetches the data saved in the previous sketch and prints them on the serial monitor and see if they are correct.
If not, it works ok, do the same test using RemoteXY,
and see if it works correctly.
And so, expand the testing area.

RV mineirin

Hi ruilviana,

I tried it that way, with the Startcounter example, which worked perfectly:

#include <Preferences.h>

Preferences preferences;

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

  // Open Preferences with my-app namespace. Each application module, library, etc
  // has to use a namespace name to prevent key name collisions. We will open storage in
  // RW-mode (second parameter has to be false).
  // Note: Namespace name is limited to 15 chars.
  preferences.begin("my-app", false);

  // Remove all preferences under the opened namespace
  //preferences.clear();

  // Or remove the counter key only
  //preferences.remove("counter");

  // Get the counter value, if the key does not exist, return a default value of 0
  // Note: Key name is limited to 15 chars.
  unsigned int counter = preferences.getUInt("counter", 0);

  // Increase counter by 1
  counter++;

  // Print the counter to Serial Monitor
  Serial.printf("Current counter value: %u\n", counter);

  // Store the counter to the Preferences
  preferences.putUInt("counter", counter);

  // Close the Preferences
  preferences.end();

  // Wait 10 seconds
  Serial.println("Restarting in 10 seconds...");
  delay(10000);

  // Restart ESP
  ESP.restart();
}

void loop() {}

But the moment I add data from RemoteXY it doesn't save the data. Hence my cry for help on the forum.
Thanks for the advice!

Prefrences is a wrapper for the ESP32 NV Storage. To better understand how prefrences is used taking a look at the NVS, the underlaying structure, gives a better understanding to the purpose and scope of the use.

Non-volatile storage library - ESP32 - — ESP-IDF Programming Guide latest documentation (espressif.com)

Also, important to NVS is the NVS partation: NVS Partition Generator Utility - ESP32 - — ESP-IDF Programming Guide latest documentation (espressif.com)

YES!! that was it!
@guix: Thanks a gazillion, I've been trying to figure this out for over a week now. Next time I'll dive a little deeper in the definitions.
@Idahowalker: thanks for the help as well, the 15 char limit is indeed mentioned in the NVS library explanation.