Added wifi config management. Removed the old, unused method.
This commit is contained in:
@@ -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 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 playlistExtension = ".m3u"; // extension for playlist files
|
||||||
const String directoryPlaylistName = ".directory"; // name for directory playlists (not a path, just a filename without ext.!)
|
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 wifiConfigPath = "/.wifi.conf"; // path to configuration file; content: <WiFi SSID>\n<WiFi PSK> (ssid and password divided by an newline)
|
||||||
const String configPrimaryWiFiSSIDKey = "primarywifissid"; // key for wifi ssid in the config
|
|
||||||
const String configPrimaryWiFiPWDKey = "primarywifipwd"; // key for wifi pwd in the config
|
|
||||||
|
|
||||||
// create all needed variables
|
// create all needed variables
|
||||||
int currentVolume; // variable where current volume (0...21) is stored
|
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
|
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
|
digitalWrite(readyPin, HIGH); // show that startup is done and everything works fine
|
||||||
|
wiFiConfTest();
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
@@ -15,50 +15,51 @@ int setupSD(int SD_CS, int SPI_MISO, int SPI_MOSI, int SPI_SCK) {
|
|||||||
pinMode(SD_CS, OUTPUT);
|
pinMode(SD_CS, OUTPUT);
|
||||||
digitalWrite(SD_CS, HIGH); // make microSD-card reader board listen to the ESP (over SPI)
|
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
|
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);
|
int ret = SD.begin(SD_CS);
|
||||||
return SD.begin(SD_CS); // returns 1 if everything is OK, 0 if an error occured (eg. wiring not correct)
|
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) {
|
String getWiFiSSID(String confPath) {
|
||||||
char currentChar;
|
String ssid;
|
||||||
String currentKey;
|
char new_char;
|
||||||
String result;
|
|
||||||
|
|
||||||
currentChar = configurationFile.read();
|
if (!SD.exists(confPath)) return ""; // if the config file doesn't exist, return nothing and exit
|
||||||
while (configurationFile.available()) { // go until the end of the file
|
File confFile = SD.open(confPath);
|
||||||
currentKey += currentChar;
|
|
||||||
currentChar = configurationFile.read();
|
ssid = (char)confFile.read();
|
||||||
if (currentChar == ':') {
|
while (ssid[ssid.length() - 1] != '\n' && confFile.available()) {
|
||||||
if (currentKey == key) { // if the current key (current key at read position) is the wanted
|
new_char = (char)confFile.read();
|
||||||
while (configurationFile.available()) { // retrieve the value of the key
|
if (new_char == '\n') break;
|
||||||
currentChar = configurationFile.read(); // read next byte
|
ssid += new_char;
|
||||||
if (currentChar == '\n') return result;
|
|
||||||
result += currentChar;
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
currentKey = ""; // reset the current key...
|
return ssid;
|
||||||
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!
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return "";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String getPrimaryWiFiPWDFromConf(String configurationPath) {
|
String getWiFiPSK(String confPath) {
|
||||||
if (!SD.exists(configurationPath)) return ""; // if the config file doesn't exist, return nothing and exit
|
String line1;
|
||||||
File configurationFile = SD.open(configurationPath);
|
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)
|
bool createPlaylistFromDirectory(String folderpath) { // create a .m3u playlist from all directory contents (the directory 'folderpath' is used)
|
||||||
File folder;
|
File folder;
|
||||||
@@ -106,8 +107,8 @@ String getSongFromPlaylist(String path, int position) {
|
|||||||
song = (char)playlist.read(); // read character...
|
song = (char)playlist.read(); // read character...
|
||||||
while (song[song.length() - 1] != '\n' && playlist.available()) song += (char)playlist.read(); // ... as long as a newline hits
|
while (song[song.length() - 1] != '\n' && playlist.available()) song += (char)playlist.read(); // ... as long as a newline hits
|
||||||
song = song.c_str();
|
song = song.c_str();
|
||||||
song[song.length() - 1] = '\0';
|
song[song.length() - 1] = '\0'; // remove the trailing '\n'
|
||||||
song = String(song); // remove the trailing '\n'
|
song = String(song);
|
||||||
return song; // return the result
|
return song; // return the result
|
||||||
}
|
}
|
||||||
currentChar = playlist.read(); // read character...
|
currentChar = playlist.read(); // read character...
|
||||||
|
@@ -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/>
|
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));
|
||||||
|
}
|
Reference in New Issue
Block a user