LittleFS mount failed Error

in below code, it update gps location to firebase RTDB and alertStatus. and recorded audio is uploading to firebase storage.

im using
ESP32 devkitV1 board
NEO6M MODULE
INMP441 MIC
BUTTON

I have connected neo 6m, INMP441 and the button to esp32 board. I have seperately checked each one and those are working fine. then i merge all code together

code is working fine there is no errors in this code. but when i press button to start recording and after finish recording, upload to firebase,, while it recording it say no more free storage.

so what i did is format SPIFFS and LITTLEFS and again upload my full code. then it showing failed to mount LIttleFS .. so how i can fix this issue. im not using SD card for keep audio files. note- im using .raw type to store audio data. then my android app, im convert it to .wav and play in my app. and after file upload to firebase, i clear the SPIFFS also.

how i can fix this issue

Connecting to Wi-Fi...

Connected to Wi-Fi!

E (406) esp_littlefs: ./managed_components/joltwallet__littlefs/src/littlefs/lfs.c:1369:error: Corrupted dir pair at {0x0, 0x1}

E (410) esp_littlefs: mount failed, (-84)

E (414) esp_littlefs: Failed to initialize LittleFS

LittleFS initialization failed!
#include <driver/i2s.h>
#include <WiFi.h>
#include <Firebase_ESP_Client.h>
#include <WiFiClientSecure.h>
#include <LittleFS.h>  // Include LittleFS instead of SPIFFS
#include <Preferences.h>  // Include Preferences library
#include <Arduino.h>
#include <TinyGPS++.h>

// Firebase configuration
#include "addons/TokenHelper.h"
#include "addons/RTDBHelper.h"

// I2S configuration for recording
#define I2S_WS 25
#define I2S_SD 33
#define I2S_SCK 32
#define I2S_PORT I2S_NUM_0
#define I2S_SAMPLE_RATE (16000)
#define I2S_SAMPLE_BITS (16)
#define I2S_READ_LEN (16 * 1024)
#define RECORD_TIME (20)  // Seconds
#define I2S_CHANNEL_NUM (1)
#define FLASH_RECORD_SIZE (I2S_CHANNEL_NUM * I2S_SAMPLE_RATE * I2S_SAMPLE_BITS / 8 * RECORD_TIME)


// Wi-Fi and Firebase configurations
#define WIFI_SSID "Chamika"
#define WIFI_PASSWORD "24682468"
#define API_KEY "*******"
#define USER_EMAIL "*******"
#define USER_PASSWORD "*******"
#define STORAGE_BUCKET_ID "*******"
#define DATABASE_URL "*******"

// Define the push button pin
#define BUTTON_PIN 23

File file;
char filename[50];  // To store dynamically generated filename

// Create object for HardwareSerial and GPS
HardwareSerial GPS_Serial(2); // (Rx, Tx)
TinyGPSPlus gps;

// Firebase objects
FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;

Preferences preferences;  // Create Preferences object

// File counter
int fileCounter;  // Declare fileCounter
// Task flags
bool recordingComplete = false;
bool taskCompleted = false;

unsigned long sendDataPrevMillis = 0;
bool signupOK = false;
int buttonState = 0;

// Function prototypes
void LittleFSInit();
void i2sInit();
void i2s_adc(void* arg);
void uploadToFirebase();
bool fileExists(const char* path);
void generateNewFileName();
void clearLittleFS();

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

  GPS_Serial.begin(9600, SERIAL_8N1, 16, 17);  // Use pins 16 and 17 for RX and TX
  // Set up the push button pin
  pinMode(BUTTON_PIN, INPUT_PULLUP);

  preferences.begin("counter", false);  // Open preferences with the namespace "counter" 
  // Retrieve the counter from preferences (default to 1 if not set)
  fileCounter = preferences.getInt("fileCounter", 1);
  Serial.printf("Starting with fileCounter: %d\n", fileCounter);

  // Initialize Wi-Fi connection
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  while (WiFi.status() != WL_CONNECTED) {
    delay(300);
    Serial.println("Connecting to Wi-Fi...");
  }
  Serial.println("Connected to Wi-Fi!");

  // Initialize LittleFS
  LittleFSInit();

  // Initialize Firebase
  config.api_key = API_KEY;
  config.database_url = DATABASE_URL;

  // Sign up for Firebase
  if (Firebase.signUp(&config, &auth, "", "")) {
    Serial.println("Firebase signup successful");
    signupOK = true;
  } else {
    Serial.printf("Firebase signup failed: %s\n", config.signer.signupError.message.c_str());
  }

  // Token status callback function
  config.token_status_callback = tokenStatusCallback;

  Firebase.begin(&config, &auth);
  Firebase.reconnectWiFi(true);
}

void loop() {
  smartDelay(1000);  // Handle GPS communication

  if (Firebase.ready() && signupOK && (millis() - sendDataPrevMillis > 5000 || sendDataPrevMillis == 0)) {
    sendDataPrevMillis = millis();

    if (gps.location.isValid()) {
      double latitude = gps.location.lat();
      double longitude = gps.location.lng();
      double altitude = gps.altitude.meters();

      // Send latitude to Firebase
      if (Firebase.RTDB.setFloat(&fbdo, "/gps/latitude", latitude)) {
        
      } else {
        Serial.println("Failed to update latitude.");
        Serial.println(fbdo.errorReason());
      }

      // Send longitude to Firebase
      if (Firebase.RTDB.setFloat(&fbdo, "/gps/longitude", longitude)) {
        
      } else {
        Serial.println("Failed to update longitude.");
        Serial.println(fbdo.errorReason());
      }

      // Send altitude to Firebase
      if (Firebase.RTDB.setFloat(&fbdo, "/gps/altitude", altitude)) {
        
      } else {
        Serial.println("Failed to update altitude.");
        Serial.println(fbdo.errorReason());
      }
    } else {
      Serial.println("Invalid GPS data");
    }
  }

  // Handle push button state and update Firebase
  if (Firebase.ready() && signupOK) {
    buttonState = digitalRead(BUTTON_PIN);

    if (buttonState == LOW) {  // Button pressed
      if (Firebase.RTDB.setInt(&fbdo, "/alertStatus", 1)) {
        Serial.println("Button pressed! Alert sent to Firebase.");
        // Initialize I2S and start recording task
        i2sInit();
        xTaskCreate(i2s_adc, "i2s_adc", 1024 * 4, NULL, 1, NULL);
        if (Firebase.ready() && recordingComplete && !taskCompleted) {
          taskCompleted = true;

          if (fileExists(filename)) {
            uploadToFirebase();
          } else {
            Serial.println("File not found, cannot upload.");
          }
        }
      } else {
        Serial.println("Failed to write to Firebase");
        Serial.println(fbdo.errorReason());
      }
    } else {  // Button not pressed
      if (Firebase.RTDB.setInt(&fbdo, "/alertStatus", 0)) {
        // Serial.println("No alert. Button not pressed.");
      } else {
        Serial.println("Failed to write to Firebase");
        Serial.println(fbdo.errorReason());
      }
    }

    // Small delay to avoid flooding Firebase
    delay(500);
  }
}

static void smartDelay(unsigned long ms) {
  unsigned long start = millis();
  do {
    while (GPS_Serial.available()) {
      gps.encode(GPS_Serial.read());
    }
  } while (millis() - start < ms);
}

void LittleFSInit() {
  if (!LittleFS.begin()) {  // Initialize LittleFS
    Serial.println("LittleFS initialization failed!");
    while (1) yield();
  }

  // Generate a new filename for the current recording
  generateNewFileName();

  file = LittleFS.open(filename, FILE_WRITE);
  if (!file) {
    Serial.println("File creation failed!");
    while (1) yield();
  }
  Serial.println("LittleFS initialized and file opened.");
}

