Changed wifi management from SD card file to EEPROM (via espressif's Preferences.h library)

This commit is contained in:
2023-12-11 22:17:59 +01:00
parent edb2c80e5e
commit 0ecb3099d4
4 changed files with 24 additions and 89 deletions

View File

@@ -48,19 +48,19 @@ const String directoryPlaylistName = ".directory"; // name for d
const String wifiConfigPath = "/.wifi.conf"; // MAX LENGTH: 32 resp. 63 chars; path to configuration file; content: <WiFi SSID>\n<WiFi PSK> (ssid and password divided by an newline)
const String apSSID = version; // ssid of access point opened when not able to connect to wifi
const String apPSK = "aA16161Aa"; // pre-shared-key of access point opened when not able to connect to wifi
const String PREFERENCES_NAMESPACE = "netspeaker"; // namespace for preferences library (stores config data in the ESP32's built-in EEPROM)
const String PREFERENCES_KEY_WIFI_SSID = "wifi_ssid"; // preferences key name for wifi ssid
const String PREFERENCES_KEY_WIFI_PSK = "wifi_psk"; // preferences key name for wifi psk
const String PREFERENCES_KEY_FRIENDLY_NAME = "friendly_name"; // preferences key name for the NetSpeaker's friendly name
const String PREFERENCES_KEY_RESTORE_OLD_STATE = "restore_state"; // preferences key name for getting if the old state should be restored or not
const String PREFERENCES_KEY_VOLUME = "volume"; // preferences key name for old volume (for state restore)
const String PREFERENCES_KEY_BALANCE = "balance"; // preferences key name for old balance (for state restore)
const String PREFERENCES_KEY_EQ_LOW = "eq_low"; // preferences key name for old low equalizer value (for state restore)
const String PREFERENCES_KEY_EQ_MID = "eq_mid"; // preferences key name for old mid equalizer value (for state restore)
const String PREFERENCES_KEY_EQ_HIGH = "eq_high"; // preferences key name for old high equalizer value (for state restore)
const String PREFERENCES_KEY_PLAYING = "playing"; // preferences key name for info if currently playing (for state restore)
const String PREFERENCES_KEY_PLAYLIST_PATH = "playlist"; // preferences key name for playlist path (for state restore)
const String PREFERENCES_KEY_PLAYLIST_INDEX = "pl-index"; // preferences key name for current playlist index (for state restore)
const char PREFERENCES_NAMESPACE[] = "netspeaker"; // namespace for preferences library (stores config data in the ESP32's built-in EEPROM)
const char PREFERENCES_KEY_WIFI_SSID[] = "wifi_ssid"; // preferences key name for wifi ssid
const char PREFERENCES_KEY_WIFI_PSK[] = "wifi_psk"; // preferences key name for wifi psk
const char PREFERENCES_KEY_FRIENDLY_NAME[] = "friendly_name"; // preferences key name for the NetSpeaker's friendly name
const char PREFERENCES_KEY_RESTORE_OLD_STATE[] = "restore_state"; // preferences key name for getting if the old state should be restored or not
const char PREFERENCES_KEY_VOLUME[] = "volume"; // preferences key name for old volume (for state restore)
const char PREFERENCES_KEY_BALANCE[] = "balance"; // preferences key name for old balance (for state restore)
const char PREFERENCES_KEY_EQ_LOW[] = "eq_low"; // preferences key name for old low equalizer value (for state restore)
const char PREFERENCES_KEY_EQ_MID[] = "eq_mid"; // preferences key name for old mid equalizer value (for state restore)
const char PREFERENCES_KEY_EQ_HIGH[] = "eq_high"; // preferences key name for old high equalizer value (for state restore)
const char PREFERENCES_KEY_PLAYING[] = "playing"; // preferences key name for info if currently playing (for state restore)
const char PREFERENCES_KEY_PLAYLIST_PATH[] = "playlist"; // preferences key name for playlist path (for state restore)
const char PREFERENCES_KEY_PLAYLIST_INDEX[] = "pl-index"; // preferences key name for current playlist index (for state restore)
// create all needed variables
@@ -96,6 +96,7 @@ struct playbackInfo pbInfo = { "", "", "", "", "", "", "", "", "" };
void setup() {
Serial.begin(115200); // setup Serial console (over USB) with baud 115200
configuration.begin(PREFERENCES_NAMESPACE, false); // Initialize preferences library in RW mode
// connect to sd card reader
Serial.println("[SETUP] Setting up SD-Card reader");

View File

@@ -20,67 +20,6 @@ int setupSD(int SD_CS, int SPI_MISO, int SPI_MOSI, int SPI_SCK) {
return ret; // returns 1 if everything is OK, 0 if an error occured (eg. wiring not correct)
}
bool writeWiFiSecrets(String confPath, String ssid, String psk) {
File wifiSecrets = SD.open(confPath, FILE_WRITE);
wifiSecrets.printf("%s\n", ssid.c_str());
wifiSecrets.printf("%s", psk.c_str());
wifiSecrets.flush();
wifiSecrets.close();
return true;
}
bool writeWiFiSecrets(String ssid, String psk) {
return writeWiFiSecrets(wifiConfigPath, ssid, psk);
}
String getWiFiSSID(String confPath) {
String ssid;
char new_char;
if (!SD.exists(confPath)) return ""; // if the config file doesn't exist, return nothing and exit
File confFile = SD.open(confPath);
ssid = (char)confFile.read();
while (ssid[ssid.length() - 1] != '\n' || ssid[ssid.length() - 1] != '\r' && confFile.available()) {
new_char = (char)confFile.read();
if (new_char == '\n') break;
ssid += new_char;
}
return ssid;
}
String getWiFiSSID() {
return getWiFiSSID(wifiConfigPath);
}
String getWiFiPSK(String confPath) {
String line1;
String psk;
char new_char;
if (!SD.exists(confPath)) return ""; // if the config file doesn't exist, return nothing and exit
File confFile = SD.open(confPath);
// skip the first line
line1 = (char)confFile.read(); // read character...
while (line1[line1.length() - 1] != '\n' && confFile.available()) line1 += (char)confFile.read(); // ... as long as a newline hits
psk = (char)confFile.read();
while (confFile.available()) {
new_char = (char)confFile.read();
if (new_char == '\n' || new_char == '\r') break;
psk += new_char;
}
return psk;
}
String getWiFiPSK() {
return getWiFiPSK(wifiConfigPath);
}
bool createPlaylistFromDirectory(String folderpath) { // create a .m3u playlist from all directory contents (the directory 'folderpath' is used)
File folder;
File playlist;

View File

@@ -417,7 +417,7 @@ void api_v1_system_restart() {
void api_v1_system_wifi_getssid() {
Serial.println("[HTTP] [API] 200 - GET '/api/v1/system/wifi/get_ssid'");
api_server.send(200, "application/json", generate_api_json(true, "\"wifi_ssid\": \"" + getWiFiSSID() + "\""));
api_server.send(200, "application/json", generate_api_json(true, "\"wifi_ssid\": \"" + configuration.getString(PREFERENCES_KEY_WIFI_SSID) + "\""));
}
void api_v1_system_wifi_change() {
@@ -440,11 +440,12 @@ void api_v1_system_wifi_change() {
}
if (ssid != "" && psk != "") { // both ssid and psk are given
writeWiFiSecrets(ssid, psk);
configuration.putString(PREFERENCES_KEY_WIFI_SSID, ssid);
configuration.putString(PREFERENCES_KEY_WIFI_PSK, psk);
} else if (ssid != "" && psk == "") { // just the ssid is given
writeWiFiSecrets(ssid, getWiFiPSK());
configuration.putString(PREFERENCES_KEY_WIFI_SSID, ssid);
} else if (ssid == "" && psk != "") { // just the psk is given
writeWiFiSecrets(getWiFiSSID(), psk);
configuration.putString(PREFERENCES_KEY_WIFI_PSK, psk);
} else { // none of ssid or psk is given
api_server.send(200, "application/json", generate_api_json(false));
return;

View File

@@ -17,26 +17,20 @@ void setupWiFi() {
switch(currentWiFiMode) {
case 0: {
Serial.println("[WiFi] Connecting to WiFi...");
String WiFiSSIDstr = getWiFiSSID();
String WiFiPSKstr = getWiFiPSK();
if(WiFiPSKstr == String() || WiFiSSIDstr == String()) {
String WiFiSSID = configuration.getString(PREFERENCES_KEY_WIFI_SSID);
String WiFiPSK = configuration.getString(PREFERENCES_KEY_WIFI_PSK);
if(WiFiPSK == String() || WiFiSSID == String()) {
currentWiFiMode = 1;
Serial.printf("[WiFi] No WiFi configured\n");
setupWiFi(); // recursive call to reach the 'case 1' statement
break;
}
char WiFiSSID[33];
char WiFiPSK[64];
// copy to char arrays as the WiFi library needs these (maximum length 32/63 chars)
for(int i = 0; i<33; i++) { if(WiFiSSIDstr.length() >= i) {WiFiSSID[i] = WiFiSSIDstr[i];} else break; }
for(int i = 0; i<64; i++) { if(WiFiPSKstr.length() >= i) {WiFiPSK[i] = WiFiPSKstr[i];} else break; }
//Serial.printf("[WiFi] Credentials: SSID '%s' | PSK '%s'\n", WiFiSSID, WiFiPSK); // kept here fordebugging wifi problems
//Serial.printf("[WiFi] Credentials: SSID '%s' | PSK '%s'\n", WiFiSSID.c_str(), WiFiPSK.c_str()); // kept here for debugging wifi problems
WiFi.disconnect();
WiFi.mode(WIFI_AP_STA);
WiFi.begin(WiFiSSID, WiFiPSK);
WiFi.begin(WiFiSSID.c_str(), WiFiPSK.c_str());
// wait for 10 seconds for wifi to connect
int start_timer = millis(); while(WiFi.status() != WL_CONNECTED) { if((millis()-start_timer) > 20000) break; delay(10); }
if(WiFi.status() != WL_CONNECTED) {