Handle WebServer
This commit is contained in:
@@ -15,9 +15,10 @@
|
||||
<div class="container">
|
||||
<h1>ESP8266 DCC Command Station</h1>
|
||||
<p>This is your ESP8266 DCC Command Station.</p>
|
||||
<p>Pilotage train <a href="mouse.html">ici</a>.</p>
|
||||
<p>Configure WiFi, manage XpressNet, and control your layout from here.</p>
|
||||
<p>For documentation, see <a href="/xpressnet.html
|
||||
">XpressNet Commands</a> and <a href="/README.md">Hardware Setup</a>.</p>
|
||||
<p>For documentation, see <a href="/xpressnet.html">XpressNet Commands</a>
|
||||
and <a href="/README.md">Hardware Setup</a>.</p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
18
src/WebServer.cpp
Normal file
18
src/WebServer.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
#include <Arduino.h>
|
||||
#include <LittleFS.h>
|
||||
#include "main.h"
|
||||
#include "WebServer.h"
|
||||
|
||||
|
||||
void handlePage(char *page) {
|
||||
if (LittleFS.exists(page)) {
|
||||
File file = LittleFS.open(page, "r");
|
||||
if (file) {
|
||||
String html = file.readString();
|
||||
server.send(200, "text/html", html);
|
||||
file.close();
|
||||
return;
|
||||
}
|
||||
}
|
||||
server.send(404, "text/plain", "Page not found");
|
||||
}
|
||||
3
src/WebServer.h
Normal file
3
src/WebServer.h
Normal file
@@ -0,0 +1,3 @@
|
||||
extern ESP8266WebServer server;
|
||||
|
||||
void handlePage(char *page);
|
||||
69
src/main.cpp
69
src/main.cpp
@@ -24,58 +24,6 @@ Adafruit_SSD1306 *display;
|
||||
// Create web server instance on port 80
|
||||
ESP8266WebServer server(80);
|
||||
|
||||
// // HTML content for welcome page
|
||||
// const char* welcome_html = R"HTML(
|
||||
// <!DOCTYPE html>
|
||||
// <html lang=\"en\">
|
||||
// <head>
|
||||
// <meta charset=\"UTF-8\">
|
||||
// <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">
|
||||
// <title>Welcome to ESP8266 DCC Command Station</title>
|
||||
// <style>
|
||||
// body { font-family: Arial, sans-serif; background: #f4f4f4; color: #333; margin: 0; padding: 0; }
|
||||
// .container { max-width: 600px; margin: 40px auto; background: #fff; border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.1); padding: 32px; }
|
||||
// h1 { color: #0077cc; }
|
||||
// p { font-size: 1.2em; }
|
||||
// </style>
|
||||
// </head>
|
||||
// <body>
|
||||
// <div class=\"container\">
|
||||
// <h1>Welcome!</h1>
|
||||
// <p>This is your ESP8266 DCC Command Station.</p>
|
||||
// <p>Configure WiFi, manage XpressNet, and control your layout from here.</p>
|
||||
// <p>For documentation, see <a href=\"/xpressnet.md\">XpressNet Commands</a> and <a href=\"/README.md\">Hardware Setup</a>.</p>
|
||||
// </div>
|
||||
// </body>
|
||||
// </html>
|
||||
// )HTML";
|
||||
|
||||
void handleWelcome() {
|
||||
if (LittleFS.exists("/index.html")) {
|
||||
File file = LittleFS.open("/index.html", "r");
|
||||
if (file) {
|
||||
String html = file.readString();
|
||||
server.send(200, "text/html", html);
|
||||
file.close();
|
||||
return;
|
||||
}
|
||||
}
|
||||
server.send(404, "text/plain", "Welcome page not found");
|
||||
}
|
||||
|
||||
void handleXpressNetPage() {
|
||||
if (LittleFS.exists("/xpressnet.html")) {
|
||||
File file = LittleFS.open("/xpressnet.html", "r");
|
||||
if (file) {
|
||||
String html = file.readString();
|
||||
server.send(200, "text/html", html);
|
||||
file.close();
|
||||
return;
|
||||
}
|
||||
}
|
||||
server.send(404, "text/plain", "XpressNet page not found");
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
delay(100);
|
||||
@@ -96,19 +44,12 @@ void setup() {
|
||||
}
|
||||
} else {
|
||||
Serial.println("[DEBUG] LittleFS mounted");
|
||||
// List files in LittleFS root for debug
|
||||
// Serial.println("[DEBUG] LittleFS root directory:");
|
||||
// Dir dir = LittleFS.openDir("/");
|
||||
// while (dir.next()) {
|
||||
// Serial.print(" ");
|
||||
// Serial.print(dir.fileName());
|
||||
// Serial.print(" (size: ");
|
||||
// Serial.print(dir.fileSize());
|
||||
// Serial.println(")");
|
||||
// }
|
||||
}
|
||||
server.on("/", handleWelcome);
|
||||
server.on("/xpressnet.html", handleXpressNetPage);
|
||||
server.on("/", []() { handlePage("/index.html"); });
|
||||
server.on("/xpressnet.html", []() { handlePage("/xpressnet.html"); });
|
||||
server.on("/mouse.html", []() { handlePage("/mouse.html"); });
|
||||
server.on("/js/script.js", []() { handlePage("/js/script.js"); });
|
||||
server.on("/css/style.css", []() { handlePage("/css/style.css"); });
|
||||
server.begin();
|
||||
Serial.println("[DEBUG] Web server started");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user