From 4c25b59fcdd2a5a34d375e2460928ac580284616 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sun, 1 Jan 2023 02:43:54 +0100 Subject: [PATCH] Allow selecting a different file --- data/www/index.html | 4 ++-- data/www/script.js | 31 +++++++++++++++++++++++++++++++ platformio.ini | 1 + src/main.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 2 deletions(-) diff --git a/data/www/index.html b/data/www/index.html index 6f8bf0f..1e538fc 100644 --- a/data/www/index.html +++ b/data/www/index.html @@ -19,8 +19,8 @@
-

Fichier sélectionné

-

Aucun

+

Fichiers disponibles

+
diff --git a/data/www/script.js b/data/www/script.js index fee1a93..7022a75 100644 --- a/data/www/script.js +++ b/data/www/script.js @@ -4,3 +4,34 @@ function play() { xhttp.open("GET", "play", true); xhttp.send(); } + +function loadAvailableFiles() { + console.log("Available files..."); + fetch("/available-files") + .then(response => response.json()) + .then(data => { + console.log("data", data); + let dom = ""; + data.files.forEach(element => { + if (element === data.selectedFile) { + dom += ``; + } + else { + dom += ``; + } + }); + document.getElementById("available-files").innerHTML = dom; + }); +} + +function selectFile(name) { + console.log("Select file"); + const body = new FormData(); + body.set("fileName", name); + fetch("/select-file", {method: "POST", body: body}) + .then(res => loadAvailableFiles()); +} + +(() => { + loadAvailableFiles(); +})(); diff --git a/platformio.ini b/platformio.ini index fee0b73..01c946b 100644 --- a/platformio.ini +++ b/platformio.ini @@ -17,3 +17,4 @@ monitor_speed = 115200 lib_deps = https://github.com/me-no-dev/ESPAsyncWebServer.git AsyncTCP + bblanchon/ArduinoJson diff --git a/src/main.cpp b/src/main.cpp index 9914731..968b28d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,12 +1,14 @@ #include #include #include +#include #include "creds.h" // #define B_WIFI_AP const byte led = 2; bool ledOn = false; +String selectedFile = ""; AsyncWebServer server(80); @@ -26,6 +28,42 @@ void onPlay(AsyncWebServerRequest *request) request->send(200); } +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); +} + void setup() { // Setup serial @@ -71,6 +109,8 @@ void setup() // Server server.on("/play", HTTP_GET, onPlay); + server.on("/available-files", HTTP_GET, onAvailableFiles); + server.on("/select-file", HTTP_POST, onSelectFile); server.serveStatic("/", SPIFFS, "/www/").setDefaultFile("index.html"); server.begin(); Serial.println("Server ready!");