Berhasil & Stabil
This commit is contained in:
20
src/main.cpp
20
src/main.cpp
@@ -21,6 +21,7 @@
|
|||||||
#include <tcpip_adapter.h>
|
#include <tcpip_adapter.h>
|
||||||
#include <ArduinoOTA.h>
|
#include <ArduinoOTA.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
#include <esp_task_wdt.h>
|
||||||
|
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
#include <DFRobotDFPlayerMini.h>
|
#include <DFRobotDFPlayerMini.h>
|
||||||
@@ -264,8 +265,20 @@ void broadcastStatus() {
|
|||||||
// Web server endpoints
|
// Web server endpoints
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
void initWebServer() {
|
void initWebServer() {
|
||||||
|
// Serve static files from LittleFS
|
||||||
server.serveStatic("/", LittleFS, "/").setDefaultFile("index.html");
|
server.serveStatic("/", LittleFS, "/").setDefaultFile("index.html");
|
||||||
|
|
||||||
|
// Add a catch-all handler for 404 errors to prevent file system errors
|
||||||
|
server.onNotFound([](AsyncWebServerRequest *request){
|
||||||
|
// Check if it's an API request that doesn't exist
|
||||||
|
if (request->url().startsWith("/api/")) {
|
||||||
|
request->send(404, "text/plain", "API endpoint not found");
|
||||||
|
} else {
|
||||||
|
// For non-API requests, try to serve the file
|
||||||
|
request->send(LittleFS, request->url(), "text/html");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// settings page (no longer protected - has login form)
|
// settings page (no longer protected - has login form)
|
||||||
server.on("/setting.html", HTTP_GET, [](AsyncWebServerRequest *req){
|
server.on("/setting.html", HTTP_GET, [](AsyncWebServerRequest *req){
|
||||||
req->send(LittleFS, "/setting.html", "text/html");
|
req->send(LittleFS, "/setting.html", "text/html");
|
||||||
@@ -488,6 +501,10 @@ void resetAdminToDefault() {
|
|||||||
// Setup & Loop
|
// Setup & Loop
|
||||||
//////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////
|
||||||
void setup(){
|
void setup(){
|
||||||
|
// Initialize Task Watchdog Timer (TWDT) to prevent watchdog resets
|
||||||
|
esp_task_wdt_init(30, true); // 30 seconds timeout, panic on timeout
|
||||||
|
esp_task_wdt_add(NULL); // Add current task to watchdog
|
||||||
|
|
||||||
pinMode(STATUS_LED_PIN, OUTPUT);
|
pinMode(STATUS_LED_PIN, OUTPUT);
|
||||||
digitalWrite(STATUS_LED_PIN, LOW);
|
digitalWrite(STATUS_LED_PIN, LOW);
|
||||||
pinMode(RELAY_PIN, OUTPUT);
|
pinMode(RELAY_PIN, OUTPUT);
|
||||||
@@ -682,5 +699,8 @@ void loop(){
|
|||||||
digitalWrite(BUZZER_PIN, LOW);
|
digitalWrite(BUZZER_PIN, LOW);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Reset Task Watchdog Timer to prevent timeout
|
||||||
|
esp_task_wdt_reset();
|
||||||
|
|
||||||
delay(10); // tiny yield
|
delay(10); // tiny yield
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user