[GH-ISSUE #1437] Usage of WiFiManager with flashing text file to LittleFS #1230

Closed
opened 2026-02-28 01:29:08 +03:00 by kerem · 4 comments
Owner

Originally created by @bbrr132 on GitHub (Jun 21, 2022).
Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/1437

Basic Infos

Hardware

WiFimanager Branch/Release: Master

Esp8266/Esp32:

Hardware: ESP8266

Core Version: 2.4.0, staging

Description

Hello, I am trying to use the WiFi manager with my ESP01 while using littlefs. My previous sketches uses littlefs to load a text file I flashed to the esp01 using ESP8266 sketch data upload. However, when uploading the wifimanager program, my littlefs data is lost and flashing the data after upload overwrites the wifimanager.

Settings in IDE

Module: ESP01

Additional libraries:
LittleFS, FS

Sketch

#BEGIN
#include <Arduino.h>
#include <LittleFS.h>
#include <FS.h>

String readFile(fs::FS &fs, const char * path){
  Serial.printf("Reading file: %s\r\n", path);
  File file = fs.open(path, "r");
  if(!file || file.isDirectory()){
    Serial.println("- empty file or failed to open file");
    return String();
  }
  Serial.println("- read from file:");
  String fileContent;
  while(file.available()){
    fileContent+=String((char)file.read());
  }
  file.close();
  Serial.println(fileContent);
  return fileContent;
}
void writeFile(fs::FS &fs, const char * path, const char * message){
  Serial.printf("Writing file: %s\r\n", path);
  File file = fs.open(path, "w");
  if(!file){
    Serial.println("- failed to open file for writing");
    return;
  }
  if(file.print(message)){
    Serial.println("- file written");
  } else {
    Serial.println("- write failed");
  }
  file.close();
}

int data = 4; 

#include <WiFiManager.h>
#define TRIGGER_PIN 2
int timeout = 120; // seconds to run for

void setup() {
data = readFile(LittleFS, "/data.txt").toInt();
WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP  
  // put your setup code here, to run once:
  pinMode(TRIGGER_PIN, INPUT_PULLUP);
  WiFiManager wm;
  //wm.resetSettings();
  bool res;
  res = wm.autoConnect("Setup");
  if(!res) {
     Serial.println("Failed to connect");
     // ESP.restart();
  } 

}

void loop() {
if ( digitalRead(TRIGGER_PIN) == LOW) {
    WiFiManager wm;    
    //wm.resetSettings();
    wm.setConfigPortalTimeout(timeout);
    if (!wm.startConfigPortal("Sharmander")) {
      Serial.println("failed to connect and hit timeout");
      delay(3000);
      ESP.restart();
      delay(5000);
    }
    Serial.println("connected...yeey :)");
}
}
#END

Debug Messages

messages here
Originally created by @bbrr132 on GitHub (Jun 21, 2022). Original GitHub issue: https://github.com/tzapu/WiFiManager/issues/1437 ### Basic Infos #### Hardware WiFimanager Branch/Release: Master Esp8266/Esp32: Hardware: ESP8266 Core Version: 2.4.0, staging ### Description Hello, I am trying to use the WiFi manager with my ESP01 while using littlefs. My previous sketches uses littlefs to load a text file I flashed to the esp01 using ESP8266 sketch data upload. However, when uploading the wifimanager program, my littlefs data is lost and flashing the data after upload overwrites the wifimanager. ### Settings in IDE Module: ESP01 Additional libraries: LittleFS, FS ### Sketch ```cpp #BEGIN #include <Arduino.h> #include <LittleFS.h> #include <FS.h> String readFile(fs::FS &fs, const char * path){ Serial.printf("Reading file: %s\r\n", path); File file = fs.open(path, "r"); if(!file || file.isDirectory()){ Serial.println("- empty file or failed to open file"); return String(); } Serial.println("- read from file:"); String fileContent; while(file.available()){ fileContent+=String((char)file.read()); } file.close(); Serial.println(fileContent); return fileContent; } void writeFile(fs::FS &fs, const char * path, const char * message){ Serial.printf("Writing file: %s\r\n", path); File file = fs.open(path, "w"); if(!file){ Serial.println("- failed to open file for writing"); return; } if(file.print(message)){ Serial.println("- file written"); } else { Serial.println("- write failed"); } file.close(); } int data = 4; #include <WiFiManager.h> #define TRIGGER_PIN 2 int timeout = 120; // seconds to run for void setup() { data = readFile(LittleFS, "/data.txt").toInt(); WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP // put your setup code here, to run once: pinMode(TRIGGER_PIN, INPUT_PULLUP); WiFiManager wm; //wm.resetSettings(); bool res; res = wm.autoConnect("Setup"); if(!res) { Serial.println("Failed to connect"); // ESP.restart(); } } void loop() { if ( digitalRead(TRIGGER_PIN) == LOW) { WiFiManager wm; //wm.resetSettings(); wm.setConfigPortalTimeout(timeout); if (!wm.startConfigPortal("Sharmander")) { Serial.println("failed to connect and hit timeout"); delay(3000); ESP.restart(); delay(5000); } Serial.println("connected...yeey :)"); } } #END ``` ### Debug Messages ``` messages here ```
kerem 2026-02-28 01:29:08 +03:00
Author
Owner

@bbrr132 commented on GitHub (Jun 21, 2022):

Missed out
if (!LittleFS.begin()) {
Serial.println("LittleFS mount failed");
return;
}
resulting in FS not starting

<!-- gh-comment-id:1161326017 --> @bbrr132 commented on GitHub (Jun 21, 2022): Missed out if (!LittleFS.begin()) { Serial.println("LittleFS mount failed"); return; } resulting in FS not starting
Author
Owner

@tablatronix commented on GitHub (Jun 21, 2022):

Can you post or submit a PR for a littlefs examples?

<!-- gh-comment-id:1161697305 --> @tablatronix commented on GitHub (Jun 21, 2022): Can you post or submit a PR for a littlefs examples?
Author
Owner

@bbrr132 commented on GitHub (Jun 22, 2022):

Changed made to fix the issue: added LittleFS.begin() in setup to start LittleFS

#BEGIN
#include <Arduino.h>
#include <LittleFS.h>
#include <FS.h>

String readFile(fs::FS &fs, const char * path){
  Serial.printf("Reading file: %s\r\n", path);
  File file = fs.open(path, "r");
  if(!file || file.isDirectory()){
    Serial.println("- empty file or failed to open file");
    return String();
  }
  Serial.println("- read from file:");
  String fileContent;
  while(file.available()){
    fileContent+=String((char)file.read());
  }
  file.close();
  Serial.println(fileContent);
  return fileContent;
}
void writeFile(fs::FS &fs, const char * path, const char * message){
  Serial.printf("Writing file: %s\r\n", path);
  File file = fs.open(path, "w");
  if(!file){
    Serial.println("- failed to open file for writing");
    return;
  }
  if(file.print(message)){
    Serial.println("- file written");
  } else {
    Serial.println("- write failed");
  }
  file.close();
}

int data = 4; 

#include <WiFiManager.h>
#define TRIGGER_PIN 2
int timeout = 120; // seconds to run for

void setup() {
if (!LittleFS.begin()) { //to start littlefs
Serial.println("LittleFS mount failed");
return;
}
data = readFile(LittleFS, "/data.txt").toInt();
WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP  
  // put your setup code here, to run once:
  pinMode(TRIGGER_PIN, INPUT_PULLUP);
  WiFiManager wm;
  //wm.resetSettings();
  bool res;
  res = wm.autoConnect("Setup");
  if(!res) {
     Serial.println("Failed to connect");
     // ESP.restart();
  } 

}

void loop() {
if ( digitalRead(TRIGGER_PIN) == LOW) {
    WiFiManager wm;    
    //wm.resetSettings();
    wm.setConfigPortalTimeout(timeout);
    if (!wm.startConfigPortal("Sharmander")) {
      Serial.println("failed to connect and hit timeout");
      delay(3000);
      ESP.restart();
      delay(5000);
    }
    Serial.println("connected...yeey :)");
}
}
#END
<!-- gh-comment-id:1162615399 --> @bbrr132 commented on GitHub (Jun 22, 2022): ### Changed made to fix the issue: added LittleFS.begin() in setup to start LittleFS ```cpp #BEGIN #include <Arduino.h> #include <LittleFS.h> #include <FS.h> String readFile(fs::FS &fs, const char * path){ Serial.printf("Reading file: %s\r\n", path); File file = fs.open(path, "r"); if(!file || file.isDirectory()){ Serial.println("- empty file or failed to open file"); return String(); } Serial.println("- read from file:"); String fileContent; while(file.available()){ fileContent+=String((char)file.read()); } file.close(); Serial.println(fileContent); return fileContent; } void writeFile(fs::FS &fs, const char * path, const char * message){ Serial.printf("Writing file: %s\r\n", path); File file = fs.open(path, "w"); if(!file){ Serial.println("- failed to open file for writing"); return; } if(file.print(message)){ Serial.println("- file written"); } else { Serial.println("- write failed"); } file.close(); } int data = 4; #include <WiFiManager.h> #define TRIGGER_PIN 2 int timeout = 120; // seconds to run for void setup() { if (!LittleFS.begin()) { //to start littlefs Serial.println("LittleFS mount failed"); return; } data = readFile(LittleFS, "/data.txt").toInt(); WiFi.mode(WIFI_STA); // explicitly set mode, esp defaults to STA+AP // put your setup code here, to run once: pinMode(TRIGGER_PIN, INPUT_PULLUP); WiFiManager wm; //wm.resetSettings(); bool res; res = wm.autoConnect("Setup"); if(!res) { Serial.println("Failed to connect"); // ESP.restart(); } } void loop() { if ( digitalRead(TRIGGER_PIN) == LOW) { WiFiManager wm; //wm.resetSettings(); wm.setConfigPortalTimeout(timeout); if (!wm.startConfigPortal("Sharmander")) { Serial.println("failed to connect and hit timeout"); delay(3000); ESP.restart(); delay(5000); } Serial.println("connected...yeey :)"); } } #END ```
Author
Owner

@tablatronix commented on GitHub (Aug 5, 2022):

github.com/tzapu/WiFiManager@9e408e41a8

<!-- gh-comment-id:1206562078 --> @tablatronix commented on GitHub (Aug 5, 2022): https://github.com/tzapu/WiFiManager/commit/9e408e41a8000a7b87d8651a3e7e148c30421d5f
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/WiFiManager#1230
No description provided.