Implement local web server on SoftAP for uploading PNG/MP3/WAV files directly to SD card
This commit is contained in:
148
sound_picture_app/Web_Server.cpp
Normal file
148
sound_picture_app/Web_Server.cpp
Normal file
@@ -0,0 +1,148 @@
|
|||||||
|
#include "Web_Server.h"
|
||||||
|
#include <WiFi.h>
|
||||||
|
#include <WebServer.h>
|
||||||
|
#include "SD_Card.h"
|
||||||
|
#include "Display_PNG.h"
|
||||||
|
#include "Audio_Player.h"
|
||||||
|
|
||||||
|
WebServer server(80);
|
||||||
|
File uploadFile;
|
||||||
|
|
||||||
|
const char* uploadPage = R"rawliteral(
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Photo Upload Panel</title>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
|
||||||
|
background: linear-gradient(135deg, #0f172a, #1e293b);
|
||||||
|
color: #f8fafc;
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
align-items: center;
|
||||||
|
height: 100vh;
|
||||||
|
margin: 0;
|
||||||
|
}
|
||||||
|
.card {
|
||||||
|
background: rgba(30, 41, 59, 0.7);
|
||||||
|
backdrop-filter: blur(10px);
|
||||||
|
border: 1px solid rgba(255, 255, 255, 0.1);
|
||||||
|
border-radius: 16px;
|
||||||
|
padding: 32px;
|
||||||
|
width: 90%;
|
||||||
|
max-width: 400px;
|
||||||
|
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.3);
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
h1 { font-size: 24px; margin-bottom: 24px; color: #38bdf8; }
|
||||||
|
.input-group { margin-bottom: 20px; }
|
||||||
|
input[type="file"] { display: none; }
|
||||||
|
.file-label {
|
||||||
|
display: inline-block;
|
||||||
|
padding: 12px 24px;
|
||||||
|
background: #0284c7;
|
||||||
|
color: white;
|
||||||
|
border-radius: 8px;
|
||||||
|
cursor: pointer;
|
||||||
|
font-weight: 600;
|
||||||
|
transition: background 0.2s;
|
||||||
|
}
|
||||||
|
.file-label:hover { background: #0369a1; }
|
||||||
|
button {
|
||||||
|
width: 100%;
|
||||||
|
padding: 12px;
|
||||||
|
background: #10b981;
|
||||||
|
color: white;
|
||||||
|
border: none;
|
||||||
|
border-radius: 8px;
|
||||||
|
font-size: 16px;
|
||||||
|
font-weight: 600;
|
||||||
|
cursor: pointer;
|
||||||
|
transition: background 0.2s;
|
||||||
|
margin-top: 10px;
|
||||||
|
}
|
||||||
|
button:hover { background: #059669; }
|
||||||
|
#status { margin-top: 15px; font-size: 14px; color: #94a3b8; }
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="card">
|
||||||
|
<h1>Upload Photo / Audio</h1>
|
||||||
|
<form method="POST" action="/upload" enctype="multipart/form-data" id="uploadForm">
|
||||||
|
<div class="input-group">
|
||||||
|
<label for="file" class="file-label">Choose File (.png / .mp3 / .wav)</label>
|
||||||
|
<input type="file" id="file" name="file" accept=".png,.mp3,.wav" onchange="updateFilename()">
|
||||||
|
</div>
|
||||||
|
<div id="filename" style="margin-bottom: 20px; font-size: 14px; color: #38bdf8;">No file selected</div>
|
||||||
|
<button type="submit">Upload to Frame</button>
|
||||||
|
</form>
|
||||||
|
<div id="status">Supports PNG (172x320 max) and MP3/WAV.</div>
|
||||||
|
</div>
|
||||||
|
<script>
|
||||||
|
function updateFilename() {
|
||||||
|
const fileInput = document.getElementById('file');
|
||||||
|
const filenameDiv = document.getElementById('filename');
|
||||||
|
if (fileInput.files.length > 0) {
|
||||||
|
filenameDiv.textContent = fileInput.files[0].name;
|
||||||
|
} else {
|
||||||
|
filenameDiv.textContent = 'No file selected';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
)rawliteral";
|
||||||
|
|
||||||
|
void handleUpload() {
|
||||||
|
HTTPUpload& upload = server.upload();
|
||||||
|
if (upload.status == UPLOAD_FILE_START) {
|
||||||
|
String filename = upload.filename;
|
||||||
|
if (!filename.startsWith("/")) {
|
||||||
|
filename = "/" + filename;
|
||||||
|
}
|
||||||
|
printf("WebUpload: Starting upload of %s\r\n", filename.c_str());
|
||||||
|
uploadFile = SD_MMC.open(filename, "w");
|
||||||
|
}
|
||||||
|
else if (upload.status == UPLOAD_FILE_WRITE) {
|
||||||
|
if (uploadFile) {
|
||||||
|
uploadFile.write(upload.buf, upload.currentSize);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (upload.status == UPLOAD_FILE_END) {
|
||||||
|
if (uploadFile) {
|
||||||
|
uploadFile.close();
|
||||||
|
printf("WebUpload: Successfully uploaded %s (%d bytes)\r\n", upload.filename.c_str(), upload.totalSize);
|
||||||
|
// Re-scan SD card folders to index the new file
|
||||||
|
Search_Images("/");
|
||||||
|
Search_Audio("/");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void initWebServer() {
|
||||||
|
// Start WiFi Access Point
|
||||||
|
WiFi.softAP("ESP32-Photo-Frame");
|
||||||
|
IPAddress IP = WiFi.softAPIP();
|
||||||
|
printf("WiFi AP Started. SSID: 'ESP32-Photo-Frame'\r\n");
|
||||||
|
printf("Web Server IP address: %s\r\n", IP.toString().c_str());
|
||||||
|
|
||||||
|
// Serve Page
|
||||||
|
server.on("/", HTTP_GET, []() {
|
||||||
|
server.send(200, "text/html", uploadPage);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Handle file uploads
|
||||||
|
server.on("/upload", HTTP_POST, []() {
|
||||||
|
// Redirect back to main upload page on success
|
||||||
|
server.sendHeader("Location", "/");
|
||||||
|
server.send(303);
|
||||||
|
}, handleUpload);
|
||||||
|
|
||||||
|
server.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
void handleWebServer() {
|
||||||
|
server.handleClient();
|
||||||
|
}
|
||||||
5
sound_picture_app/Web_Server.h
Normal file
5
sound_picture_app/Web_Server.h
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <Arduino.h>
|
||||||
|
|
||||||
|
void initWebServer();
|
||||||
|
void handleWebServer();
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
#include "SD_Card.h"
|
#include "SD_Card.h"
|
||||||
#include "Display_PNG.h"
|
#include "Display_PNG.h"
|
||||||
#include "Audio_Player.h"
|
#include "Audio_Player.h"
|
||||||
|
#include "Web_Server.h"
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
#define BOOT_KEY_PIN 9
|
#define BOOT_KEY_PIN 9
|
||||||
@@ -79,6 +80,9 @@ void setup() {
|
|||||||
Display_Image_By_Index("/", 0);
|
Display_Image_By_Index("/", 0);
|
||||||
currentState = STATE_IDLE;
|
currentState = STATE_IDLE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Start the SoftAP and local Web Server
|
||||||
|
initWebServer();
|
||||||
}
|
}
|
||||||
|
|
||||||
void playCurrentPair() {
|
void playCurrentPair() {
|
||||||
@@ -265,6 +269,9 @@ void loop() {
|
|||||||
|
|
||||||
lastButtonState = currentButtonReading;
|
lastButtonState = currentButtonReading;
|
||||||
|
|
||||||
|
// Handle incoming web server clients
|
||||||
|
handleWebServer();
|
||||||
|
|
||||||
// Update status RGB LED
|
// Update status RGB LED
|
||||||
updateLEDStatus();
|
updateLEDStatus();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user