At the top of my arduino sketch, I have the following :
// softAp
const char *softApSsid = "abcdefg";
const char *softApPassword = "123456";
Those are the DEFAULT ssid and password to connect to the soft AP. The soft AP is start in the setup() such as :
WiFi.softAP(softApSsid, softApPassword);
according to the github repo, that function indeed requires "const char*" as parameters (source: https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp#L97)
Further in my sketch, I want to let the user edit the ssid and password. I wrote a function that reads the first byte of the eeprom. If it equals 255, we use the default credentials, if it equals to 1, it means the user set custom credentials, so we will use those to spawn the soft AP.
I wrote this :
void getSoftApAuth() {
bool isDefaultSoftApAuth = 255 == EEPROM.read(0);
if (! isDefaultSoftApAuth) {
softApSsid = "";
softApPassword = "";
int i = 1;
while ('\0' != EEPROM.read(i)) {
softApSsid += char(EEPROM.read(i));
i++;
}
i = 65;
while ('\0' != EEPROM.read(i)) {
softApPassword += char(EEPROM.read(i));
i++;
}
}
}
void setSoftApAuth(const char* ssid, const char* passphrase) {
EEPROM.write(0, 1);
// ssid
int positionOfSsid = 1;
int lengthOfSsid = strlen(ssid);
for (int i = 0; i < lengthOfSsid; i++) {
EEPROM.write(positionOfSsid + i, ssid[i]);
}
EEPROM.write(positionOfSsid + lengthOfSsid, '\0');
// passphrase
int positionOfPassphrase = 65;
int lengthOfPassphrase = strlen(passphrase);
for (int i = 0; i < lengthOfPassphrase; i++) {
EEPROM.write(positionOfPassphrase + i, passphrase[i]);
}
EEPROM.write(positionOfPassphrase + lengthOfPassphrase, '\0');
if (EEPROM.commit()) {
Serial.println("Successful EEPROM write");
} else {
Serial.println("Failed EEPROM write");
}
}
void resetSoftApAuth() {
EEPROM.write(0, 255);
if (EEPROM.commit()) {
Serial.println("Successful EEPROM reset");
} else {
Serial.println("Failed EEPROM reset");
}
}
this does compile but crashes and throws exceptions when I run the chip. I believe the issue is in getSoftApAuth() where i try to concatenate the string such as softApSsid += char(EEPROM.read(i));
. I print the value of softApSsid in the Serial Monitor and it outputs garbage. I replaced everything with "String" type, and this time it outputs correct username/password stored in the eeprom, but it wont start the soft AP, I believe because I am now trying to pass String instead of const char* to the WiFi.softAP()
function as I said earlier (it starts the soft AP with factory default SSID like ESP-AABBCCDDEE appending the MAC address and without any password).
So, basically, how can I modify my softApSsid
down in my code, while i declared it at the top as a const char*
, knowing it feeds a function that takes (exclusively?) a const char*
(WiFi.softAP())
Or is there an other approach / design pattern to achieve this? That is to say: having default credentials hard-coded, but allowing the user to define custom credentials (and storing/retrieving them from the eeprom).
Thanks. Please be indulgent as I am mostly a webdev and not yet comfortable with the strongly typed languages (let alone pointers / references ...)