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.
DaveX
March 2, 2022, 3:30pm
4
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.
opened 08:19PM - 03 Dec 20 UTC
closed 12:47AM - 05 Dec 20 UTC
I have a Mega2560 connected to the AT24C512 using the I2C interface. Pullups (2.… 1K) are on SDL and SDA.
I'm using Put to store a 17-byte structure to EEPROM memory, starting at address 1. The first 3 records are fine, but the 4th record which should go to byte 68, stops writing at byte 63. This is with a page size of 64. Changing the page size to 128 will work until it hits byte 127, then stops writing. If I write a single byte to any of these locations, that works as expected. The only change to settings was going from 64 to 128 for the page size.
Code snippets:
struct eRecordType {
float Vout1;
float Vout2;
float Vout3;
float Vout4;
byte ValidRec;
};
eRecordType eRecord; //variables used
ExternalEEPROM ExtMem;
ExtMem.put(addr,eRecord); //where addr = 52 for page=64 or 120 for page=128
Attached are memory dumps where the same data is written until the page boundary is hit.
[memory dumps.txt](https://github.com/sparkfun/SparkFun_External_EEPROM_Arduino_Library/files/5638711/memory.dumps.txt)
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
system
Closed
September 1, 2022, 7:30pm
9
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.