Issue with Preferences.h in Nodemcu

Hello experts,

I am working on a project based on ESP8266 nodemcu with preferences library by 'vshymanskyy'.

I am saving some values in the preferences & reading them back. The values are sent & viewed by user on a Webserver.

The issue with me is the ESP is forgetting the value of just one key after restarting the ESP & other key values are not forgotten. The value is remembered when ESP is not restarted but after restarting, it forgots only that one value.

Why it is forgetting only that value & not other?

My code has only one preference name (domain).

What should I try doing to solve this?

Also let me know can I/ should I use EEPROM.h with PREFERENCES.h for storing values using both in same project?

Thanks!!!

--SRJ

The problem is probably in the sketch that you have not posted, but how would we know ?

Why do you need to use anything other than the EEPROM.h library bundled with the esp8266 core.

Maybe because the use of EEPROM is deprecated by Espressif in favour of Preferences

1 Like

The issue is with SelBtnHTML Key in getRelStBtnPref() & RelStBtnHandler() which is at the bottom of the code.

#include <ESP8266WiFi.h>
#include "ESPAsyncWebServer.h"
#include "FS.h"
#include <DNSServer.h>
#include <Preferences.h>
#include <RTClib.h>
#include <Wire.h>
#include "var.h"
#include <ArduinoJson.h>

RTC_DS1307 rtc;
int relayState = 0;
DNSServer dnsServer;
AsyncWebServer server(80);
Preferences preferences;
DateTime now;

// Replace with your network credentials
const char *ssid = "rgSDTq3w";
const char *password = "frdsfgdg";


void setup() {
  Serial.begin(115200);
  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    while (1) delay(10);
  }
  if (!SPIFFS.begin()) {
    Serial.println("An Error has occurred while mounting SPIFFS");
    return;
  }





  IPAddress ip(192, 168, 0, 8);
  IPAddress gateway(192, 168, 0, 1);
  IPAddress subnet(255, 255, 255, 0);

  WiFi.softAPConfig(ip, gateway, subnet);
  WiFi.mode(WIFI_AP);
  if (!WiFi.softAP(ssid, password)) {
    Serial.println("Failed to set up SoftAP");
    return;
  }

  delay(100);
  Serial.println("Access Point IP address: " + WiFi.softAPIP().toString());


  // Start DNS server
  dnsServer.start(53, "*", WiFi.softAPIP());


  server.onNotFound([](AsyncWebServerRequest *request) {
    String dta = HmepgHTML(String(now.hour()), String(now.minute()), String(now.second()), String(now.dayOfTheWeek()), String(now.day()), String(now.month()), String(now.year()), relayState);
    request->send(200, "text/html", dta);
  });

  // Route for Home page
  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
    //HmepgHTML(Hr, Min, Second, Day, Date, Month, Year, Relay State)
    String dta = HmepgHTML(String(now.hour()), String(now.minute()), String(now.second()), String(now.dayOfTheWeek()), String(now.day()), String(now.month()), String(now.year()), relayState);
    request->send(200, "text/html", dta);
  });

  // Route for Home page CSS
  server.on("/hmStyle.css", HTTP_GET, [](AsyncWebServerRequest *request) {
    Serial.println("This ran!!!!!!!!!!!!!!!!!!!!!!!");
    request->send(SPIFFS, "/style.css", "text/css");
    //request->send(200, "text/css", "dta0");
  });

  // Route for Home page JS
  server.on("/HMscript.js", HTTP_GET, [](AsyncWebServerRequest *request) {
    String dta0 = HmJS(getRelStBtnPref());
    request->send(200, "application/javascript", dta0);
  });


  // Route for Sys page
  server.on("/Sys", HTTP_GET, [](AsyncWebServerRequest *request) {
    request->send(SPIFFS, "/Sys/index.html");
  });

  // Route for Sys page CSS
  server.on("/SysStyle.css", HTTP_GET, [](AsyncWebServerRequest *request) {
    request->send(SPIFFS, "/Sys/style.css");
  });

  // Route for AddEve page
  server.on("/AddEve", HTTP_GET, [](AsyncWebServerRequest *request) {
    request->send(SPIFFS, "/AddEve/index.html");
  });

  // Route for AddEve page CSS
  server.on("/AddEveStyle.css", HTTP_GET, [](AsyncWebServerRequest *request) {
    request->send(SPIFFS, "/AddEve/style.css");
  });

  // Route for ManEve page
  server.on("/manEve", HTTP_GET, [](AsyncWebServerRequest *request) {
    request->send(SPIFFS, "/ManEvn/manEve.html");
  });

  // Route for ManEve page CSS
  server.on("/manEveCss.css", HTTP_GET, [](AsyncWebServerRequest *request) {
    request->send(SPIFFS, "/ManEvn/manEveCss.css");
  });



  server.on("/dtTmUpdateRTC", HTTP_GET, dtTmUpdateRTCHandler);

  server.on("/RelStBtn", HTTP_GET, RelStBtnHandler);

  server.on("/ESecHmDta", HTTP_GET, ESecHmDtaHandler);

  server.on("/RelStBtn", HTTP_GET, RelStBtnHandler);


  // Start server
  server.begin();
}


