2022-12-30 19:19:52 +01:00
|
|
|
#include <Arduino.h>
|
|
|
|
#include <ESPAsyncWebServer.h>
|
|
|
|
#include <SPIFFS.h>
|
2023-01-01 02:43:54 +01:00
|
|
|
#include <AsyncJson.h>
|
2023-01-01 01:02:18 +01:00
|
|
|
#include "creds.h"
|
2022-12-30 19:19:52 +01:00
|
|
|
|
2023-01-01 01:02:18 +01:00
|
|
|
// #define B_WIFI_AP
|
2022-12-30 20:14:35 +01:00
|
|
|
|
|
|
|
const byte led = 2;
|
2022-12-31 20:54:44 +01:00
|
|
|
bool ledOn = false;
|
2023-01-01 02:43:54 +01:00
|
|
|
String selectedFile = "";
|
2022-12-30 20:14:35 +01:00
|
|
|
|
|
|
|
AsyncWebServer server(80);
|
|
|
|
|
2022-12-31 20:54:44 +01:00
|
|
|
void onPlay(AsyncWebServerRequest *request)
|
|
|
|
{
|
|
|
|
Serial.println("Toggling LED");
|
|
|
|
if (ledOn)
|
|
|
|
{
|
|
|
|
ledOn = false;
|
|
|
|
digitalWrite(led, LOW);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ledOn = true;
|
|
|
|
digitalWrite(led, HIGH);
|
|
|
|
}
|
|
|
|
request->send(200);
|
|
|
|
}
|
|
|
|
|
2023-01-01 02:43:54 +01:00
|
|
|
void onAvailableFiles(AsyncWebServerRequest *request)
|
|
|
|
{
|
|
|
|
Serial.println("Available files");
|
|
|
|
AsyncResponseStream *response = request->beginResponseStream("application/json");
|
|
|
|
|
|
|
|
DynamicJsonDocument root(256);
|
|
|
|
root["selectedFile"] = selectedFile;
|
|
|
|
JsonArray files = root.createNestedArray("files");
|
|
|
|
File music = SPIFFS.open("/music");
|
|
|
|
File file = music.openNextFile();
|
|
|
|
while (file)
|
|
|
|
{
|
|
|
|
Serial.print("File: ");
|
|
|
|
String fileName = file.name();
|
|
|
|
Serial.println(fileName);
|
|
|
|
files.add(fileName);
|
|
|
|
file.close();
|
|
|
|
file = music.openNextFile();
|
|
|
|
}
|
|
|
|
serializeJson(root, *response);
|
|
|
|
|
|
|
|
request->send(response);
|
|
|
|
}
|
|
|
|
|
|
|
|
void onSelectFile(AsyncWebServerRequest *request)
|
|
|
|
{
|
|
|
|
Serial.print("Select file: ");
|
|
|
|
if (request->hasParam("fileName", true))
|
|
|
|
{
|
|
|
|
selectedFile = request->getParam("fileName", true)->value();
|
|
|
|
Serial.print(selectedFile);
|
|
|
|
}
|
|
|
|
Serial.println();
|
|
|
|
request->send(200);
|
|
|
|
}
|
|
|
|
|
2022-12-30 20:14:35 +01:00
|
|
|
void setup()
|
|
|
|
{
|
|
|
|
// Setup serial
|
|
|
|
Serial.begin(115200);
|
|
|
|
pinMode(led, OUTPUT);
|
|
|
|
digitalWrite(led, LOW);
|
|
|
|
|
|
|
|
// Setup SPIFFS
|
|
|
|
if (!SPIFFS.begin())
|
|
|
|
{
|
|
|
|
Serial.println("SPIFFS error. Exiting.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// List existing files
|
|
|
|
File root = SPIFFS.open("/");
|
|
|
|
File file = root.openNextFile();
|
|
|
|
while (file)
|
|
|
|
{
|
|
|
|
Serial.print("File: ");
|
2022-12-31 20:54:44 +01:00
|
|
|
Serial.println(file.path());
|
2022-12-30 20:14:35 +01:00
|
|
|
file.close();
|
|
|
|
file = root.openNextFile();
|
|
|
|
}
|
|
|
|
|
2023-01-01 01:02:18 +01:00
|
|
|
// Wifi
|
|
|
|
#ifdef B_WIFI_AP
|
2022-12-30 20:14:35 +01:00
|
|
|
Serial.println("Setting up AP...");
|
|
|
|
WiFi.softAP(ssid, password);
|
|
|
|
Serial.print("IP address: ");
|
|
|
|
Serial.println(WiFi.softAPIP());
|
2023-01-01 01:02:18 +01:00
|
|
|
#else
|
|
|
|
Serial.print("Connecting to wifi...");
|
|
|
|
WiFi.begin(ssid, password);
|
|
|
|
while (WiFi.status() != WL_CONNECTED)
|
|
|
|
{
|
|
|
|
Serial.print(".");
|
|
|
|
delay(100);
|
|
|
|
}
|
|
|
|
Serial.print("IP address: ");
|
|
|
|
Serial.println(WiFi.localIP());
|
|
|
|
#endif
|
2022-12-30 20:14:35 +01:00
|
|
|
|
|
|
|
// Server
|
2022-12-31 20:54:44 +01:00
|
|
|
server.on("/play", HTTP_GET, onPlay);
|
2023-01-01 02:43:54 +01:00
|
|
|
server.on("/available-files", HTTP_GET, onAvailableFiles);
|
|
|
|
server.on("/select-file", HTTP_POST, onSelectFile);
|
2022-12-31 20:54:44 +01:00
|
|
|
server.serveStatic("/", SPIFFS, "/www/").setDefaultFile("index.html");
|
2022-12-30 20:14:35 +01:00
|
|
|
server.begin();
|
|
|
|
Serial.println("Server ready!");
|
2022-12-30 19:19:52 +01:00
|
|
|
}
|
|
|
|
|
2022-12-30 20:14:35 +01:00
|
|
|
void loop()
|
|
|
|
{
|
2022-12-31 20:54:44 +01:00
|
|
|
}
|