Last active
January 12, 2023 06:40
-
-
Save cotestatnt/23e674e1bd14be5f4905b243ac15f7f1 to your computer and use it in GitHub Desktop.
esp-fs-webserver example with "reload config" and "restart" handler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <esp-fs-webserver.h> // https://github.com/cotestatnt/esp-fs-webserver | |
#include <FS.h> | |
#include <LittleFS.h> | |
#define FILESYSTEM LittleFS | |
// Test "options" values | |
uint32_t longVar = 1234567890; | |
String stringVar = "Test option String"; | |
uint8_t ledPin = LED_BUILTIN; | |
bool boolVar = true; | |
// Timezone definition to get properly time from NTP server | |
#define MYTZ "CET-1CEST,M3.5.0,M10.5.0/3" | |
struct tm Time; | |
WebServer server(80); | |
FSWebServer myWebServer(FILESYSTEM, server); | |
static const char button_html[] PROGMEM = R"EOF( | |
<button class='button' onclick=window.open('/restart')>Restart ESP</button> | |
<button class='button' style= 'background-color: crimson;' onclick=window.open('/reload')>Reload config</button> | |
<br><br> | |
)EOF"; | |
//////////////////////////////// Filesystem ///////////////////////////////////////// | |
void startFilesystem() { | |
// FILESYSTEM INIT | |
if (FILESYSTEM.begin()) { | |
File root = FILESYSTEM.open("/", "r"); | |
File file = root.openNextFile(); | |
while (file) { | |
const char* fileName = file.name(); | |
size_t fileSize = file.size(); | |
Serial.printf("FS File: %s, size: %lu\n", fileName, (long unsigned)fileSize); | |
file = root.openNextFile(); | |
} | |
Serial.println(); | |
} else { | |
Serial.println("ERROR on mounting filesystem. It will be formmatted!"); | |
FILESYSTEM.format(); | |
ESP.restart(); | |
} | |
} | |
//////////////////// Load application options from filesystem //////////////////// | |
bool loadApplicationConfig() { | |
StaticJsonDocument<1024> doc; | |
File file = FILESYSTEM.open("/config.json", "r"); | |
if (file) { | |
DeserializationError error = deserializeJson(doc, file); | |
file.close(); | |
if (!error) { | |
Serial.println(F("Deserializing config JSON..")); | |
boolVar = doc["A bool var"]; | |
stringVar = doc["A String var"].as<String>(); | |
longVar = doc["A long var"]; | |
ledPin = doc["LED Pin"]; | |
serializeJsonPretty(doc, Serial); | |
Serial.println(); | |
return true; | |
} else { | |
Serial.println(F("Failed to deserialize JSON. File could be corrupted")); | |
Serial.println(error.c_str()); | |
} | |
} | |
return false; | |
} | |
//////////////////////////// HTTP Request Handlers //////////////////////////////////// | |
void handleLoadConfig() { | |
WebServerClass* webRequest = myWebServer.getRequest(); | |
String reply; | |
if (loadApplicationConfig()) { | |
reply = "Updated configuration file reloaded"; | |
} else | |
reply = "ERROR"; | |
webRequest->send(200, "text/plain", reply); | |
} | |
void handleRestart() { | |
WebServerClass* webRequest = myWebServer.getRequest(); | |
webRequest->send(200, "text/plain", "ESP now will be restarted"); | |
delay(500); | |
ESP.restart(); | |
} | |
void setup() { | |
Serial.begin(115200); | |
// FILESYSTEM INIT | |
startFilesystem(); | |
// Try to connect to flash stored SSID, start AP if fails after timeout | |
myWebServer.startWiFi(15000, "ESP_AP", "123456789"); | |
Serial.println("\nWiFi connected\nIP address: "); | |
Serial.println(WiFi.localIP()); | |
// Load configuration (if not present, default will be created when webserver will start) | |
if (loadApplicationConfig()) { | |
Serial.println(F("Application option loaded")); | |
} else { | |
Serial.println(F("Application NOT loaded!")); | |
Serial.print(F("Open http://")); | |
Serial.print(WiFi.localIP()); | |
Serial.println(F("/setup to configure parameters")); | |
} | |
// Add custom HTML to setup page | |
myWebServer.addOption(FILESYSTEM, "raw-html-button", button_html); | |
// Configure /setup page and start Web Server | |
myWebServer.addOption(FILESYSTEM, "LED Pin", ledPin); | |
myWebServer.addOption(FILESYSTEM, "A long var", longVar); | |
myWebServer.addOption(FILESYSTEM, "A String var", stringVar.c_str()); | |
myWebServer.addOption(FILESYSTEM, "A bool var", boolVar); | |
// Add custom page handlers to webserver | |
myWebServer.addHandler("/reload", HTTP_GET, handleLoadConfig); | |
myWebServer.addHandler("/restart", HTTP_GET, handleRestart); | |
// Start webserver | |
if (myWebServer.begin()) { | |
Serial.print(F("ESP Web Server started: ")); | |
Serial.println(F("Open /setup page to configure optional parameters")); | |
Serial.println(F("Open /edit page to view and edit files")); | |
Serial.println(F("Open /update page to upload firmware and filesystem updates")); | |
} | |
} | |
void loop() { | |
myWebServer.run(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment