For ESP8266/ESP32 does there exist a "framework" make preferences adjustable through webinterface?

Hi everybody,

I have used things like

  • ESPAsync-WifiManager
  • library preferences.h
  • ESP-DASH with "text-inputcards"
    • ESP-DASH creates the html-code in the backround for having a text-inputfield on a website

each of the above options alone.

Beeing able to connect to an ESP8266 / ESP32 through a webinterface to configure parameters would be a comfortable way to adjust user-adjustable parameters.

Does somebody know of a library that encapsulates this functionality all at once?
I mean some like
you just write a few lines of code

define input-type

  • boolean (which results in a checkbox)

  • integernumber (website inputfield)

  • float-number (website inputfield)

  • text ((website inputfield)

  • define an epxlaining title for the checkbox or inputfield

  • define position on the screen (or position is given through the order)

  • define preference-ID and default-value

and the library does all the rest which is:
readout preference-value if not found use default value
create a "save" and a "cancle"-button
if button save is clicked store all values as dislayed on the website

Of course - if all this is done by the library - the website-design is preconfigured.
But me personal I don't care much about the design. The thing has to be usable and effective
through just writing that things as code that are individual in each project.

best regards Stefan

Yes, mine!!! :rofl: :rofl:

  • Filesystem based webserver
  • WiFi manager with captive portal
  • Online editor (webserver sources)
  • Custom options
  • Custom HTML injection in the /setup page

WiFiManager supports custom parameters and can be run on demand.
I use it in this project:
https://github.com/JAndrassy/SunriseClock/blob/main/SunriseClock/SunriseClock.ino

Hi @cotestatnt

first of all thank you very much for pointing me to your library.
You put a lot of work into it. I can honor this of course. Your library looks promising. I did a first test with the example custom options

I changed the parameters

I saved the changed parameters

Then I pressed the reset-button. But then the default settings are shown again.
Huh? That's not what I would expect from an example. I would expect to see the changed parameters after a reset.

I want to contribute to your project by improving the examples. What do I have to change to make the example show the changed parameters?

This is the serial otput

Connecting to FRITZ!Box 7490
.......FS File: config.json, size: 113

ESP Web Server started on IP Address: 192.168.178.96
Open /setup page to configure optional parameters
Open /edit page to view and edit files
Open /update page to upload firmware and filesystem updates
Deserializing config JSON..
{
  "LED Pin": 2,
  "A long var": 1234567890,
  "A String var": "Test option String",
  "A bool var": true
}
Application option loaded
handleFileRead: /config.json
/config.json

handleFileUpload Name: /config.json
Upload: START, filename: /config.json
Upload: WRITE, Bytes: 95
Upload: END, Size: 95
handleFileRead: /config.json
handleFileRead: /config.json

then I pressed the resetbutton. But then the default values are loaded again

Connecting to FRITZ!Box 7490
.......FS File: config.json, size: 113

ESP Web Server started on IP Address: 192.168.178.96
Open /setup page to configure optional parameters
Open /edit page to view and edit files
Open /update page to upload firmware and filesystem updates
Deserializing config JSON..
{
  "LED Pin": 2,
  "A long var": 1234567890,
  "A String var": "Test option String",
  "A bool var": true
}
Application option loaded
handleFileRead: /config.json
/config.json

handleFileUpload Name: /config.json
Upload: START, filename: /config.json
Upload: WRITE, Bytes: 95
Upload: END, Size: 95
handleFileRead: /config.json
handleFileRead: /config.json

Connecting to FRITZ!Box 7490
.......FS File: config.json, size: 113

ESP Web Server started on IP Address: 192.168.178.96
Open /setup page to configure optional parameters
Open /edit page to view and edit files
Open /update page to upload firmware and filesystem updates
Deserializing config JSON..
{
  "LED Pin": 2,
  "A long var": 1234567890,
  "A String var": "Test option String",
  "A bool var": true
}
Application option loaded

best regards Stefan

HI @StefanL38
You are right, the example doesn't work properly because application config file need to be parsed before start webserver, my mistake.

You need simply to edit in this way the setup sequence:

void setup(){
  Serial.begin(115200);

  // FILESYSTEM INIT
  startFilesystem();

  // Try to connect to flash stored SSID, start AP if fails after timeout
  IPAddress myIP = myWebServer.startWiFi(15000, "ESP8266_AP", "123456789" );

  // 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(myIP);
     Serial.println(F("/setup to configure parameters"));
  }

  // 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);
  
  if (myWebServer.begin()) {
    Serial.print(F("ESP Web Server started on IP Address: "));
    Serial.println(myIP);
    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"));
  }  
}

