I am creating a test program with Arduino Nano where I can save network and password. Here is my code to start with:
void SaveNetwork(String ssid, String pass){
String com = ssid + "~" + pass;
const char *array = com.c_str();
for(int i = 0; i<sizeof(com); i++){
EEPROM.write(i, array[i]);
}
Serial.println("Saved");
char ar[255] = ""; //test read string.
for(int i = 0; i<sizeof(com); i++){
ar[i] = EEPROM.read(i);
}
Serial.println(ar);
}
SaveNetwork("Testing", "Network123......");
Now I print ar string which is suppose to return ssid, "~" and password.
BUT what I get is "Testin" instead of "Testing~Network123......".
Is there is a limitation for EEPROM size? If so, how can I expand the capacity, should I use a memory chip, or what is the code I should use to resolve this?
1 Answer 1
The problem is with sizeof(com)
in your loops. sizeof()
is for C arrays, not string objects. If you print it, it will report 6, which is not the length of the array. For string objects you should use com.length()
instead. Then the output becomes
Saved
Testing~Network123......
P.S. The EEPROM can store 1024 bytes on ATmega328P-based boards (I believe that's what most Arduino Nanos use (clones included).