3

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?

asked Sep 23, 2018 at 18:51

1 Answer 1

3

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).

answered Sep 23, 2018 at 19:58
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.