Hi @cotestatnt ,

thank you very much for answering quick and for correcting the setup-function.
Now the example works.

I did test the example-code with option erase flash => "All Flash Contents"
and it works as expected
To have it really conveniant to copy the code here is the complete corrected sketch as a code-section.

I have added more serial output to make it easier to track down bugs
and the complete urls in its own line to make it easier to copy & paste

#include <esp-fs-webserver.h>   // https://github.com/cotestatnt/esp-fs-webserver

#include <FS.h>
#include <LittleFS.h>
#define FILESYSTEM LittleFS

#ifndef LED_BUILTIN
#define LED_BUILTIN 2
#endif

// Test "options" values
bool boolVar = true;
uint32_t longVar = 1234567890;
String stringVar = "Test option String";
uint8_t ledPin = LED_BUILTIN;

// Timezone definition to get properly time from NTP server
#define MYTZ "CET-1CEST,M3.5.0,M10.5.0/3"
struct tm Time;

#ifdef ESP8266
  ESP8266WebServer server(80);
#elif defined(ESP32)
  WebServer server(80);
#endif

FSWebServer myWebServer(FILESYSTEM, server);


////////////////////////////////  Filesystem  /////////////////////////////////////////
void startFilesystem(){
  Serial.println( F("(try to start FileSystem") );
  // FILESYSTEM INIT
  if ( FILESYSTEM.begin()){
    Serial.println( F("FILESYSTEM.begin() done") );
    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( F("ERROR on mounting filesystem. It will be formmatted!") );
    FILESYSTEM.format();
    Serial.println( F("formatting done ESP.restart()") );
    ESP.restart();
  }
}


////////////////////  Load application options from filesystem  ////////////////////
bool loadApplicationConfig() {
  StaticJsonDocument<1024> doc;
  File file = FILESYSTEM.open("/config.json", "r");
  if (file) {
    Serial.println( F("successfully opened file config.json for read") );
    DeserializationError error = deserializeJson(doc, file);
    file.close();
    if (!error) {
      Serial.println(F("Deserializing config JSON.. successful"));
      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);
  Serial.println( F("Setup-Start") );
  // FILESYSTEM INIT
  startFilesystem();

  // Try to connect to flash stored SSID, start AP if fails after timeout
  IPAddress myIP = myWebServer.startWiFi(15000, "ESP8266_AP", "123456789" );

  // 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(myIP);
     Serial.println( F("/setup to configure parameters") );
  }

  // 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);
  
  if (myWebServer.begin()) {
    Serial.print( F("ESP Web Server started on IP Address: ") );
    Serial.println(myIP);
    Serial.println();
    
    Serial.println( F("Open ") );
    Serial.print( F("http://") );
    Serial.print(myIP);
    Serial.println( F("/setup") );
    Serial.println( F("to configure optional parameters") );
    Serial.println();

    Serial.println( F("Open ") );
    Serial.print( F("http://") );
    Serial.print(myIP);
    Serial.println( F("/edit") );
    Serial.println( F("to view and edit files") );
    Serial.println();

    Serial.println( F("Open ") );
    Serial.print( F("http://") );
    Serial.print(myIP);
    Serial.println( F("/update") );
    Serial.println( F("to upload firmware and filesystem updates"));
    Serial.println();
    Serial.println( F("start looping myWebServer.run()") );
  }  
}

void loop() {
  myWebServer.run();
}

best regards Stefan

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.