Write and read data from EEPROM memory - NODEMCU

  #include <EEPROM.h>


#define EEPROM_SIZE 512

//EEPROM memory in NodeMCU is a kind of memory that does not delete any data even if
//the nodeMCU gets a reset of the power supply. Its like ROM

String myStr = "Hello";

void setup()
{
  Serial.begin(115200);
  EEPROM.begin(EEPROM_SIZE);
  Serial.println();
  writeString(70,"Kriya");
  writeString(140,"Item1,Item2,");
  Serial.println(read_String(70));
}
void loop()
{
 
}

void writeString(int add,String data)
{
  int _size = data.length();
  int i;
  for(i=0;i<_size;i++)
  {
    EEPROM.write(add+i,data[i]);
  }
  EEPROM.write(add+_size,'\0');   //Add termination null character for String Data
  EEPROM.commit();
}


String read_String(int add)
{
  int i;
  char data[100]; //Max 100 Bytes
  int len=0;
  unsigned char k;
  k=EEPROM.read(add);
  while(k != '\0' && len<500)   //Read until null character
  {    
    k=EEPROM.read(add+len);
    data[len]=k;
    len++;
  }
  data[len]='\0';
  return String(data);
}

Comments

Popular posts from this blog

Husband's Affair with Step Daughter Ends in Grisly Murder (True Crime Documentary)