Play audio

This commit is contained in:
Gabriel Augendre 2023-01-01 18:09:35 +01:00
parent 7904bb6707
commit e5d5d226b2
2 changed files with 15 additions and 27 deletions

View file

@ -18,3 +18,4 @@ lib_deps =
https://github.com/me-no-dev/ESPAsyncWebServer.git https://github.com/me-no-dev/ESPAsyncWebServer.git
AsyncTCP AsyncTCP
bblanchon/ArduinoJson bblanchon/ArduinoJson
https://github.com/schreibfaul1/ESP32-audioI2S.git

View file

@ -2,29 +2,25 @@
#include <ESPAsyncWebServer.h> #include <ESPAsyncWebServer.h>
#include <SPIFFS.h> #include <SPIFFS.h>
#include <AsyncJson.h> #include <AsyncJson.h>
#include <Audio.h>
#include "creds.h" #include "creds.h"
// #define B_WIFI_AP // #define B_WIFI_AP
const byte led = 2; #define I2S_DOUT 25
bool ledOn = false; #define I2S_BCLK 27
#define I2S_LRC 26
String selectedFile = ""; String selectedFile = "";
AsyncWebServer server(80); AsyncWebServer server(80);
Audio audio;
void onPlay(AsyncWebServerRequest *request) void onPlay(AsyncWebServerRequest *request)
{ {
Serial.println("Toggling LED"); String path = "/music/" + selectedFile;
if (ledOn) Serial.println("Playing file: " + path);
{ audio.connecttoFS(SPIFFS, path.c_str());
ledOn = false;
digitalWrite(led, LOW);
}
else
{
ledOn = true;
digitalWrite(led, HIGH);
}
request->send(200); request->send(200);
} }
@ -67,8 +63,6 @@ void setup()
{ {
// Setup serial // Setup serial
Serial.begin(115200); Serial.begin(115200);
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
// Setup SPIFFS // Setup SPIFFS
if (!SPIFFS.begin()) if (!SPIFFS.begin())
@ -77,17 +71,6 @@ void setup()
return; 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 // Wifi
#ifdef B_WIFI_AP #ifdef B_WIFI_AP
Serial.println("Setting up AP..."); Serial.println("Setting up AP...");
@ -100,8 +83,9 @@ void setup()
while (WiFi.status() != WL_CONNECTED) while (WiFi.status() != WL_CONNECTED)
{ {
Serial.print("."); Serial.print(".");
delay(100); delay(500);
} }
Serial.println();
Serial.print("IP address: "); Serial.print("IP address: ");
Serial.println(WiFi.localIP()); Serial.println(WiFi.localIP());
#endif #endif
@ -116,6 +100,9 @@ void setup()
server.begin(); server.begin();
Serial.println("Server ready!"); Serial.println("Server ready!");
audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);
audio.setVolume(21); // Max 21
} }
void loop() void loop()