Library for writing to EEPROM

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);
APSSID=Eeprm.readdata(1);
APPASS=Eeprm.readdata(2);
WiFi.softAP(APSSID.c_str(),APPASS.c_str());

THanks for sharing

a quick view shows some improvements

  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

   for (i=field; i <= field+19; i++)
   {    
    if(EEPROM.read(i)==126){break;}
    Datatoread += char(EEPROM.read(i));
   }
   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.

int val = EEPROM.read(i + field);
if (val != CurASCIICode)
{
  EEPROM.write(i+field, CurASCIICode);
}

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)