Why is my char array empty

Following Sketch will run totally fine and as expected:

#include <Wire.h>

#include "SparkFun_External_EEPROM.h"
ExternalEEPROM ExtEEPROM;
char username[33] = "";
char password[65] = "";

void setup()
{
  Serial.begin(115200);
  delay(1000);
  Wire.begin();
  #define EEPROM_ADDRESS 0b1011000 // 0x58
  ExtEEPROM.setMemorySize(256000/8); // 256kbit = 32kbyte
  ExtEEPROM.setPageSize(64); // 64 byte page size.
  ExtEEPROM.enablePollForWriteComplete();
  ExtEEPROM.setPageWriteTime(10); // max. ms for AT24C128
  if (ExtEEPROM.begin(EEPROM_ADDRESS, Wire) == false) 
  {
    Serial.println("No memory detected. Freezing.");
    while (1);
  }
  Serial.println("Memory detected!");

  String uuu = "myusername";
  String ppp = "mypassword";

  uuu.toCharArray(username, sizeof(username) - 1);
  ppp.toCharArray(password, sizeof(password) - 1);
  Serial.print("password=");Serial.println(password);
  ExtEEPROM.put(0, username);
  //delay(100);
  ExtEEPROM.put(0 + sizeof(username), "greatnewpassword");
}
  
void loop() {
  char xxx[33] = "";
  ExtEEPROM.get(0, xxx);
  Serial.print("xxx=");Serial.println(xxx); // prints the username

  char yyy[33] = "";
  ExtEEPROM.get(0 + sizeof(username), yyy);
  Serial.print("yyy=");Serial.println(yyy); // prints the password
  delay(1000);
}

Webpage
However, if I replace ExtEEPROM.put(0 + sizeof(username), "greatnewpassword"); to ExtEEPROM.put(0 + sizeof(username), password);, then yyy prints empty in the serial monitors. Why?

I think this is going to store the 2-byte ADDRESS of "greatnewpassword" in EEPROM. To store the CONTENTS, try:

  strcpy(password, "greatnewpassword");
  ExtEEPROM.put(0 + sizeof(username), password);`

That should store all 65 bytes of the password array.

Note:

  char yyy[33] = "";
  ExtEEPROM.get(0 + sizeof(username), yyy);

Shouldn't yyy be a 65-character array to hold the 65-character password?

Notice the eprom size is of username but the password is being placed. The username is set as 33 char and the password is set as 65 char. A size mismatch.

The first argument of EEPROM.put is the place to start storing, not the size of the object to be written. The password is stored after the offset left to hold username.

If you look at the source code of the SparkFun library you will see, it just truncates the write at the page boundary, rather than managing a write across two pages.

There was a bug posted against the library in GitHub about the page boundaries as well.

It's not clear to me if your larger password character array will not see a page write issue.

According to someone:

.get() and .put() should only be used for atomic, self-contained, types like int or float, not pointers like char *.

Not sure What Atomic means here, but you may. put and .get any data type, especially a struct.

struct EPROMData {
  char username[33];
  char password[65];
}; 
EPROMData data {"myusername","mypassword"};

void setup() {
   ...
   ExtEEPROM.put(0,data);
}
void loop() {
   EPROMData d;
   ExtEEPROM.get(0,d);
   Serial.print("user="); Serial.println(d.username); 
   ...
}

Sure, if the data type is a pointer or a String object (containing a pointer only), you won't be happy with .put / .get source

Will the above line give the expected result? Or should it be:

If 256000 won't fit into an 'int' it is treated as 'long'. Adding the "UL" suffix will make it an 'unsigned long' but I don't think the result will be different.

It should probably be:
ExtEEPROM.setMemorySize((256*1024ul)/8); // 256kbit = 32kbyte
or
ExtEEPROM.setMemorySize((256/8)*1024ul); // 256kbit = 32kbyte

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