DrEichenbach
DrEichenbach

Reputation: 382

Write String to permanent flash memory of Arduino ESP32

I want to write some text into the flash memory of an Arduino ESP32. It works kinda but not as I want it to.

void writeString(const char* toStore, int startAddr) {
  int i = 0;
  for (; i < LENGTH(toStore); i++) {
    EEPROM.write(startAddr + i, toStore[i]);
  }
  EEPROM.write(startAddr + i, '\0');
  EEPROM.commit();
}

My call

writeString("TEST_STRING_TO_WRITE", 0);

only writes TEST into the memory. I do not understand why. Is that because of the _? Or am I missing something different?

Here is the used LENGTH macro

#define LENGTH(x) (sizeof(x)/sizeof(x[0]))

and the method I use to read the string from the memory again (which seems to work correctly):

String readStringFromFlash(int startAddr) {
  char in[128];
  char curIn;
  int i = 0;
  curIn = EEPROM.read(startAddr);
  for (; i < 128; i++) {
    curIn = EEPROM.read(startAddr + i);
    in[i] = curIn;
  }
  return String(in);
}

Upvotes: 5

Views: 13594

Answers (2)

Rounak Datta
Rounak Datta

Reputation: 460

Below is the code to demonstrate the storing as well as retrieving of the string ssid in the EEPROM (permanent storage).

#include "EEPROM.h"

int addr = 0;
#define EEPROM_SIZE 64

// the sample text which we are storing in EEPROM
char ssid[64] = "CARNIVAL OF RUST";

void setup() {
    Serial.begin(115200);
    Serial.println("starting now...");

    if (!EEPROM.begin(EEPROM_SIZE)) {
        Serial.println("failed to init EEPROM");
        while(1);
    }

    // writing byte-by-byte to EEPROM
    for (int i = 0; i < EEPROM_SIZE; i++) {
        EEPROM.write(addr, ssid[i]);
        addr += 1;
    }
    EEPROM.commit();

    // reading byte-by-byte from EEPROM
    for (int i = 0; i < EEPROM_SIZE; i++) {
        byte readValue = EEPROM.read(i);

        if (readValue == 0) {
            break;
        }

        char readValueChar = char(readValue);
        Serial.print(readValueChar);
    }
}

void loop() {

}

Upvotes: 3

romkey
romkey

Reputation: 7069

Where on earth did you get that LENGTH macro from? It’s surreal.

sizeof will not do what you want here. It’s a compile-time function that computes the storage requirements of its argument. In this case it should return the length in bytes of a character pointer, not the string it points to.

You want to use strlen(), assuming your char* is a properly terminated C string. Add one to make sure the ‘\0’ at the end gets stored, too.

#define LENGTH(x) (strlen(x) + 1)

Upvotes: 3

Related Questions