diff --git a/platformio.ini b/platformio.ini index 01c946b..b88151b 100644 --- a/platformio.ini +++ b/platformio.ini @@ -18,3 +18,4 @@ lib_deps = https://github.com/me-no-dev/ESPAsyncWebServer.git AsyncTCP bblanchon/ArduinoJson + https://github.com/schreibfaul1/ESP32-audioI2S.git diff --git a/src/main.cpp b/src/main.cpp index 97be26d..aeddaa9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,29 +2,25 @@ #include #include #include +#include #include "creds.h" // #define B_WIFI_AP -const byte led = 2; -bool ledOn = false; +#define I2S_DOUT 25 +#define I2S_BCLK 27 +#define I2S_LRC 26 + String selectedFile = ""; AsyncWebServer server(80); +Audio audio; void onPlay(AsyncWebServerRequest *request) { - Serial.println("Toggling LED"); - if (ledOn) - { - ledOn = false; - digitalWrite(led, LOW); - } - else - { - ledOn = true; - digitalWrite(led, HIGH); - } + String path = "/music/" + selectedFile; + Serial.println("Playing file: " + path); + audio.connecttoFS(SPIFFS, path.c_str()); request->send(200); } @@ -67,8 +63,6 @@ void setup() { // Setup serial Serial.begin(115200); - pinMode(led, OUTPUT); - digitalWrite(led, LOW); // Setup SPIFFS if (!SPIFFS.begin()) @@ -77,17 +71,6 @@ void setup() return; } - // List existing files - File root = SPIFFS.open("/music"); - File file = root.openNextFile(); - if (file) - { - String fileName = file.name(); - if (!fileName.startsWith(".")) - selectedFile = fileName; - file.close(); - } - // Wifi #ifdef B_WIFI_AP Serial.println("Setting up AP..."); @@ -100,8 +83,9 @@ void setup() while (WiFi.status() != WL_CONNECTED) { Serial.print("."); - delay(100); + delay(500); } + Serial.println(); Serial.print("IP address: "); Serial.println(WiFi.localIP()); #endif @@ -116,6 +100,9 @@ void setup() server.begin(); Serial.println("Server ready!"); + + audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT); + audio.setVolume(21); // Max 21 } void loop()