ESP-FS-WebServer

repository·master·Indexed 41 days ago

https://github.com/cotestatnt/esp-fs-webserver

A library for ESP8266 and ESP32 providing a comprehensive web server environment. Key features include a built-in file system browser and editor at /edit, a WiFi configuration manager with a captive portal at /setup, WebSocket support for real-time two-way communication, and OTA updates. It supports both FSWebServer and AsyncFsWebServer implementations, allowing for dynamic webpage generation, telemetry dashboards using ECharts 5, and secure credential management with AES-256-CBC encryption.

Tokens
17.8K
Snippets
68
Records
91
Agent score
52%

What's inside ESP-FS-WebServer

  1. Overview of the localRFID access control system

    master

    The localRFID example implements a simple access control system using RFID tags and the Arduino_MFRC522v2 library.

    Key Features:

    • User Roles: Users can be assigned different role levels.
    • Access Control: The system allows enabling or disabling users.
    • Permission Logic: Higher role levels (e.g., level 5 or admin) are required to modify or delete user accounts. Users with lower levels can log in but are restricted to viewing data only.
  2. Core Features of ESP-FS-WebServer

    master

    The library provides several integrated services for ESP8266/ESP32 devices:

    • Dynamic WiFi Configuration: An integrated /setup page for scanning networks and connecting to WiFi. Supports up to 5 different SSIDs. Passwords are encrypted using AES-256-CBC (hardware-accelerated on ESP32).
    • Advanced File Management: An embedded file manager at /edit for browsing, viewing, uploading, and deleting files and folders.
    • Over-the-Air (OTA) Updates: Secure firmware updates and the ability to upload an entire web project's data folder to the ESP filesystem.
    • WebSocket Support: Built-in real-time, two-way communication.
    • Customizable UI: Ability to add custom configuration parameters (text boxes, checkboxes, sliders, dropdowns) and inject custom HTML, CSS, and JavaScript into the setup page.
  3. How CredentialManager works with FSWebServer

    master

    The CredentialManager is integrated into the FSWebServer library to provide transparent, encrypted WiFi credential management.

    Workflow

    1. Addition: Users add WiFi credentials via the /setup web page provided by FSWebServer.
    2. Storage: Credentials are automatically encrypted using AES-256-CBC and saved to persistent storage (NVS on ESP32 or Filesystem on ESP8266).
    3. Startup: On boot, FSWebServer calls startWiFi(), which triggers an RSSI-based selection process.
    4. Connection: The system scans available networks, matches them against stored credentials, and selects the network with the highest signal strength (best RSSI) to connect to.

    Key Management Logic

    • FIFO (First In, First Out): When the maximum number of credentials is reached, the oldest credential is automatically removed to make room for the new one.
    • RSSI-Based Selection: The device prioritizes the strongest signal (e.g., -42 dBm is preferred over -65 dBm).
  4. How WiFi Management and Reconnection works

    master

    The library uses a WiFiService class to manage scanning, connections, captive portals, mDNS, and watchdog timers.

    Connection Logic:

    1. startWiFi() loads credentials from the CredentialManager.
    2. It selects the SSID with the strongest RSSI.
    3. If no credentials exist, it falls back to a captive portal.
    4. Once a connection is established, it advertises the device via mDNS (e.g., http://<hostname>.local).

    Reconnection Experience: When a user changes SSIDs via the /setup page, the browser enters a polling loop. It polls http://<hostname>.local/getStatus (falling back to the captive AP if necessary). The /getStatus endpoint provides:

    • Firmware version
    • Active mode
    • Hostname
    • IP address
    • Configuration file path

    This allows the UI to automatically detect when the device has reappeared on the new network and update the user without a manual refresh.

  5. Understand WiFi credential storage (CredentialManager)

    master

    WiFi credentials (SSID, password, DHCP/static IP settings) are handled by an internal CredentialManager and are stored separately from the application config.json for security.

    • Storage Location: Credentials are stored in an encrypted format. On ESP32, this uses NVS; on ESP8266, it uses the filesystem (e.g., LittleFS).
    • Usage: The /setup web interface communicates directly with the WiFi APIs (/wifi/credentials, /connect, etc.). You generally do not need to manually read or write WiFi data in your sketch.
  6. Quickstart: Basic Usage of ESP-FS-WebServer

    master

    To use ESP-FS-WebServer, you need to initialize a filesystem (like LittleFS), instantiate the FSWebServer class with a port, the filesystem reference, and a hostname, and then call startWiFi() and begin(). You must call server.run() in your loop() to handle incoming requests.

    Note: startWiFi(timeout) will attempt to connect to saved credentials or start a captive portal if no credentials are available.

    #include <FS.h>
    #include <LittleFS.h>
    #if defined(ESP8266)
    #include <ESP8266WiFi.h>
    #else
    #include <WiFi.h>
    #endif
    #include "FSWebServer.h"
    
    // Use LittleFS
    fs::FS& filesystem = LittleFS;
    
    FSWebServer server(80, filesystem, "esphost");
    
    void setup() {
      Serial.begin(115200);
    
      // Initialize Filesystem
      if (!filesystem.begin()) {
        Serial.println("Error mounting file system.");
        return;
      }
    
      // Start WiFi (or captive portal if no credentials)
      server.startWiFi(10000);
    
      // Add a handler for the root page
      server.on("/", []() {
        server.send(200, "text/html", "<h1>Hello from FSWebServer!</h1>");
      });
    
      // Start the server
      server.begin();
      Serial.println("HTTP server started.");
    }
    
    void loop() {
      server.run();
    }
  7. Configure charts using ECharts 5

    master

    The dashboard utilizes ECharts 5 for data visualization. It implements real-time line charts with a time axis and formatted tooltips.

    To customize the appearance, behavior, or data mapping of the charts, you must modify the chart configuration options located in the data/index.htm file within the example directory.

    For detailed configuration of chart types, axes, and series, refer to the official ECharts documentation.

  8. Enable the Web File Editor (/edit)

    master

    To enable the ACE-based web file editor, call server.enableFsCodeEditor(). Once enabled, the following endpoints become available for managing files via the browser:

    • GET /edit: Access the editor page
    • GET /list?dir=/: View directory listings
    • POST /edit: Upload files
    • PUT /edit: Create or rename files
    • DELETE /edit: Delete files
    server.enableFsCodeEditor();
  9. Customize web pages for the localRFID example

    master

    By default, the localRFID example uses web pages embedded in the firmware as compressed Gzip C arrays. To use custom web pages instead, follow these steps:

    1. Prepare files: Locate the login and rfid files (without extensions) in the data/ folder of the example.
    2. Upload to Flash: Use the built-in /setup webpage functionality to upload these files directly to the microcontroller's flash memory.
    3. Update Firmware Configuration: Modify the source code to disable embedded files by setting #define USE_EMBEDDED_HTM false.
    #define USE_EMBEDDED_HTM false