Files
ESP8266-Station/src/main.cpp
2026-02-23 10:53:32 +01:00

121 lines
3.9 KiB
C++

#include <Arduino.h>
#include <Adafruit_SSD1306.h>
#include <Wire.h>
#include "DisplayHelper.h"
#include "WiFiManagerHelper.h"
#include "main.h"
#include <ESP8266WebServer.h>
#include <LittleFS.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C // If not work please scan the bus
#define OLED_SDA 14 // D6
#define OLED_SCL 12 // D5
// Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Adafruit_SSD1306 *display;
// Mode for command station operation
// Remove conflicting definition, use extern from main.h
// Mode currentMode = MODE_OFF;
// 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);
Serial.println("[DEBUG] Serial started at 115200 baud");
displayInit();
Serial.println("[DEBUG] OLED display initialized");
setupWiFiManager();
if (!LittleFS.begin()) {
Serial.println("[ERROR] LittleFS mount failed");
while(true) {
display->clearDisplay();
display->setTextSize(1);
display->setTextColor(SSD1306_WHITE);
display->setCursor(0, 0);
display->println("LittleFS Mount Failed!");
display->display();
delay(1000);
}
} 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.begin();
Serial.println("[DEBUG] Web server started");
}
void loop() {
server.handleClient();
delay(1000);
}