My read
function returns an array of strings. i.e ssid and password
String* configuration::read() {
String rw_ssid = "";
String rw_pswd = "";
const int keys = 2;
String read_ssid_pswd [keys];
if (EEPROM.read(0) != 0) {
for (int i = 0; i < 32; ++i) {
rw_ssid += char(EEPROM.read(i));
}
for (int i = 32; i < 96; ++i) {
rw_pswd += char(EEPROM.read(i));
}
Serial.print("rPASSWORD: ");
Serial.println(rw_pswd);
read_ssid_pswd[0] = rw_ssid;
read_ssid_pswd[1] = rw_pswd;
Serial.print("Sending ssid:");
Serial.print(read_ssid_pswd[0]);
Serial.print(" Pswd: ");
Serial.println(read_ssid_pswd[1]);
return read_ssid_pswd;
} else {
Serial.println("Data wifi not found!");
return read_ssid_pswd;
}
}
I can see the print in the serial monitor.
Sending ssid:SSID Pswd: password
but where this function is called from I have set it up like this:
void setup() {
Serial.begin(115200);
Eeprom::configuration::initialize();
String* ssid_password = Eeprom::configuration::read();
Serial.print(">SSID: ");
Serial.println(ssid_password[0]);
Serial.print(">Password: ");
Serial.println(ssid_password[1]);
Funcs::connection::connectWifi(ssid_password[0], ssid_password[1]);
}
and I get thing in the prints I thought its not giving any error at compile time so it should be correct.
2 Answers 2
Majenko has it right, your String array should be allocated in Setup(). Notice that I passed back the success/failure of your initial EEPROM test as the return value.
void setup() {
Serial.begin(115200);
Eeprom::configuration::initialize();
const int keys = 2;
String ssid_password[keys];
if (Eeprom::configuration::read(ssid_password)) {
Serial.print(">SSID: ");
Serial.println(ssid_password[0]);
Serial.print(">Password: ");
Serial.println(ssid_password[1]);
Funcs::connection::connectWifi(ssid_password[0], ssid_password[1]);
} else {
Serial.println("Data wifi was not found!");
}
}
The String array is the parameter, not the return value. It is passed by reference. The copying of EEPROM chars to String doesn't need to be into temporary variables, in fact it's inefficient and pointless; it can go straight into read_ssid_pswd[0] and [1], but I didn't want to change your code excessively, and delete the debugging serial writes.
bool configuration::read(String read_ssid_pswd[]) {
String rw_ssid = "";
String rw_pswd = "";
if (EEPROM.read(0) != 0) {
for (int i = 0; i < 32; ++i) {
rw_ssid += char(EEPROM.read(i));
}
for (int i = 32; i < 96; ++i) {
rw_pswd += char(EEPROM.read(i));
}
Serial.print("rSSID: ");
Serial.println(rw_ssid);
Serial.print("rPASSWORD: ");
Serial.println(rw_pswd);
read_ssid_pswd[0] = rw_ssid;
read_ssid_pswd[1] = rw_pswd;
Serial.print("Sending ssid:");
Serial.print(read_ssid_pswd[0]);
Serial.print(" Pswd: ");
Serial.println(read_ssid_pswd[1]);
return true;
} else {
Serial.println("Data wifi not found!");
return false;
}
}
-
I see so just because the array in initialised in the setup method I do not have to return the array . thanks for posting an example.Ciasto piekarz– Ciasto piekarz2020年05月31日 21:00:41 +00:00Commented May 31, 2020 at 21:00
-
tried your suggestion , but I got this error:
expected ',' or '...' before 'read_ssid_pswd'
atboolean configuration::read(String[] read_ssid_pswd) {
Ciasto piekarz– Ciasto piekarz2020年05月31日 21:18:15 +00:00Commented May 31, 2020 at 21:18 -
My bad. Try
read(String read_ssid_pswd[]) {
MAXdB– MAXdB2020年06月02日 04:40:10 +00:00Commented Jun 2, 2020 at 4:40 -
1Thanks for the edits in the if statement and the function definition, both were my mistakes.MAXdB– MAXdB2020年06月02日 04:56:36 +00:00Commented Jun 2, 2020 at 4:56
You can't return locally defined arrays like that. The array only exists in the function, and all you do is return a pointer to where that array was. As the array has gone when you leave the function your pointer just points to garbage.
Instead you should pass an array to the function that the function then populates.
-
but the save is in a different cpp file and I am trying to access from setup function of the sketch.Ciasto piekarz– Ciasto piekarz2020年05月30日 20:11:42 +00:00Commented May 30, 2020 at 20:11
-
So? How does that affect what you pass to the function?Majenko– Majenko2020年05月30日 20:13:19 +00:00Commented May 30, 2020 at 20:13
-
you said function then populates !Ciasto piekarz– Ciasto piekarz2020年05月30日 20:15:23 +00:00Commented May 30, 2020 at 20:15
-
ah I see just declare the
read_ssid_pswd
outside teh function module level.Ciasto piekarz– Ciasto piekarz2020年05月30日 20:19:19 +00:00Commented May 30, 2020 at 20:19 -
3Define your array in setup() then pass that as a parameter to the read function. The read function then uses that passed array which means setup() sees the same data. You can then use the return value to indicate success or failure.Majenko– Majenko2020年05月30日 20:22:23 +00:00Commented May 30, 2020 at 20:22