void generateNewFileName() {
  int fileNumber = 1;

  // Check if the counter file exists in LittleFS
  if (LittleFS.exists("/fileCounter.txt")) {
    File counterFile = LittleFS.open("/fileCounter.txt", FILE_READ);
    fileNumber = counterFile.parseInt();
    counterFile.close();
  }

  // Create a new filename using the file number
  sprintf(filename, "/guardianRecord%d.raw", fileNumber);

  // Increment and save the new file number back to LittleFS
  File counterFile = LittleFS.open("/fileCounter.txt", FILE_WRITE);
  counterFile.printf("%d", fileNumber + 1);
  counterFile.close();

  Serial.printf("New file name generated: %s\n", filename);
}

void i2sInit() {
  i2s_config_t i2s_config = {
    .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX),
    .sample_rate = I2S_SAMPLE_RATE,
    .bits_per_sample = i2s_bits_per_sample_t(I2S_SAMPLE_BITS),
    .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
    .communication_format = i2s_comm_format_t(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB),
    .intr_alloc_flags = 0,
    .dma_buf_count = 16,
    .dma_buf_len = 1024,
    .use_apll = 1
  };
  i2s_driver_install(I2S_PORT, &i2s_config, 0, NULL);

  const i2s_pin_config_t pin_config = {
    .bck_io_num = I2S_SCK,
    .ws_io_num = I2S_WS,
    .data_out_num = -1,
    .data_in_num = I2S_SD
  };
  i2s_set_pin(I2S_PORT, &pin_config);
}

void i2s_adc(void* arg) {
  int i2s_read_len = I2S_READ_LEN;
  int flash_wr_size = 0;
  size_t bytes_read;
  char* i2s_read_buff = (char*)calloc(i2s_read_len, sizeof(char));

  while (flash_wr_size < FLASH_RECORD_SIZE) {
    i2s_read(I2S_PORT, (void*)i2s_read_buff, i2s_read_len, &bytes_read, portMAX_DELAY);
    file.write((const unsigned char*)i2s_read_buff, bytes_read);
    flash_wr_size += bytes_read;
    Serial.printf("Recording %d%%\n", flash_wr_size * 100 / FLASH_RECORD_SIZE);
  }
  file.close();
  free(i2s_read_buff);

  Serial.println("Recording complete");
  recordingComplete = true;

  i2s_driver_uninstall(I2S_PORT);
  vTaskDelete(NULL);
}

void uploadToFirebase() {
  String uploadPath = "/audio/guardianRecord" + String(fileCounter) + ".raw";
  Firebase.Storage.upload(&fbdo, STORAGE_BUCKET_ID, filename, mem_storage_type_flash, uploadPath.c_str(), "audio/raw");

  if (fbdo.httpCode() == FIREBASE_ERROR_HTTP_CODE_OK) {
    Serial.println("Upload successful!");

 // Increment file counter and save to preferences
    fileCounter++;
    preferences.putInt("fileCounter", fileCounter);

    // Clear LittleFS after successful upload
    clearLittleFS();
  } else {
    Serial.printf("Failed to upload file, reason: %s\n", fbdo.errorReason().c_str());
  }
}

bool fileExists(const char* path) {
  return LittleFS.exists(path);
}

void clearLittleFS() {
  LittleFS.remove(filename);
  Serial.println("LittleFS cleared.");
}

 

You need to provide the error log in code tags, and a wiring diagram, hand drawn is fine.

No sir, Wiring and all is okay. only thing is here im using old firebase library - firebase esp client library for gps code and the new firebase library - firebase client library for recorded audio upload to firebase storage.

i merge these two codes together. so later i tried to modify each one and use 1 library for both. but Im not good in this field very much sir. i tried Chatgpt also. but i dont know how to modify that full code. i want to use firebase client library for gps code also sir.

Have fun at the exhibit.

1 Like

Sorry, it's impossible to help under those circumstances, muting.

You came here asking for help. Helpers asked for more context (error messages, properly formatted; schematic, because we can better comprehend your project). You have refused. Helpers here are people who donate their time freely, not paid servants; accordingly, you shouldn't waste their time being difficult. Particularly as you were 'begging' for help!

I am the third helper now leaving the scene. I suggest you reconsider, provide the requested information, and try to work more constructively with whomever still wants to help.

Extremely sorry for the convenience sir.

since today morning we didnt have power. so i couldnt take any error log screenshots and all. Anyway i have fixed the issue i had sir.

i modified code and now im trying to compile and check. I will shurely give an update sir. Im really sorry sir.

i have provided all information below comment

Im extremely sorry. Im a beginner for this forum and all. after i start IOT subject in my campus 2 week ago,, today i created arduino account also. im again asking sorry from you all. i update my post. could you please check it !!

I have updated the post. can someone please help me.

You can try these options in the tools menu in the IDE if you have not already done so:

  1. Set the option to delete all flash memory on sketch upload (reset if before the next upload). This could clear the corruption you have reported.
  2. Choose a partition scheme which gives the right balance between storage space for your sketch and for spiffs/littleFS.

Post a screenshot of the IDE tools menu.
When you post code, it is best to remove credentials such as passwords, keys etc.

i choose huge app option previously. then when the 1st time run, it successfully start recording audio. but in the middle , it said no more free storage.

so i unplug the board and again connect it.. then it showed littlefs mound failed error.

since that, i still getting same error which is littlefs mount failed.

and i used littlefs code example. it successfully mount littlefs.. here is that serial monitor results

 - 1048576 bytes written in 9550 ms
- reading................................................................
- 1048576 bytes read in 275 ms
Deleting file: /test.txt
- file deleted
Test complete

You have to get back to a stable, repeatable situation, say using a much smaller audio file, maybe recording only a few seconds of sound before progressing any further otherwise you will never know where the problem lies. It seems that when it reports no more free storage it either corrupts the file system (maybe in a way that your individual littleFS upload sketch is insensitive to) or does not release storage occupied by the part created file.
If you erase the flash be enabling the option "Erase all flash before sketch upload" can you repeat the success you had originally ? You did not say if you tried this suggestion.
You are working with large amounts of data/program and only a relatively small flash memory (4MB). Show the complete compiler output so it is clear how much space you need for your sketch. Clearly if it is less than 1MB you can give 3MB to SPIFFS/LittleFS.

You can also try this to see if you can run the sketch again even after getting the "no more free storage" message :

  // Initialize LittleFS
  clearLittleFS() ; // add this line <<<<<<<<<<<<<<<<<<<<<<<
  LittleFSInit();

since more than 2 hours , im trying to compile this. but it like stuck in the middle


i have faced this issue several times. after compile is finished and code is uploaded, i will update

You seem to have a variety of ESP32 boards. The LilyGo T-Display and . . .

Which version of the Arduino ESP32 core have you installed ? 2 hours does sound long for a compilation.

image
image

actually in my esp32 board behind, it marked ESP32 devkit V1.
but when i connect it to my arduino, it automatically show this lili go display.

this one im using. also im using tinygps plus. sometimes this take that much of time. then when i again upload skecth, it doesnt take too much time and code will upload within 2-3 minute. this one using my cpu100%. i tried to use platformIO in my vs code. but it doesnt create a project. it said project is creating this take times and i waited almost 4-5 hours. so i removed it and again start to use arduino IDE.

The IDE does, under some circumstances, incorrectly identify the board (based on a non-unique USB VID/PID pair). However, you must change manually it to the correct board otherwise the code may behave incorrectly (default pin assignments, processor type, flash type etc. etc.)

image

then should i need to change this board type to correct one ? how can i find correct board type. i used gps and mic code sepearely with this board and all are worked fine. and the mic code worked as expected.

before buy this board, i search google and i got confuse bcz there are lot of esp32 boards. in my board front side it say espwroom32 and the back side it say esp32 devkitv1

when i connect it it show lilygo t display

my code complied successfully.. in the output, it show this. i selected 2MB APP/ 2MB SPIFFS partition scheme.

Sketch uses 1307405 bytes (62%) of program storage space. Maximum is 2097152 bytes.
Global variables use 48732 bytes (14%) of dynamic memory, leaving 278948 bytes for local variables. Maximum is 327680 bytes.
esptool.py v4.6
Serial port COM8
Connecting.........
Chip is ESP32-D0WD-V3 (revision v3.1)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
Crystal is 40MHz
MAC: 88:13:bf:62:1a:60
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 921600
Changed.
Configuring flash size...
Erasing flash (this may take a while)...
Chip erase completed successfully in 1.6s
Compressed 24896 bytes to 16262...
Writing at 0x00001000... (100 %)
Wrote 24896 bytes (16262 compressed) at 0x00001000 in 0.3 seconds (effective 590.6 kbit/s)...
Hash of data verified.
Compressed 3072 bytes to 134...
Writing at 0x00008000... (100 %)
Wrote 3072 bytes (134 compressed) at 0x00008000 in 0.0 seconds (effective 1820.4 kbit/s)...
Hash of data verified.
Compressed 8192 bytes to 47...
Writing at 0x0000e000... (100 %)
Wrote 8192 bytes (47 compressed) at 0x0000e000 in 0.0 seconds (effective 2704.3 kbit/s)...
Hash of data verified.
Compressed 1313984 bytes to 840903...
Writing at 0x00010000... (1 %)
Writing at 0x000161ab... (3 %)
Writing at 0x00022415... (5 %)
Writing at 0x0002c78f... (7 %)
Writing at 0x00031697... (9 %)
Writing at 0x0003886c... (11 %)
Writing at 0x0004bced... (13 %)
Writing at 0x00054b8f... (15 %)
Writing at 0x0005aa89... (17 %)
Writing at 0x00060fc1... (19 %)
Writing at 0x00066c24... (21 %)
Writing at 0x0006cbcd... (23 %)
Writing at 0x00072d00... (25 %)
Writing at 0x000789bb... (26 %)
Writing at 0x0007e821... (28 %)
Writing at 0x000845ac... (30 %)
Writing at 0x0008a882... (32 %)
Writing at 0x000900ba... (34 %)
Writing at 0x00095f18... (36 %)
Writing at 0x0009b8e9... (38 %)
Writing at 0x000a08d1... (40 %)
Writing at 0x000a5a8c... (42 %)
Writing at 0x000aae8b... (44 %)
Writing at 0x000b02a8... (46 %)
Writing at 0x000b5a08... (48 %)
Writing at 0x000bafc5... (50 %)
Writing at 0x000bff77... (51 %)
Writing at 0x000c5054... (53 %)
Writing at 0x000ca3a6... (55 %)
Writing at 0x000cf9e9... (57 %)
Writing at 0x000d4653... (59 %)
Writing at 0x000d9d17... (61 %)
Writing at 0x000df55a... (63 %)
Writing at 0x000e4d01... (65 %)
Writing at 0x000ea32b... (67 %)
Writing at 0x000ef4f7... (69 %)
Writing at 0x000f4ac6... (71 %)
Writing at 0x000f9d17... (73 %)
Writing at 0x000ff4f3... (75 %)
Writing at 0x00104a6c... (76 %)
Writing at 0x0010a194... (78 %)
Writing at 0x0010fffa... (80 %)
Writing at 0x0011583a... (82 %)
Writing at 0x0011bda0... (84 %)
Writing at 0x00124455... (86 %)
Writing at 0x0012cd85... (88 %)
Writing at 0x001323ce... (90 %)
Writing at 0x00137453... (92 %)
Writing at 0x0013eaa0... (94 %)
Writing at 0x00144358... (96 %)
Writing at 0x0014972b... (98 %)
Writing at 0x0014ee03... (100 %)
Wrote 1313984 bytes (840903 compressed) at 0x00010000 in 12.4 seconds (effective 847.2 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...

but it show again mount failed.. what should i do.. previously this worked. it updated the gps to firebase. when i pressed the button only it start record and displayed no more free storage.

Starting with fileCounter: 1
Connecting to Wi-Fi...
Connected to Wi-Fi!
LittleFS cleared.
E (421) esp_littlefs: ./managed_components/joltwallet__littlefs/src/littlefs/lfs.c:1369:error: Corrupted dir pair at {0x0, 0x1}

E (425) esp_littlefs: mount failed,  (-84)
E (429) esp_littlefs: Failed to initialize LittleFS
LittleFS initialization failed!

Have you attempted to change the file system type but perhaps without changing all the dependent routines ?