Write string to EEPROM

Hello,

I am trying to write a string to EEPROM but I have been having some trouble getting it to work. I've tried using the "write anything", "eepromex", and "EepromUtil" libraries but still can't get it to work. I'm new to Arduino so any help, especially examples, would be really helpful.

Thank you.

thedudeabides_22:
Hello,

I am trying to write a string to EEPROM but I have been having some trouble getting it to work. I’ve tried using the “write anything”, “eepromex”, and “EepromUtil” libraries but still can’t get it to work. I’m new to Arduino so any help, especially examples, would be really helpful.

Thank you.

have you looked here?

EEPROM.write()

break the string into bytes.

Could you please give me an example on how to do this? I haven't actually seen it done in any forum. What would the code look like to break "hello world" into bytes, for example?

thedudeabides_22:
I am trying to write a string to EEPROM but I have been having some trouble getting it to work. I've tried using the "write anything", "eepromex", and "EepromUtil" libraries but still can't get it to work.

What you're trying to do is certainly possible. If your attempt isn't working, post your code and perhaps somebody can explain what you're doing wrong.

I posted a very simple example to do exactly this a couple of days ago.

Hello,

I’m trying to break a string into bytes and then write to EEPROM.

int index = 0;
int test = 1;
float Lat = GPS.latitude;
float Lon = GPS.longitude;
char buffer[10];
String LatString = dtostrf(Lat, 9, 4, buffer);
String LonString = dtostrf(Lon, 9, 4, buffer);
String concatString = String(index) + ", " + String(test) + ", " + String(LatString) + ", " + String(LonString);
byte len = (byte) strlen (concatString );
EEPROM.write(0, len);
for (int i = 0; i < len; i++)
{
EEPROM.write(1 + i, concatString [i]);
}

I am getting the error message "cannot convert ‘String’ to ‘char*’ for argument ‘1’ to ‘size_t strlen(const char*)’.

I think I need concatString to be a ‘char*’, is there a way to convert it? I tried the toCharArray() function but that didn’t seem to work.

Thank you, that example is helpful. Having trouble writing multiple variables to EEPROM though. In the code below, how could I write the variable “dog” and read it into the serial monitor on a separate line?

#include <EEPROM.h>

void setup()
{
  Serial.begin (115200);
  char* fox = "The quick brown fox";
  char* dog = "jumped over the lazy dog";
  byte len = (byte) strlen (fox);
  EEPROM.write(0, len);
  for (int i = 0; i < len; i++)
  {
    EEPROM.write(1 + i, fox [i]);
  }
}

void loop()
{
  byte len = EEPROM.read (0);
  for (int i = 0; i < len; i++)
  {
    Serial.print ( (char)EEPROM.read(1 + i));
  }
  Serial.println ();
  delay (500);  
}

Hi.
I prefer useing this way.
http://playground.arduino.cc/Code/EEPROMWriteAnything

I used this method on a lot of my projects.
Its much easier, and you dont need to write as much code as you should normaly do.

gamecompiler:
Hi.
I prefer useing this way.
http://playground.arduino.cc/Code/EEPROMWriteAnything

I used this method on a lot of my projects.
Its much easier, and you dont need to write as much code as you should normaly do.

A couple of words of warning, however:

  1. You may be tempted to write
 char* fox = "The quick brown fox";
  byte len = (byte) strlen (fox);
  EEPROM_writeAnything (0, len);
  EEPROM_writeAnything (1, fox);

which won't work.
2) Although you may write less source, your generated code may well be larger.

which line did the error occur on

This one ??

byte len = (byte) strlen (concatString );

It looks like you are trying to get the length of a StringOject using the C string function strlen

String is not the same as char ;

if you must use String, look in the reference http://arduino.cc/en/Reference/StringObject and use the .length() method to get the length,
But you will probably run into more problems later if you need to use other functions which expect a C String instead of a String Object
e.g. EEPROM.write(1 + i, concatString ); probably takes a C string, so you'd need to use
toCharArray() and put it into a char array before you send it too the eeprom
Overall you're probably better off just declaring a C string buffer at the start and using C string functions for all the stuff you want to do e.g. like the concatenation
In fact if you look at sprintf() you can probably do it all in one line

Maybe you should look in your other topic on the same subject in this forum.

Mark