EEPROM Question


#1

If I reload the dashboard, the values are update and sending again to my device or not.
Cause I see that in the serial monitor when I reload the dashboard I see a lot of “Writing bytes: 16[ok]”.

And it would be bad for my microcontroller coz I’m using EEPROM to insert a set point. this is part of the code.

#define _DEBUG_
#include <ESP8266WiFi.h>
#include <ThingerWebConfig.h>
#include <EEPROM.h>
#define LedStrip D1
//#define LedStrip 4

unsigned long interval1 = 1000;
unsigned long interval2 = 2000;
unsigned long previousMillis1;
unsigned long previousMillis2;
/*
 * Define a value for initial flash prog
 */
byte stPointLDR = 40;
int TimeFade = 30;
int TimeMax = 30;
float TimeMul = 8.5;

/*
 * Initial states for our variables
 */

int ledState = LOW; // Initial state from led mode
int SensorState, LDRVal;
bool SelectModo = false;
bool WifiChange = false;
int brightness = 0;
int SPLDR;

ThingerWebConfig thing;
void setup() {

  EEPROM.begin(512);
  Serial.begin(115200);
  stPointLDR = EEPROM.read(0);
  TimeFade = EEPROM.read(1);
  TimeMul = EEPROM.read(2);
  TimeMax = EEPROM.read(3);
  
  previousMillis1 = millis();
  previousMillis2 = millis();


  pinMode (LedStrip, OUTPUT);
  pinMode (LedModoAuto, OUTPUT);
  pinMode(PirSensor, INPUT);
  pinMode (A0, INPUT);
  pinMode (LedStrip, INPUT);
  analogWriteFreq(500);
  analogWriteRange(100);

  digitalWrite(LedStrip, HIGH);

  //analogWrite(LedStrip, 0);    // Set analogWrite to begin PWM

  thing["Intesidad"] << inputValue(brightness);
  thing["modo"] << inputValue(SelectModo);
  thing["wifi"] << inputValue(WifiChange);
  thing["StPointLDR"] << inputValue(stPointLDR, {
    EEPROM.write(0, stPointLDR);
    EEPROM.commit();
  });
  thing["TimeFade"] << inputValue(TimeFade, {
    EEPROM.write(1, TimeFade);
    EEPROM.commit();
  });
  thing["TimeMul"] << inputValue(TimeMul, {
    EEPROM.write(2, TimeMul);
    EEPROM.commit();
  });
  thing["TimeMax"] << inputValue(TimeMax, {
    EEPROM.write(3, TimeMax);
    EEPROM.commit();
  });

  
  thing["Estado"] >> [] (pson & out) {
    out = SelectModo;
  };
  //Reading value in memory and in value
  thing["SP"] >> [] (pson & out) {
    out = stPointLDR;
  };
  thing["SET"] >> [] (pson & out) {
    out = SPLDR;
  };
  
  //Reading the LDR value
  const int analogInPin = A0;
  thing["ReadLDR"] >> [] (pson & out) {
    int sensorValue = analogRead(analogInPin);
    out = sensorValue;
  };
  
}

void loop() {


  thing.handle();
  unsigned long currentMillis = millis();
  if ((unsigned long)(currentMillis - previousMillis1) >= interval1)
  {
    StatusWifi();
    WebServer();
    intensidad();
    previousMillis1 = millis();
  }
  if ((unsigned long)(currentMillis - previousMillis2) >= interval2)
  {

    ledEstadoAuto();
    previousMillis2 = millis();
  }
}

void WebServer() {
  switch (WifiChange) {
    case false:
      break;
    case true:
      WifiChange = false;
      WiFi.disconnect();
      
      break;
  }
}
void intensidad() {
  switch (SelectModo)
  {
    case false:
      automatico();
      break;
    case true:
      digitalWrite(LedModoAuto, HIGH);
      analogWrite(LedStrip, 100 - (brightness * 1));

      switch (brightness)
      {
        case 0:
          analogWrite(LedStrip, 100);
          break;
        case 1:
          analogWrite(LedStrip , 0);
          break;
        case 2:
          break;
      }
      break;
    case 2:
      break;
  }
}

void ledEstadoAuto() {
  if (ledState == LOW) {
    ledState = HIGH;
  } else {
    ledState = LOW;
  }
  digitalWrite(LedModoAuto, ledState);
}
int contador;
void automatico() {

 //Mode auto
}

void setFocoLed(float porcentaje) {
 
}

void StatusWifi() {
  if (WiFi.status() != WL_CONNECTED)
  {
    automatico();
  }

}

#2

As you said, it is bad for your microcontroller

I think that maybe something like this can be implemented, but I dont know if you can call the EEPROM always, I mean have this memory access active all the time, in terms of ram memory available in the microcontroller

Kind of this for read

void readConfig()
{
conf1=EEPROM.read(1);
conf2=EEPROM.read(2);
...
 }

And for save

void saveConf()
{
  if (conf1!=EEPROM.read(1)
   {
    EEPROM.write(1, conf1);
   }
  
  if (conf2!=EEPROM.read(2)
   {
    EEPROM.write(2, conf2);
   }
  ...
  EEPROM.commit();

}

And to call the saving function anywhere in the code, even in thinger thing…

if (conf1!=EEPROM.read(1) || conf2!=EEPROM.read(2)...)
{
saveConf();
}

And of course you must call the readConf() function in setup to load the last saved configuration when the microncontroller starts

Hope this helps