Advertisement
TolentinoCotesta

esp-fs-webserver

Feb 7th, 2023 (edited)
689
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.93 KB | None | 0 0
  1. #include <esp-fs-webserver.h>  // https://github.com/cotestatnt/esp-fs-webserver
  2.  
  3. #include <FS.h>
  4. #include <LittleFS.h>
  5. #define FILESYSTEM LittleFS
  6.  
  7. // Test "options" values
  8. bool boolVar = true;
  9. uint32_t longVar = 1234567890;
  10. String stringVar = "Test option String";
  11. uint8_t ledPin = 4;
  12.  
  13. // Timezone definition to get properly time from NTP server
  14. #define MYTZ "CET-1CEST,M3.5.0,M10.5.0/3"
  15. struct tm Time;
  16.  
  17. static const char button_html[] PROGMEM = R"EOF(
  18. <button class='button' onclick=window.open('/restart')>Restart ESP</button>
  19. <button class='button' style= 'background-color: crimson;' onclick=window.open('/config')>Open link</button>
  20. )EOF";
  21.  
  22. void runWebServer(uint32_t timeout) {
  23.   WebServer server(80);
  24.   FSWebServer myWebServer(FILESYSTEM, server);
  25.  
  26.   WiFi.mode(WIFI_AP);
  27.   WiFi.persistent(false);
  28.   WiFi.softAP("ESP32_AP", "123456789");
  29.  
  30.   Serial.println("\nAP started\nIP address: ");
  31.   Serial.println(WiFi.softAPIP());
  32.  
  33.   // Add custom HTML to setup page
  34.   myWebServer.addOption(FILESYSTEM, "raw-html-button", button_html);
  35.  
  36.   // Configure /setup page and start Web Server
  37.   myWebServer.addOption(FILESYSTEM, "LED Pin", ledPin);
  38.   myWebServer.addOption(FILESYSTEM, "A long var", longVar);
  39.   myWebServer.addOption(FILESYSTEM, "A String var", stringVar.c_str());
  40.   myWebServer.addOption(FILESYSTEM, "A bool var", boolVar);
  41.  
  42.   // Start webserver
  43.   if (myWebServer.begin()) {
  44.     Serial.print(F("ESP Web Server started: "));
  45.     Serial.println(F("Open /setup page to configure optional parameters"));
  46.     Serial.println(F("Open /edit page to view and edit files"));
  47.     Serial.println(F("Open /update page to upload firmware and filesystem updates"));
  48.   }
  49.  
  50.   uint32_t tTime = millis();
  51.   while( millis() - tTime < timeout) {
  52.     myWebServer.run();
  53.  
  54.     // Se c'รจ un client connesso allunghiamo il timeout di 30 secondi
  55.     if (WiFi.softAPgetStationNum())
  56.       timeout += 30000;
  57.   }
  58.  
  59.   WiFi.mode(WIFI_STA);
  60.   Serial.print(F("ESP Web Server stopped\n Clear memory on exit"));
  61.   WiFi.begin();
  62. }
  63.  
  64.  
  65. ////////////////////////////////  Filesystem  /////////////////////////////////////////
  66. void startFilesystem() {
  67.   // FILESYSTEM INIT
  68.   if (FILESYSTEM.begin()) {
  69.     File root = FILESYSTEM.open("/", "r");
  70.     File file = root.openNextFile();
  71.     while (file) {
  72.       const char* fileName = file.name();
  73.       size_t fileSize = file.size();
  74.       Serial.printf("FS File: %s, size: %lu\n", fileName, (long unsigned)fileSize);
  75.       file = root.openNextFile();
  76.     }
  77.     Serial.println();
  78.   } else {
  79.     Serial.println("ERROR on mounting filesystem. It will be formmatted!");
  80.     FILESYSTEM.format();
  81.     ESP.restart();
  82.   }
  83. }
  84.  
  85.  
  86. ////////////////////  Load application options from filesystem  ////////////////////
  87. bool loadApplicationConfig() {
  88.   StaticJsonDocument<1024> doc;
  89.   File file = FILESYSTEM.open("/config.json", "r");
  90.   if (file) {
  91.     DeserializationError error = deserializeJson(doc, file);
  92.     file.close();
  93.     if (!error) {
  94.       Serial.println(F("Deserializing config JSON.."));
  95.       boolVar = doc["A bool var"];
  96.       stringVar = doc["A String var"].as<String>();
  97.       longVar = doc["A long var"];
  98.       ledPin = doc["LED Pin"];
  99.       serializeJsonPretty(doc, Serial);
  100.       Serial.println();
  101.       return true;
  102.     } else {
  103.       Serial.println(F("Failed to deserialize JSON. File could be corrupted"));
  104.       Serial.println(error.c_str());
  105.     }
  106.   }
  107.   return false;
  108. }
  109.  
  110. void setup() {
  111.   Serial.begin(115200);
  112.  
  113.   // FILESYSTEM INIT
  114.   startFilesystem();
  115.  
  116.   // Load configuration (if not present, default will be created when webserver will start)
  117.   if (loadApplicationConfig()) {
  118.     Serial.println(F("Application option loaded"));
  119.   } else {
  120.     Serial.println(F("Application NOT loaded!"));
  121.     Serial.print(F("Open http://"));
  122.     Serial.print(WiFi.localIP());
  123.     Serial.println(F("/setup to configure parameters"));
  124.   }
  125.  
  126.   runWebServer(60000);
  127. }
  128.  
  129.  
  130. void loop() {
  131.  
  132. }
  133.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement