Saving settings
The library can save values in the ESP32's flash memory. They survive restarts and power cuts, which is useful for settings such as a threshold, a mode or the last state of an output.
// Save
iot.saveString("device_name", "Sensor-01");
iot.saveInt ("threshold", 30);
iot.saveFloat ("last_lat", 23.0225);
iot.saveBool ("alarm_on", false);
// Load: the second argument is returned if the key was never saved
String name = iot.getString("device_name", "Unknown");
int thr = iot.getInt ("threshold", 25);
float lat = iot.getFloat ("last_lat", 0.0);
bool alarm = iot.getBool ("alarm_on", false);
// Manage
iot.hasKey("threshold"); // true if the key exists
iot.removeKey("threshold"); // delete one key
iot.clearAllData(); // delete everything you saved
Key names can be at most 15 characters.
Restore outputs after a restart
Save a setting when the user changes it, and restore it at boot:
bool relayOn = false;
void onRelay(bool on) {
relayOn = on;
digitalWrite(RELAY_PIN, on);
iot.saveBool("relay_on", on);
}
void setup() {
pinMode(RELAY_PIN, OUTPUT);
relayOn = iot.getBool("relay_on", false);
digitalWrite(RELAY_PIN, relayOn); // correct state straight away, even offline
iot.onSwitch("V8", onRelay);
iot.begin();
}
Flash memory wears out after very many writes. Save when a value changes, not on every loop.