Preferences.h data not storing new value during loop

I am having issues where I can write preferences during the setup function and things save no problem but during the loop it never saves the changes after the preferences end and during a reboot or power cycle. I have tried the sketch on multiple esp32 boards and board styles with the same result. Everything I am finding in forums and posts say it should work. I have included the code that I use to write the Preferences during the loop and the serial output. The original value was "9876" and attempted to write "5468" but the value does not hold once the preferences are closed. This only seems to be the case during the loop writes.

Function used during loop to write new value:

void writeSysPref(const char* key, String value, bool reboot) {
  preferences.begin("system", false);
  preferences.remove(key);
  preferences.putString(key, value);
  delay(1000);
  preferences.end();

  preferences.begin("system", true);
  Serial.println(String(key) + ": " + preferences.getString(key));
  preferences.end();
  initSysPrefs();

  if (reboot) {
    delay(100);
    ESP.restart();
  }
}

Output (9876 was written during setup and was the original value, attempted to write 5468 during loop):

Received: spw:brddnshostwc:5468
brddnshostwc
5468
brddnshostwc: 9876

post a full code demonstrating the issue

what is initSysPrefs(); ??

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

Seems your parsing is going wrong you need to correct the last line in this if

Into
incomingString.replace(String("spwr:"), "");

omg :expressionless: ... I have been staring at this for a week. Thank you. [Insert I'm an idiot + curse words here]

:slight_smile:

Not an idiot - just happened to everyone of us. Staring at the code for too long and you don’t see the obvious anymore - a fresh pair of eyes often helps

Have fun !

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.