Storing "String" data using EEPROM 'Write' or 'Put' command

I am new to this forum, so please forgive me if I am not following generally established norms on this forum.
I am stuck up on using EEPROM command 'Write' / 'Put' for referencing mobile number through which I received the SMS (i.e. number received through "sms.remoteNumber"). I want to store this mobile number using EEPROM 'Write' command for future use in the program. The intention is that even if my Arduino goes through power recycle, I can use stored number for sending other messages for future. I am using Uno & GSM shield in my project.
Has anybody programmed Uno for this kind of requirement? Appreciate if you can share the code or any help in this regard.

Hi

This is how I write strings to EEPROM:

#define E_MOBILE_330 3819

EPSW(E_MOBILE_330,"MOBILE");


void EPSW(int p_start_posn, char p_string[] ) { //EEPROMStringWrite
//Write a string to EEPROM and terminate it with a NULL
//We read first and do not rewrite bytes/chars that are unchanged

  for (int l_posn = 0; l_posn < (int) strlen(p_string); l_posn ++) {
	byte l_byte = (byte) p_string[l_posn];
	byte l_read = EEPROM.read(p_start_posn + l_posn);
	if (l_read != l_byte) {
	  EEPROM.write(p_start_posn + l_posn, l_byte);
	}
  }
  //write the NULL termination
  if (EEPROM.read(p_start_posn + strlen(p_string)) != 0)
	EEPROM.write(p_start_posn + strlen(p_string), 0);
  //
} //EPSW

This is writing the string "MOBILE" (which is the 330th string I have stored in EEPROM) into EEPROM starting at byte 3819.

I can retrieve the value associated with the define E_MOBILE_330 this way:

String l_mobile = EPSR(E_MOBILE_330);

String EPSR(int p_start_posn) { //EEPROMStringRead
//Read a NULL terminated string from EEPROM
//Only strings up to 128 bytes are supported
  byte l_byte;

  //Count first, reserve exact string length and then extract
  int l_posn = 0;
  while (true) {
	l_byte = EEPROM.read(p_start_posn + l_posn);
	if (l_byte == 0) {
	  break;
	}
	l_posn ++;
  }

  //Now extract the string
  String l_string = "";
  l_string.reserve(l_posn + 1);
  char l_char;
  l_posn = 0;
  while (true) {
	l_byte = EEPROM.read(p_start_posn + l_posn);
	if (l_byte == 0) {
	  break;
	}
	l_char = (char) l_byte;
	l_string += l_char;
	l_posn ++;
	if (l_posn == 128)
	  break;
	//
  }
  return l_string;
} //EPSR

I hope this helps.

Cheers

Catweazle NZ

Thanks Catweazle!!! I think, this should work for me as well. I will revert back after my try.

Cheers,
Lambodar

Hi

This is i have created for reading and writing string in EEPROM. i can able to read and write the values from EEPROM. here i am trying to store the values into one variable, but i can not able to store the values. Please help me , i am new for this .

#include <EEPROM.h>
void setup()
{
Serial.begin (9600);
char* siteid = "00000001 BTS001 BSC001 40 30";
byte len = (byte) strlen (siteid);
EEPROM.write(0, len);
for (int i = 0; i < len; i++)
{
EEPROM.write(1 + i, siteid );

  • }*
    }
    void loop()
    {
  • for (int i = 0; i < 8; i++)*
  • {*
  • char val=Serial.print ( (char)EEPROM.read(1 + i));*
  • }*
  • for (int i = 9; i < 15; i++)*
  • {*
  • char val2=Serial.print ( (char)EEPROM.read(1 + i));*
  • }*
  • for (int i = 17; i < 22; i++)*
  • {*
  • char val3=Serial.print ( (char)EEPROM.read(1 + i));*
  • }*
  • for (int i = 23; i < 25; i++)*
  • {*
  • char val4=Serial.print ( (char)EEPROM.read(1 + i));*
  • }*
  • for (int i = 26; i < 28; i++)*
  • {*
  • char val5=Serial.print ( (char)EEPROM.read(1 + i));*
  • }*
  • delay (500); *
    }

i can able to read and write the values from EEPROM.

OK. You should take a look at the functions EEPROM.put() and EEPROM.get() which are now part of EEPROM.h and can be used for multi byte write and read.

here i am trying to store the values into one variable, but i can not able to store the values

Can you please explain what you are trying to do, and how you plan to use the result.