Go Down

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

MartinViljoen

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.

Quote
Eeprm Eeprm(0);
APSSID=Eeprm.readdata(1);
APPASS=Eeprm.readdata(2);
WiFi.softAP(APSSID.c_str(),APPASS.c_str());

robtillaart

#1
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++)
   {   
    if(EEPROM.read(i)==126){break;}
    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)

MartinViljoen

Thanks for the suggestions ,will most certainly improve it.

MartinViljoen

Its been updated.

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

Quote
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