186 lines
3.4 KiB
C++
186 lines
3.4 KiB
C++
#if defined(ESP8266)
|
|
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
|
|
#else
|
|
#include <WiFi.h>
|
|
#endif
|
|
|
|
#include <ESPAsyncWebServer.h> //Local WebServer used to serve the configuration portal
|
|
#include <ESPAsyncWiFiManager.h> //https://github.com/tzapu/WiFiManager WiFi Configuration Magic
|
|
|
|
#include "Audio.h"
|
|
|
|
#define I2S_DOUT 26 // connect to DAC pin DIN
|
|
#define I2S_BCLK 27 // connect to DAC pin BCK
|
|
#define I2S_LRC 25 // connect to DAC pin LCK
|
|
|
|
#include <SPI.h>
|
|
#include <SD.h>
|
|
|
|
#include "WebContent.h"
|
|
|
|
File root;
|
|
File mp3File;
|
|
|
|
void printDirectory(File dir, int numTabs);
|
|
|
|
unsigned long lastStart = 0;
|
|
|
|
const int startDelay = 250;
|
|
|
|
Audio audio;
|
|
|
|
// Set web server port number to 80
|
|
AsyncWebServer server(80);
|
|
AsyncWebServer server2(81);
|
|
DNSServer dns;
|
|
File next;
|
|
|
|
// Variable to store the HTTP request
|
|
String header;
|
|
|
|
void playNextMp3()
|
|
{
|
|
next = root.openNextFile();
|
|
|
|
if (!next)
|
|
{
|
|
root = SD.open("/");
|
|
}
|
|
|
|
while (!String(next.name()).endsWith(".mp3"))
|
|
{
|
|
next = root.openNextFile();
|
|
if (!next)
|
|
{
|
|
Serial.println("no more files found.");
|
|
return;
|
|
}
|
|
}
|
|
Serial.print("initialized");
|
|
Serial.print(next.name());
|
|
Serial.print("\n");
|
|
audio.stopSong();
|
|
audio.connecttoSD(next.name());
|
|
}
|
|
|
|
void audio_info(const char *info)
|
|
{
|
|
// Serial.print("info "); Serial.println(info);
|
|
}
|
|
|
|
String processor(const String& var)
|
|
{
|
|
if(var == "PLAYING")
|
|
return F(next.name());
|
|
return String();
|
|
}
|
|
|
|
void stop() {
|
|
if (audio.isRunning()) {
|
|
audio.stopSong();
|
|
}
|
|
}
|
|
|
|
void start() {
|
|
if (!next) {
|
|
playNextMp3();
|
|
} else {
|
|
audio.connecttoSD(next.name());
|
|
}
|
|
}
|
|
|
|
void setup()
|
|
{
|
|
// put your setup code here, to run once:
|
|
Serial.begin(115200);
|
|
|
|
pinMode(D3, INPUT_PULLUP);
|
|
|
|
// first parameter is name of access point, second is the password
|
|
AsyncWiFiManager wifiManager(&server, &dns);
|
|
|
|
wifiManager.autoConnect("HannaBox");
|
|
|
|
Serial.print("Initializing SD card...");
|
|
|
|
if (!SD.begin(D1))
|
|
{
|
|
Serial.println("SD initialization failed!");
|
|
while (1)
|
|
;
|
|
}
|
|
Serial.println("SD initialization done.");
|
|
|
|
root = SD.open("/");
|
|
// printDirectory(root, 0);
|
|
audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);
|
|
audio.setVolume(12); // 0...21
|
|
|
|
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
|
|
{ request->send_P(200, "text/html", index_html,processor); });
|
|
|
|
server.on("/start", HTTP_GET, [] (AsyncWebServerRequest *request) {
|
|
start();
|
|
request->send(200, "text/plain", "start");
|
|
});
|
|
|
|
server.on("/stop", HTTP_GET, [] (AsyncWebServerRequest *request) {
|
|
stop();
|
|
request->send(200, "text/plain", "stop");
|
|
});
|
|
|
|
|
|
server.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
void printDirectory(File dir, int numTabs)
|
|
{
|
|
while (true)
|
|
{
|
|
|
|
File entry = dir.openNextFile();
|
|
if (!entry)
|
|
{
|
|
// no more files
|
|
break;
|
|
}
|
|
for (uint8_t i = 0; i < numTabs; i++)
|
|
{
|
|
Serial.print('\t');
|
|
}
|
|
Serial.print(entry.name());
|
|
if (entry.isDirectory())
|
|
{
|
|
Serial.println("/");
|
|
printDirectory(entry, numTabs + 1);
|
|
}
|
|
else
|
|
{
|
|
// files have sizes, directories do not
|
|
Serial.print("\t\t");
|
|
Serial.println(entry.size(), DEC);
|
|
}
|
|
entry.close();
|
|
}
|
|
}
|
|
|
|
void loop()
|
|
{
|
|
|
|
audio.loop();
|
|
// Serial.print(digitalRead(D2));
|
|
|
|
if (digitalRead(D3) == LOW)
|
|
{
|
|
unsigned long now = millis();
|
|
if (now - lastStart > startDelay)
|
|
{
|
|
playNextMp3();
|
|
Serial.println("mp3 started.");
|
|
lastStart = now;
|
|
}
|
|
}
|
|
} |