#include <EEPROM.h>
// Define the addresses where you want to store your uint64_t variables
bool has_credentials() {
return EEPROM.read(0) == 0x42 /* credentials marker */;
}
// Define addresses for storing numbers in EEPROM
const int ADDRESS_NUMBER1 = 1; // Address for the first number
const int ADDRESS_NUMBER2 = 11; // Address for the second number
// Function to save a number as a string to EEPROM
void saveNumberToEEPROM(long number, int address) {
if (!has_credentials()) EEPROM.write(0, 0x42);
// Convert the number to a string
char buffer[9]; // 8 digits + null terminator
sprintf(buffer, "%ld", number);
// Write each character of the string to EEPROM
for (int i = 0; i < 8; i++) {
EEPROM.write(address + i, buffer[i]);
}
// Null-terminate the string in EEPROM
EEPROM.write(address + 8, '\0');
}
// Function to load a number as a string from EEPROM
long loadNumberFromEEPROM(int address) {
if (!has_credentials()) return 0;
// Read each character from EEPROM and reconstruct the string
char buffer[9];
for (int i = 0; i < 8; i++) {
buffer[i] = EEPROM.read(address + i);
}
// Null-terminate the string
buffer[8] = '\0';
// Convert the string back to a number
long number = atol(buffer);
return number;
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
// Initialize EEPROM
EEPROM.begin(512);
// Save and load example
long number1 = 1;
long number2 = 2;
saveNumberToEEPROM(number1, ADDRESS_NUMBER1); // Save number1 at ADDRESS_NUMBER1
saveNumberToEEPROM(number2, ADDRESS_NUMBER2); // Save number2 at ADDRESS_NUMBER2
long loadedNumber1 = loadNumberFromEEPROM(ADDRESS_NUMBER1); // Load number1 from ADDRESS_NUMBER1
long loadedNumber2 = loadNumberFromEEPROM(ADDRESS_NUMBER2); // Load number1 from ADDRESS_NUMBER1
Serial.println(loadedNumber1);
Serial.println(loadedNumber2);
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
}