async-esp-fs-webserver icon indicating copy to clipboard operation
async-esp-fs-webserver copied to clipboard

clearOptions Method missing

Open petricf opened this issue 6 months ago • 12 comments

There is following code in your example "customHTML":

#if CLEAR_OPTIONS if (server.clearOptions()) ESP.restart(); #endif

The method "clearOptions" is not defined in AsyncFsWebServer as thir compile error states:

/tmp/.arduinoIDE-unsaved2025526-1386448-17i2m7d.0exzg/customOptions/customOptions.ino: In function 'void setup()': /tmp/.arduinoIDE-unsaved2025526-1386448-17i2m7d.0exzg/customOptions/customOptions.ino:147:10: error: 'class AsyncFsWebServer' has no member named 'clearOptions'

Remark: The old library esp_fs_webserver has this method implemented.

petricf avatar Jun 26 '25 08:06 petricf

Hi, yes, in fact there isn't a built-in method to delete the settings. I think I'll add it in a future release. Anyway, it's just a file, so you can simply delete it and then restart the ESP.

For example, if you're using LittleFS:

#if CLEAR_OPTIONS
if (LittleFS.remove(server.getConfiFileName())) {
  ESP.restart();
}

#endif

cotestatnt avatar Jun 26 '25 09:06 cotestatnt

Would this not result in an endless loop ?

Delete config -> restart -> running ... some later reboot -> delete config -> restart ...

petricf avatar Jun 26 '25 09:06 petricf

Tested - used example customOptions. Renamed option box.

Code inserted after:

// Try to connect to WiFi (will start AP if not connected after timeout) if (!server->startWiFi(10000)) { Serial.println("\nWiFi not connected! Starting AP mode..."); server->startCaptivePortal("ESP_AP", "123456789", "/setup"); captiveRun = true; }

The "My Options" tab was not renamed.

petricf avatar Jun 26 '25 09:06 petricf

Code added:

Serial.printf("removing %s", server->getConfiFileName()); if (LittleFS.remove(server->getConfiFileName())) { Serial.println("restarting ..."); ESP.restart(); }

The console output:

11:51:45.567 -> removing /config/config.jsonrestarting ... 11:51:45.696 -> B΃K\�L �%��w�B�[+�KÁY�P1�% )n@�5Y 11:51:45.696 -> !LZP�В�A�-�Y�9��% )��@�Y 11:51:45.696 -> �HS JJ�P9�N��VZr��)�� 11:51:45.759 -> rf cal sector: 252 11:51:45.759 -> freq trace enable 0 11:51:45.759 -> rf[112] : 0�/config/config/config.json 11:51:45.791 -> 11:51:45.791 -> Listing directory: / 11:51:45.791 -> 11:51:45.791 -> Listing directory: config 11:51:45.791 -> |__ FILE: raw-html-buttons.htm (82 bytes) 11:51:45.823 -> |__ FILE: raw-javascript-js.js (502 bytes) 11:51:45.823 -> Config file not exist 11:51:45.823 -> Application options NOT loaded! 11:51:45.823 -> 11:51:45.823 -> [E][AsyncFsWebServer.cpp:617] startWiFi(): File not found, will be created new configuration file 11:51:50.912 -> ESP Web Server started on IP Address: 192.168.2.190 11:51:50.912 -> This is "customOptions.ino" example. 11:51:50.944 -> Open /setup page to configure optional parameters. 11:51:50.944 -> Open /edit page to view, edit or upload example or your custom webserver source files.

Still the old menu entries are displayed.

Currently i have no idea where to look next.

petricf avatar Jun 26 '25 09:06 petricf

Could you share the complete code? As soon as I have some free time, I'll try to reproduce it.

cotestatnt avatar Jun 26 '25 10:06 cotestatnt

I had such problems too, there is a problem with the caching of the browser. The Browser always has old data. In my case it helps to delete the browser cache.

willi-uce avatar Jun 26 '25 10:06 willi-uce

The values in the config seems to be ok. An reload request under "My Options" displays the additional option "Speaker state" i've added:

16:37:15.512 -> This are the current values stored: 16:37:15.512 -> 16:37:15.512 -> Speaker state: on 16:37:15.512 -> LED pin value: 2 16:37:15.512 -> Bool value: true 16:37:15.512 -> Long value: 1234567890 16:37:15.512 -> Float value: 15.500 16:37:15.512 -> String value: Test option String 16:37:15.512 -> Dropdown selected value: Monday 16:37:15.544 -> 16:37:15.544 -> Application option loaded after web request

But this option does not show up in the browser (Firefox).

If i press the shift button when requesting reload by firefox should thow away the cached data. But it displays the /setup-Tab afterwards.

petricf avatar Jun 26 '25 14:06 petricf

The shift button does not change anything in my browser, only delete chronik (for example last hour) helps.

willi-uce avatar Jun 26 '25 15:06 willi-uce

Ok, i took an other PC never used to display this page. Here the output is as requested - everything ok.

So it seems to be a browser cache issue.

Not a good solution. The user has to know that the UI has changed an clear the cache of his browser. How other web servers overcome this ? Sending a special tag when delivering the page ? Perhaps by sending a TTL (time to live) ?

petricf avatar Jun 26 '25 15:06 petricf

Have you tested what is happening when you change values in your options page? When I change a value I have always to clear the browser cache to see the new value on the options page. (Linux, firefox browser, not tested with windows browsers)

willi-uce avatar Jun 26 '25 19:06 willi-uce

I may be wrong - i got the impression that websockets may help here.

petricf avatar Jun 26 '25 19:06 petricf

For me the following solution worked (see https://forum.arduino.cc/t/redirect-in-asyncwebserver-does-not-reload-the-actual-page/1292677 ): Before server.init() I added: DefaultHeaders::Instance().addHeader("Cache-Control", "no-cache, no-store, must-revalidate"); DefaultHeaders::Instance().addHeader("Pragma", "no-cache"); DefaultHeaders::Instance().addHeader("Expires", "0"); After compiling and saving the program to esp you have to clean once the cache of the browser.

willi-uce avatar Jun 27 '25 11:06 willi-uce