void loop() {
  now = rtc.now();
  dnsServer.processNextRequest();
  SrlDbg();
}



void dtTmUpdateRTCHandler(AsyncWebServerRequest *request) {
  // Get formatted time from request body
  String formatted_time_Up_RTC = request->arg("formatted_time");
  Serial.println("time: " + formatted_time_Up_RTC);

  // Send response
  request->send(200, "text/plain", "");

  // Extract year, month, day, hour, minute, second from formatted time
  int year = formatted_time_Up_RTC.substring(0, 4).toInt();
  int month = formatted_time_Up_RTC.substring(5, 7).toInt();
  int day = formatted_time_Up_RTC.substring(8, 10).toInt();
  int hour = formatted_time_Up_RTC.substring(11, 13).toInt();
  int minute = formatted_time_Up_RTC.substring(14, 16).toInt();
  int second = formatted_time_Up_RTC.substring(17, 19).toInt();

  // Adjust RTC time
  DateTime dt(year, month, day, hour, minute, second);
  rtc.adjust(dt);

  // Print adjusted time
  Serial.println(year);
  Serial.println(month);
  Serial.println(day);
  Serial.println(hour);
  Serial.println(minute);
  Serial.println(second);

  Serial.println("RTC time set successfully");
}

void RelStBtnHandler(AsyncWebServerRequest *request) {
  // Get formatted time from request body
  String Btn = request->arg("Option");
  Serial.println("Button Option: " + Btn);

  // Send response 
  request->send(200, "text/plain", "");

  preferences.begin("Events", false);
  preferences.putString("SelBtnHTML", Btn);

  String tester = preferences.getString("SelBtnHTML", "na");
  Serial.println("Button Value set to: " + tester);

  preferences.end();
}


void ESecHmDtaHandler(AsyncWebServerRequest *request) {

  String H = String(now.hour());
  String M = String(now.minute());
  String Dy = String(now.dayOfTheWeek());
  String Dt = String(now.day());
  String Mnt = String(now.month());
  String Sec = String(now.second());
  int S;



  Mnt = monthNumberToName(Mnt.toInt());
  M = padWithZero(M.toInt());
  Sec = padWithZero(Sec.toInt());
  Dy = daysOfTheWeek[Dy.toInt()];

  int Hint = H.toInt();

  String AP = "AM";
  String St = "ON";

  if (S == 0) {
    St = "OFF";
  }


  if (Hint >= 12) {
    AP = "PM";
    if (Hint > 12) {
      Hint -= 12;
    }
  }

  if (Hint == 0) {
    Hint = 12;
  }

  H = String(Hint);
  H = padWithZero(H.toInt());




  // Construct a JSON object
  StaticJsonDocument<300> jsonDoc;  // Adjust the size as needed
  jsonDoc["date"] = Dt;
  jsonDoc["month"] = Mnt;
  jsonDoc["day"] = Dy;
  jsonDoc["hour"] = H;
  jsonDoc["APM"] = AP;
  jsonDoc["minute"] = M;
  jsonDoc["seconds"] = Sec;
  jsonDoc["Stat"] = St;
  jsonDoc["BtnSt"] = "1";
  jsonDoc["BtnSt"] = getRelStBtnPref();

  // Serialize the JSON object to a string
  String jsonString;
  serializeJson(jsonDoc, jsonString);

  // Send the JSON response
  request->send(200, "application/json", jsonString);
}





String getRelStBtnPref() {

  preferences.begin("Events", false);
  String tester = preferences.getString("SelBtnHTML", "0");
  Serial.println("Button Value readed as: " + tester);

  preferences.end();

  return tester;
}

But only for the esp32. The esp8266 is not using the Preferences.h designed for the esp32.

with preferences library by 'vshymanskyy'.

I am using this library.

Update.....

There is one more issue with this......

The ESP is forgetting all the data (files) uploaded in the SPIFFS after power cut...

I am using ESP SKETCH DATA UPLOAD TOOL in Arduino 1.8.19 version.

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