How to save string from serial into EEPROM

Hello there I am trying to save a string from serial into the EEPROM memory of the Arduino. But I am not able to put the string into EEPROM. Here is my code:

#include <EEPROM.h>
String hi = " ";
  
void Save(int address, String Mon){
  byte len = Mon.length();
  EEPROM.write(address,len);

  for (int i = 0; i < len; i++) {
    EEPROM.write(address + 1 + i, Mon[i]);
  }
}

String give(int address){
  int len = EEPROM.read(address);
  char data[len + 1];

  for (int i = 0; i < len; i++){
    data[i] = EEPROM.read(address + 1 + i);
  }
  data[len] = '\0';

  return String(data);
}


void setup() {
  
  Serial.begin(9600);
  hi = Serial.readString();
  if(Serial.available())
  Save(16, hi);
  Serial.println(give(16));
   }
  

void loop() {
 

  }

Can you tell me what am I doing wrong and how can I correct it

Don't use the String class. Use C-strings (null terminated character arrays).

Use the methods of the serial input basics tutorial to receive the serial data into a C-string (string). The tutorial shows reliable and robust ways to receive and parse serial data.

To write to EEPROM use the put() function. To read the data back use the get() function.

Examples to illustrate.

Save string from serial monitor to EEPROM:

#include <EEPROM.h>

const byte numChars = 32;  // set to the size you need
char receivedChars[numChars];   // an array to store the received data

boolean newData = false;

void setup()
{
   Serial.begin(115200);
   Serial.println("<Arduino is ready>  Enter your string");
}

void loop()
{
  recvWithEndMarker();
  showNewData();
   if (newData)
   {
      saveToEEPROM();
      newData = false;
   }
}

void saveToEEPROM()
{
  EEPROM.put(0, receivedChars);
  Serial.println("written to EEPROM");  
}

void recvWithEndMarker()
{
   static byte ndx = 0;
   char endMarker = '\n';
   char rc;

   while (Serial.available() > 0 && newData == false)
   {
      rc = Serial.read();
      if(rc == '\r') // ignore carruage return
      {
         return;
      }
      if (rc != endMarker)
      {
         receivedChars[ndx] = rc;
         ndx++;
         if (ndx >= numChars)
         {
            ndx = numChars - 1;
         }
      }
      else
      {
         receivedChars[ndx] = '\0'; // terminate the string
         ndx = 0;
         newData = true;
      }
   }
}

void showNewData()
{
   if (newData == true)
   {
      Serial.print("This just in ... ");
      Serial.println(receivedChars);      
   }
}

Read string back from EEPROM:

#include <EEPROM.h>

char buffer[32];

void setup()
{
   Serial.begin(115200);
   Serial.print("read from EEPROM >>> ");
   Serial.println(EEPROM.get(0, buffer));
}

void loop()
{
  
}

Examples tested, successfully on real hardware (Uno).

1 Like

Several issues.

First issue:

Serial.readString() has a one-second timeout. If you don't send a message within one second of starting up, your 'hi' will be empty. Try waiting for the first character to arrive:

  Serial.begin(9600);
  delay(200);
  while (!Serial.available()) {} // Wait for a character
  hi = Serial.readString();

Second issue:

Serial.readString() reads until the input buffer has been empty for one second. Immediately after that, you check if the buffer is empty and only save the message if it is NOT empty. But it IS empty so you don't store the message. Try storing the message regardless of the contents:

  hi = Serial.readString();
  Save(16, hi);
  Serial.print("Saved: ");
  Serial.println(hi);
  Serial.print("Retreived: ");
   Serial.println(give(16));
}

All together:

void setup()
{
  Serial.begin(9600);
  delay(200);

  while (!Serial.available()) {} // Wait for a character
  hi = Serial.readString();
  Save(16, hi);
  Serial.print("Saved: ");
  Serial.println(hi);

  Serial.print("Retreived: ");
  Serial.println(give(16));
}

Sorry for replying late i was trying out what you guys had suggested, but I was not able to find the solution and messed up everything. Here is my (new)

