This is some demonstration code and it actually confuses me more than my original code. You can see in the results that if I initiate a reboot on the next loop from the preferences update the read of the value still shows the old value but if I just change the value without initiating the reboot it updates the value and the readback reads the updated value. My head hurts on this one...
Code:
#include "Arduino.h"
#include <ESPmDNS.h>
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#include <FS.h>
#include <ArduinoJson.h>
#include <Preferences.h>
#include <nvs_flash.h>
Preferences preferences;
String brddnshostwc;
bool rebootNextLoop = false;
bool initSysPrefNextLoop = false;
void setup() {
Serial.begin(115200);
Serial.println("Serial Started");
initSysPrefs();
if (String("") == brddnshostwc)
writeSysPref("brddnshostwc", "1234", false);
}
void loop() {
if (initSysPrefNextLoop) {
initSysPrefs();
initSysPrefNextLoop = false;
}
if (rebootNextLoop) {
Serial.println("Rebooting");
delay(5000);
ESP.restart();
}
// Process Serial Input
if (Serial.available() > 0) {
// read the incoming string:
String incomingString = Serial.readString();
incomingString.trim();
Serial.println(String("incomingString (Serial): ") + incomingString);
if (incomingString.startsWith(String("spwr:"))) {
Serial.println(String("action: ") + "spwr");
incomingString.replace(String("spw:"), "");
int splitLoc = incomingString.indexOf(":");
String key = incomingString.substring(0, splitLoc);
Serial.println(String("key: ") + key);
String value = incomingString.substring(splitLoc + 1);
Serial.println(String("value: ") + value);
writeSysPref(key.c_str(), value, true);
}
if (incomingString.startsWith(String("spw:"))) {
Serial.println(String("action: ") + "spw");
incomingString.replace(String("spw:"), "");
int splitLoc = incomingString.indexOf(":");
String key = incomingString.substring(0, splitLoc);
Serial.println(String("key: ") + key);
String value = incomingString.substring(splitLoc + 1);
Serial.println(String("value: ") + value);
writeSysPref(key.c_str(), value, false);
}
if (String("reboot") == incomingString) {
rebootNextLoop = true;
}
}
}
void initSysPrefs() {
Serial.println("initSysPrefs(): Started");
// Initialize Preferences: system
preferences.begin("system", true);
brddnshostwc = preferences.getString("brddnshostwc", "");
Serial.println(String("(initSysPrefs() - Variable Set [RO-Pref]) ") + String("brddnshostwc") + ": " + brddnshostwc);
preferences.end();
}
void writeSysPref(const char* key, String value, bool reboot) {
Serial.println("writeSysPref(): Started");
preferences.begin("system", false);
preferences.remove(key);
preferences.putString(key, value);
delay(1000);
preferences.end();
preferences.begin("system", true);
Serial.println(String("(writeSysPref() - Post Write[RO-Pref]) ") + String(key) + ": " + preferences.getString(key));
preferences.end();
initSysPrefNextLoop = true;
if (reboot) {
rebootNextLoop = true;
}
}
Serial Results:
ets Jun 8 2016 00:22:57
rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1044
load:0x40078000,len:10124
load:0x40080400,len:5856
entry 0x400806a8
Serial Started
initSysPrefs(): Started
(initSysPrefs() - Variable Set [RO-Pref]) brddnshostwc: 7890
incomingString (Serial): spw:brddnshostwc:6549
action: spw
key: brddnshostwc
value: 6549
writeSysPref(): Started
(writeSysPref() - Post Write[RO-Pref]) brddnshostwc: 6549
initSysPrefs(): Started
(initSysPrefs() - Variable Set [RO-Pref]) brddnshostwc: 6549
incomingString (Serial): reboot
Rebooting
ets Jun 8 2016 00:22:57
rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1044
load:0x40078000,len:10124
load:0x40080400,len:5856
entry 0x400806a8
Serial Started
initSysPrefs(): Started
(initSysPrefs() - Variable Set [RO-Pref]) brddnshostwc: 6549
incomingString (Serial): spwr:brddnshostwc:9876
action: spwr
key: spwr
value: brddnshostwc:9876
writeSysPref(): Started
(writeSysPref() - Post Write[RO-Pref]) spwr: brddnshostwc:9876
initSysPrefs(): Started
(initSysPrefs() - Variable Set [RO-Pref]) brddnshostwc: 6549
Rebooting
ets Jun 8 2016 00:22:57
rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1044
load:0x40078000,len:10124
load:0x40080400,len:5856
entry 0x400806a8
Serial Started
initSysPrefs(): Started
(initSysPrefs() - Variable Set [RO-Pref]) brddnshostwc: 6549