Go Down

Topic: Library for writing to EEPROM (Read 518 times) previous topic - next topic


If you are interested.

I created a library to write strings to the EEPROM. Also works with Node MCU.
Handy for when you want to save Access point SSIDs or Passwords.

It writes data in segments/fields of 20 characters, so the limitation is 20 characters for each setting that you want to save.

You can find the library here --> https://github.com/mailmartinviljoen/eeprm

The library contains 2 example sketches for Arduino and ESP8266.

Just a tip (Not included in the library)
When loading an access SSID and Password you do it like this.

Eeprm Eeprm(0);


Jan 14, 2018, 08:01 pm Last Edit: Jan 14, 2018, 08:03 pm by robtillaart
THanks for sharing

a quick view shows some improvements
Code: [Select]
  if (field==1){field=0;}
  if (field==2){field=20;}
  if (field==3){field=40;}
  if (field==4){field=60;}
  if (field==5){field=80;}
  if (field==6){field=100;}
  if (field==7){field=120;}
  if (field==8){field=140;}
  if (field==9){field=160;}
  if (field==10){field=180;}

could be: field = (field-1)*20;
(note that the function does not check invalid values for field)

another improvement is this loop
Code: [Select]
   for (i=field; i <= field+19; i++)
    Datatoread += char(EEPROM.read(i));

Code: [Select]
   for (i = field; i < field+20; i++)
    int val = EEPROM.read(i);   
    if (val == 126) break;
    Datatoread += char(val);

check your variables:
Datatoread ==> dataToRead  // called camel case, bit easier to read

Last but not least

EEPROM.write(i+field, CurASCIICode);

should become this to minimize write cycles.

Code: [Select]
int val = EEPROM.read(i + field);
if (val != CurASCIICode)
  EEPROM.write(i+field, CurASCIICode);

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)


Thanks for the suggestions ,will most certainly improve it.


Its been updated.

I'm sure more improvements can be made by converting from String to char to int using a more elegant convertion.

String CurCharacter;
  char CharConv;
  int CurASCIICode;
CurCharacter = datatowrite.substring(i,i+1); //Get String character
CharConv = CurCharacter.charAt(0); //Convert string to Char     
CurASCIICode=CharConv; //Convert char to integer (ASCII CODE)

Go Up