`#include <EEPROM.h>
#include "RTClib.h"
const int buzzar = 12;
RTC_DS3231 rtc;

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
String hi;
String SB = "Short";
String LB = "Long";
String hello = "";
String Mon = "  ";
String Tue = "  ";
String Wed = "  ";
String Thu = "  ";
String Fri = "  ";
String Sat = "  ";
String Sun = "  ";
String Time;
String Time1;
String Time2 =":";
String Time3;

int one = 1;
int two = 2;
int three = 3;
int four = 4;
int five = 5;
int six = 6;
int seven = 7;

void Save(int address, String hello){
  byte len = hello.length();
  EEPROM.write(address,len);

  for (int i = 0; i < len; i++) {
    EEPROM.write(address + 1 + i, hello[i]);
  }
}

String give(int address){
  int len = EEPROM.read(address);
  char data[len + 1];

  for (int i = 0; i < len; i++){
    data[i] = EEPROM.read(address + 1 + i);
  }
  data[len] = '\0';

  return String(data);
}

void Read(){
  while(!Serial.available()){}
  hi = Serial.readString();
}
void setup() {
  
  Serial.begin(9600);
  Read();
  DateTime now = rtc.now();
  pinMode(12, OUTPUT);
  #ifndef ESP8266
  while (!Serial); 
#endif

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    Serial.flush();
    while (1) delay(10);
  }

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, let's set the time!");
    
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

  }
  
}

void loop() {
 while(!Serial.available()){}
  hi = Serial.readString();
  Serial.println(hi);
  timings();
}

void timings(){
  DateTime now = rtc.now();
   Time = now.hour();
   Time1 = now.minute();
   Time3 = (Time + Time2 + Time1);
   
  if(hi.startsWith("Monday")){ 
  Mon = hi;
  Save(15, hi);
  Serial.print("yes");
    }
  if(hi.startsWith("Tuesday")){ 
  Tue = hi;
  Save(16, hi);
    }
  if(hi.startsWith("Wednesday")){ 
  Wed = hi;
  Save(17, hi);
    }
  if(hi.startsWith("Thursday")){ 
  Thu = hi;
  Save(18, hi);
    }
  if(hi.startsWith("Friday")){ 
  Fri = hi;
  Save(19, hi);
    }
  if(hi.startsWith("Saturday")){ 
  Sat = hi;
  Save(20, hi);
    }  
  if(hi.startsWith("Sunday")){ 
  Sun = hi;
  Save(21, hi);
    }
  
  
  
  if(now.dayOfTheWeek() == one){ 
    if(give(15).indexOf(Time3) != -1 && give(15).indexOf(SB) != -1){
      digitalWrite(12, HIGH);
      delay(500);
      digitalWrite(12, LOW);
    }
      if(give(15).indexOf(Time3) != -1 && give(15).indexOf(LB) != -1){
        digitalWrite(12, HIGH);
        delay(1000);
        digitalWrite(12, LOW);        
      }
    }
  
    if(now.dayOfTheWeek() == two){ 
      Serial.println(give(16));
    if(give(16).indexOf(Time3) != -1){
      digitalWrite(12, HIGH);
      Serial.println("yep");
      delay(500);
      digitalWrite(12, LOW);
    }
      if(give(16).indexOf(Time3) != -1 && give(16).indexOf(LB) != -1){
        digitalWrite(12, HIGH);
        delay(1000);
        digitalWrite(12, LOW);        
      }
    }
  
    if(now.dayOfTheWeek() == three){ 
    if(give(17).indexOf(Time3) != -1 && give(17).indexOf(SB) != -1){
      digitalWrite(12, HIGH);
      delay(500);
      digitalWrite(12, LOW);
    }
      if(give(17).indexOf(Time3) != -1 && give(17).indexOf(LB) != -1){
        digitalWrite(12, HIGH);
        delay(1000);
        digitalWrite(12, LOW);        
      }
    }
  
    if(now.dayOfTheWeek() == four){ 
    if(give(18).indexOf(Time3) != -1 && give(18).indexOf(SB) != -1){
      digitalWrite(12, HIGH);
      delay(500);
      digitalWrite(12, LOW);
    }
      if(give(18).indexOf(Time3) != -1 && give(18).indexOf(LB) != -1){
        digitalWrite(12, HIGH);
        delay(1000);
        digitalWrite(12, LOW);        
      }
    }
  
    if(now.dayOfTheWeek() == five){ 
    if(give(19).indexOf(Time3) != -1 && give(19).indexOf(SB) != -1){
      digitalWrite(12, HIGH);
      delay(500);
      digitalWrite(12, LOW);
    }
      if(give(19).indexOf(Time3) != -1 && give(19).indexOf(LB) != -1){
        digitalWrite(12, HIGH);
        delay(1000);
        digitalWrite(12, LOW);        
      }
    }
  
    if(now.dayOfTheWeek() == six){ 
    if(give(20).indexOf(Time3) != -1 && give(20).indexOf(SB) != -1){
      digitalWrite(12, HIGH);
      delay(500);
      digitalWrite(12, LOW);
    }
      if(give(20).indexOf(Time3) != -1 && give(20).indexOf(LB) != -1){
        digitalWrite(12, HIGH);
        delay(1000);
        digitalWrite(12, LOW);        
      }
    }
  
    if(now.dayOfTheWeek() == seven){ 
    if(give(21).indexOf(Time3) != -1 && give(21).indexOf(SB) != -1){
      digitalWrite(12, HIGH);
      delay(500);
      digitalWrite(12, LOW);
    }
      if(give(21).indexOf(Time3) != -1 && give(21).indexOf(LB) != -1){
        digitalWrite(12, HIGH);
        delay(1000);
        digitalWrite(12, LOW);        
      }
    }
  }
  

Pls help me to solve this error

You seem to have ignored every bit of the advice that I gave so I give up.

But for others that may want to help:

What error? Include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.

The posted code bears little resemblance to the original posted requirements.

For those that want to help, please state in detail, the requirements of your code.

Sorry but i could not understand the difference between c string and strings

You can't save a 6-character string starting at address 15 and a 7-character string starting at address 16. The 6-character string is saved in addresses 15 (length), 16 (M), 17 (o), 18 (n), 19 (d), 20 (a), and 21 (y). If you start the next string at address 16 you will be overwriting the string that started at address 15. You have to decide how long a string you need to store and space your addresses at least that far from each other.

Generally you would store a number to represent which of a known list of strings you are storing.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.