I'm using the WiFiManager library with an ESP32 and trying to use checkboxes to store configuration values. My issue is that the checked values are not retained after the ESP32 restarts.
The code is:
#include <WiFiManager.h>
#include <EEPROM.h>
#define EEPROM_SIZE 512
#define EEPROM_SEND_NOTIFICATION_ADDRESS 200
#define EEPROM_SOUND_ALARM_ADDRESS 201
// Variables for configuration
bool sendNotification = true; // Notification toggle
bool soundAlarm = true; // Sound alarm toggle
// Create a buffer for the custom HTML checkboxes
char customHtml_checkbox_sendNotification[50] = "type=\"checkbox\"";
char customHtml_checkbox_soundAlarm[50] = "type=\"checkbox\"";
void setup() {
Serial.begin(115200);
// Check if sendNotification should be checked
if (sendNotification) {
strcat(customHtml_checkbox_sendNotification, " checked");
}
// Check if soundAlarm should be checked
if (soundAlarm) {
strcat(customHtml_checkbox_soundAlarm, " checked");
}
WiFiManager wifiManager;
// Create WiFiManagerParameter objects for custom parameters
WiFiManagerParameter custom_sendNotification_checkbox("sendNotification", "Enable Notification", "T", 2, customHtml_checkbox_sendNotification, WFM_LABEL_AFTER);
WiFiManagerParameter custom_soundAlarm_checkbox("soundAlarm", "Enable Sound Alarm", "T", 2, customHtml_checkbox_soundAlarm, WFM_LABEL_AFTER);
// Add parameters to WiFiManager
wifiManager.addParameter(&custom_sendNotification_checkbox);
wifiManager.addParameter(&custom_soundAlarm_checkbox);
wifiManager.autoConnect("ConfigAP");
// Retrieve settings after configuration
sendNotification = (strncmp(custom_sendNotification_checkbox.getValue(), "T", 1) == 0);
soundAlarm = (strncmp(custom_soundAlarm_checkbox.getValue(), "T", 1) == 0);
// Print the results
Serial.println("Send Notification: " + String(sendNotification));
Serial.println("Sound Alarm: " + String(soundAlarm));
// Load settings from EEPROM
loadSettings();
// Save updated settings to EEPROM
saveSettings();
}
void loop() {
}
void saveSettings() {
EEPROM.begin(EEPROM_SIZE);
// Debugging: Print the values before saving
Serial.println("Saving settings...");
Serial.println("Send Notification: " + String(sendNotification));
Serial.println("Sound Alarm: " + String(soundAlarm));
// Save sendNotification and soundAlarm
EEPROM.write(EEPROM_SEND_NOTIFICATION_ADDRESS, sendNotification ? 1 : 0);
EEPROM.write(EEPROM_SOUND_ALARM_ADDRESS, soundAlarm ? 1 : 0);
EEPROM.commit();
EEPROM.end();
Serial.println("Settings saved.");
}
void loadSettings() {
EEPROM.begin(EEPROM_SIZE);
sendNotification = EEPROM.read(EEPROM_SEND_NOTIFICATION_ADDRESS) == 1;
soundAlarm = EEPROM.read(EEPROM_SOUND_ALARM_ADDRESS) == 1;
// Debugging: Print the loaded values
Serial.println("Settings loaded:");
Serial.println("Send Notification: " + String(sendNotification));
Serial.println("Sound Alarm: " + String(soundAlarm));
EEPROM.end();
}
The code then adds " checked" to both, since they're both true, and creates the WiFiManagerParameter objects.
It does all of this before actually reading the stored values from EEPROM, which doesn't happen until the bottom of setup(). Could you explain how the updated values make it back to the checkboxes please?