Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <esp-fs-webserver.h> // https://github.com/cotestatnt/esp-fs-webserver
- #include <FS.h>
- #include <LittleFS.h>
- #define FILESYSTEM LittleFS
- // Test "options" values
- bool boolVar = true;
- uint32_t longVar = 1234567890;
- String stringVar = "Test option String";
- uint8_t ledPin = 4;
- // Timezone definition to get properly time from NTP server
- #define MYTZ "CET-1CEST,M3.5.0,M10.5.0/3"
- struct tm Time;
- 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('/config')>Open link</button>
- )EOF";
- void runWebServer(uint32_t timeout) {
- WebServer server(80);
- FSWebServer myWebServer(FILESYSTEM, server);
- WiFi.mode(WIFI_AP);
- WiFi.persistent(false);
- WiFi.softAP("ESP32_AP", "123456789");
- Serial.println("\nAP started\nIP address: ");
- Serial.println(WiFi.softAPIP());
- // 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);
- // 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"));
- }
- uint32_t tTime = millis();
- while( millis() - tTime < timeout) {
- myWebServer.run();
- // Se c'รจ un client connesso allunghiamo il timeout di 30 secondi
- if (WiFi.softAPgetStationNum())
- timeout += 30000;
- }
- WiFi.mode(WIFI_STA);
- Serial.print(F("ESP Web Server stopped\n Clear memory on exit"));
- WiFi.begin();
- }
- //////////////////////////////// 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;
- }
- void setup() {
- Serial.begin(115200);
- // FILESYSTEM INIT
- startFilesystem();
- // 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"));
- }
- runWebServer(60000);
- }
- void loop() {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement