When I was transitioning from the ESP EEPROM emulation to the Preferences class, I did the following using the original struct to minimise changes to the rest of the program. However, I changed rapidly to using the native Preferences getter and setter functions throughout.
Config.h
#ifndef _Config_h
#define _Config_h
#include <Arduino.h>
namespace nsConfig {
// change this to force a clean out and rebuild of the EEPROM data structures.
const uint16_t config_t_version = 1002 ; // increment this if you change config_t
struct config_t {
uint16_t eepromLayoutVersion; // copy of config_t_version
unsigned int runNumber ; // incremented on reboot
bool networkMode ; // false = standalone.
//screen calibration
float xScale ;
float xShift ;
float yScale ;
float yShift ;
//wlan
char ssid[32];
char psk[64];
char remoteHost[32] ;
// number translation rules
char ruleIntlFrom[6] ; // eg "00"
char ruleIntlTo[6] ; // eg "+"
char ruleLocalFrom[6] ; // eg "0"
char ruleLocalTo[6] ; // eg "+41"
char phpRetrievePath[32] ;
char phpStorePath[32] ;
char spamLevel ;
} ;
extern config_t config ;
// prototypes
void eepromLoad() ;
void eepromFetch() ;
void PrintEepromVariables() ;
void setRequestEepromReset() ;
void setup() ;
} // namespace nsConfig {
#endif
Config.cpp
/*
Config.cpp
Handle persistent configuration data
Uses ESP32 Preferences class
Adapted from an existing EEPROM model which used a struct to store the data.
In this "Preferences" model there are 2 keys, "config_t_ver" which indicates the
version of the struct config and "configKey" which is the struct itself. If the
format or size of the struct changes, then increment config_t_version which is
used to force a force a factory refresh and prevent the use of mismatched data.
An alternative would be to use the keys directly within a name space.
*/
#include "Config.h"
#include <Preferences.h>
namespace nsConfig {
Preferences preferences;
config_t config;
bool requestEepromReset = false;
char buf[120] = { 0 }; // make global ??
void ICACHE_FLASH_ATTR eepromFetch() {
Serial.println("in nsConfig::eepromFetch()");
// EEPROM => config
size_t configKeySize = preferences.getBytesLength("configKey");
// configPtr = (config_t *)&config;
if (sizeof(config) == configKeySize) {
} else {
snprintf(buf, sizeof(buf) - 1, "sizeof(config) struct: (%d); sizeof(config) key: (%d)\r\n", sizeof(config), configKeySize);
Serial.print(buf);
}
preferences.getBytes("configKey", &config, sizeof(config));
}
void ICACHE_FLASH_ATTR eepromLoad() {
Serial.println("in nsConfig::eepromLoad()");
// config => EEPROM
// EEPROM.put(0, config);
// delay(200); // taken over from ESP8266 version
// EEPROM.commit();
preferences.putBytes("configKey", &config, sizeof(config));
}
void setRequestEepromReset() {
requestEepromReset = true;
}
void forceFactoryReset() {
Serial.println("Config.cpp: Eeprom forceFactoryReset()");
// 'factory' settings
// For convenience add your own settings to avoid having to enter
// these initially in APmode through the web interface.
// SSID/psk are best removed at the end
// of any development and before publication.
config.eepromLayoutVersion = config_t_version; // redundant here
config.runNumber = 0;
strcpy(config.psk, "ggggg");
strcpy(config.ssid, "gggg");
config.networkMode = false;
// screen model specific - dependent on rotation / flipping of touch screen
config.xScale = 1.0 / 11.0;
config.xShift = 170;
config.yScale = 1.0 / 14.9;
config.yShift = 220;
strcpy(config.remoteHost, "www.gggg.com"); //
strcpy(config.ruleIntlFrom, "00");
strcpy(config.ruleIntlTo, "+");
strcpy(config.ruleLocalFrom, "0");
strcpy(config.ruleLocalTo, "+41");
strcpy(config.phpRetrievePath, "/Json.php");
strcpy(config.phpStorePath, "/storeData.php");
config.spamLevel = '1';
preferences.putUShort("config_t_ver", config_t_version);
eepromLoad();
}
void PrintEepromVariables() {
Serial.println("\nEEprom dump:");
snprintf(buf, sizeof(buf) - 1, "sizeof(config): (%d)\r\n", sizeof(config));
Serial.print(buf);
snprintf(buf, sizeof(buf) - 1, "eepromLayourVersion: (%d) \r\n", config.eepromLayoutVersion);
Serial.print(buf);
snprintf(buf, sizeof(buf) - 1, "networkMode: (%d) \r\n", config.networkMode);
Serial.print(buf);
snprintf(buf, sizeof(buf) - 1, "ssid: (%s) \r\n", config.ssid);
Serial.print(buf);
snprintf(buf, sizeof(buf) - 1, "psk: (%s) \r\n", config.psk);
Serial.print(buf);
snprintf(buf, sizeof(buf) - 1, "remoteHost: (%s) \r\n", config.remoteHost);
Serial.print(buf);
snprintf(buf, sizeof(buf) - 1, "xScale: (%08.3f) \r\n", config.xScale);
Serial.print(buf);
snprintf(buf, sizeof(buf) - 1, "xShift: (%08.3f) \r\n", config.xShift);
Serial.print(buf);
snprintf(buf, sizeof(buf) - 1, "yScale: (%08.3f) \r\n", config.yScale);
Serial.print(buf);
snprintf(buf, sizeof(buf) - 1, "yShift: (%08.3f) \r\n", config.yShift);
Serial.print(buf);
snprintf(buf, sizeof(buf) - 1, "ruleIntlFrom: (%s) \r\n", config.ruleIntlFrom);
Serial.print(buf);
snprintf(buf, sizeof(buf) - 1, "ruleIntlTo: (%s) \r\n", config.ruleIntlTo);
Serial.print(buf);
snprintf(buf, sizeof(buf) - 1, "ruleLocalFrom: (%s) \r\n", config.ruleLocalFrom);
Serial.print(buf);
snprintf(buf, sizeof(buf) - 1, "ruleLocalTo: (%s) \r\n", config.ruleLocalTo);
Serial.print(buf);
snprintf(buf, sizeof(buf) - 1, "phpRetrievePath: (%s) \r\n", config.phpRetrievePath);
Serial.print(buf);
snprintf(buf, sizeof(buf) - 1, "phpStorePath: (%s) \r\n", config.phpStorePath);
Serial.print(buf);
snprintf(buf, sizeof(buf) - 1, "spamLevel: (%c) \r\n", config.spamLevel);
Serial.print(buf);
Serial.println("end of EEprom dump");
}
void setup() {
preferences.begin("config", false); // open RW mode
if (preferences.isKey("config_t_ver")) {
if (preferences.getUShort("config_t_ver") == config_t_version) {
snprintf(buf, sizeof(buf) - 1, "No change: preferences config_t_version= (%d) ; this config_t_version= (%d) \r\n", preferences.getUShort("config_t_ver"), config_t_version);
Serial.print(buf);
eepromFetch();
}
else {
snprintf(buf, sizeof(buf) - 1, "Mismatch: preferences config_t_version= (%d) ; this config_t_version= (%d) \r\n", preferences.getUShort("config_t_ver"), config_t_version);
Serial.print(buf);
forceFactoryReset();
}
}
else {
Serial.println("config_t_version key not found");
forceFactoryReset();
}
snprintf(buf, sizeof(buf) - 1,"There are: %u entries available in the namespace table.\r\n", preferences.freeEntries() );
Serial.print(buf);
PrintEepromVariables();
}
} // namespace nsConfig
Later, I used something similar to this showing an example of setting and getting individual preferences:
#include <Preferences.h> // ESP32 core
Preferences preferences;
. . .
preferences.begin("config", false); // open RW mode name space "config"
// if configuration information is not found in preferences then set default values
if (!preferences.isKey("input1")) preferences.putString("input1", "Factory input1");
if (!preferences.isKey("input2")) preferences.putString("input2", "Factory input2");
if (!preferences.isKey("input3")) preferences.putString("input3", "Factory input3");
if (!preferences.isKey("buttonPressedA")) preferences.putUShort("buttonPressedA", 0);
Serial.printf("input1 has value: (%s) \n", preferences.getString("input1").c_str());
Serial.printf("input2 has value: (%s) \n", preferences.getString("input2").c_str());
Serial.printf("input3 has value: (%s) \n", preferences.getString("input3").c_str());
Serial.printf("buttonPressedA has value: (%d) \n", preferences.getUShort("buttonPressedA"));
However, if you did not use a struct originally, then just replace each access to your variables with either a getter or setter function as appropriate.