Added wifi config management. Removed the old, unused method.

This commit is contained in:
2023-12-03 14:06:38 +01:00
parent 28a025f499
commit 7b025bbc16
3 changed files with 47 additions and 40 deletions

View File

@@ -36,9 +36,7 @@ const int retrySDMountTreshold = 1000; // defines how long
const int readyPin = 32; // for an LED that shows if everything started up correctly (SD card mounted, wifi connected, ...)
const String playlistExtension = ".m3u"; // extension for playlist files
const String directoryPlaylistName = ".directory"; // name for directory playlists (not a path, just a filename without ext.!)
const String configurationPath = "/.netspeaker.conf"; // path to configuration file
const String configPrimaryWiFiSSIDKey = "primarywifissid"; // key for wifi ssid in the config
const String configPrimaryWiFiPWDKey = "primarywifipwd"; // key for wifi pwd in the config
const String wifiConfigPath = "/.wifi.conf"; // path to configuration file; content: <WiFi SSID>\n<WiFi PSK> (ssid and password divided by an newline)
// create all needed variables
int currentVolume; // variable where current volume (0...21) is stored
@@ -71,6 +69,7 @@ void setup() {
audio.connecttoFS(SD, getSongFromPlaylist(currentPlaylist, currentPlaylistPosition).c_str()); // play first element of the playlist
digitalWrite(readyPin, HIGH); // show that startup is done and everything works fine
wiFiConfTest();
}
void loop() {

View File

@@ -15,50 +15,51 @@ int setupSD(int SD_CS, int SPI_MISO, int SPI_MOSI, int SPI_SCK) {
pinMode(SD_CS, OUTPUT);
digitalWrite(SD_CS, HIGH); // make microSD-card reader board listen to the ESP (over SPI)
SPI.begin(SPI_SCK, SPI_MISO, SPI_MOSI); // start SPI
Serial.printf("[SETUP] Set up SD card successfully (Pins: MISO %d | MOSI %d | SCK %d | CS %d)\n", SPI_MISO, SPI_MOSI, SPI_SCK, SD_CS);
return SD.begin(SD_CS); // returns 1 if everything is OK, 0 if an error occured (eg. wiring not correct)
int ret = SD.begin(SD_CS);
if (ret == 1) Serial.printf("[SETUP] Set up SD card successfully (Pins: MISO %d | MOSI %d | SCK %d | CS %d)\n", SPI_MISO, SPI_MOSI, SPI_SCK, SD_CS);
return ret; // returns 1 if everything is OK, 0 if an error occured (eg. wiring not correct)
}
String getConfigurationByKey(File configurationFile, String key) {
char currentChar;
String currentKey;
String result;
String getWiFiSSID(String confPath) {
String ssid;
char new_char;
currentChar = configurationFile.read();
while (configurationFile.available()) { // go until the end of the file
currentKey += currentChar;
currentChar = configurationFile.read();
if (currentChar == ':') {
if (currentKey == key) { // if the current key (current key at read position) is the wanted
while (configurationFile.available()) { // retrieve the value of the key
currentChar = configurationFile.read(); // read next byte
if (currentChar == '\n') return result;
result += currentChar;
}
} else {
currentKey = ""; // reset the current key...
while (currentChar != '\n' && configurationFile.available()) currentChar = configurationFile.read(); // ... and go until the next line
currentChar = configurationFile.read(); // ... then go into the next line and search for the next!
}
}
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' && confFile.available()) {
new_char = (char)confFile.read();
if (new_char == '\n') break;
ssid += new_char;
}
return "";
return ssid;
}
String getPrimaryWiFiPWDFromConf(String configurationPath) {
if (!SD.exists(configurationPath)) return ""; // if the config file doesn't exist, return nothing and exit
File configurationFile = SD.open(configurationPath);
String getWiFiPSK(String confPath) {
String line1;
String psk;
char new_char;
return getConfigurationByKey(configurationFile, configPrimaryWiFiPWDKey); // call getConfigurationByKey and pass 'configPrimaryWiFiPWDKey' as key to search for to it
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') break;
psk += new_char;
}
return psk;
}
String getPrimaryWiFiSSIDFromConf(String configurationPath) {
if (!SD.exists(configurationPath)) return ""; // if the config file doesn't exist, return nothing and exit
File configurationFile = SD.open(configurationPath);
return getConfigurationByKey(configurationFile, configPrimaryWiFiSSIDKey); // call getConfigurationByKey and pass 'configPrimaryWiFiPWDKey' as key to search for to it
}
bool createPlaylistFromDirectory(String folderpath) { // create a .m3u playlist from all directory contents (the directory 'folderpath' is used)
File folder;
@@ -106,9 +107,9 @@ String getSongFromPlaylist(String path, int position) {
song = (char)playlist.read(); // read character...
while (song[song.length() - 1] != '\n' && playlist.available()) song += (char)playlist.read(); // ... as long as a newline hits
song = song.c_str();
song[song.length() - 1] = '\0';
song = String(song); // remove the trailing '\n'
return song; // return the result
song[song.length() - 1] = '\0'; // remove the trailing '\n'
song = String(song);
return song; // return the result
}
currentChar = playlist.read(); // read character...
while (currentChar != '\n') currentChar = playlist.read(); // ... as long as a newline hits

View File

@@ -10,3 +10,10 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
For more information, please refer to <http://unlicense.org/>
*/
void wiFiConfTest() {
Serial.println(getWiFiPSK(wifiConfigPath));
Serial.println(getWiFiSSID(wifiConfigPath));
Serial.println(getWiFiSSID(wifiConfigPath));
Serial.println(getWiFiPSK(wifiConfigPath));
}