Skip to main content
Code extension after feedback
Source Link
Codebreaker007
  • 1.3k
  • 1
  • 7
  • 14

Open the file in the loop or the subroutine, but not in setup as file is then a local var to setup

In your code it should be

file = SPIFFS.open("/testfile", "a"); // a ... append

a Open for appending (writing at end of file). The file is created if it does not exist. The stream is positioned at the end of the file.

r+ positions stream at the beginning of the file

and to get lines you can afterwards analyze you should use

if(file){
   file.println("Your MQTT payload")){;
    Serial.println("File was written");
   file.close();
} else {
    Serial.println("File write failed");
}
 
file.close();

and do not forget to close after the operation.
EDIT
As reaction to the comments here a "clean solution" as a function:

bool writeLog(const char fileName[],  char payloadMqtt[]) {
  File logFile = SPIFFS.open(fileName, "a");
  if (!logFile)  {
        Serial.println("logFile open failed");
    return false;
  }
  else {
    //Write data to file
    Serial.println("writing Data to logFile");
    logFile.print(payloadMqtt);
    logFile.close();
    return true;
  }
}

You call it with:

writeLog("/myfilepath", payload);

and can also be used like

if (writeLog("/myfilepath", payload)) Serial.println("Log save success");
else Serial.println("Log save failure");

Hope this helps to understand the nature of SPIFFS/LittleFS/SD operations

Open the file in the loop or the subroutine, but not in setup as file is then a local var to setup

In your code it should be

file = SPIFFS.open("/testfile", "a"); // a ... append

a Open for appending (writing at end of file). The file is created if it does not exist. The stream is positioned at the end of the file.

r+ positions stream at the beginning of the file

and to get lines you can afterwards analyze you should use

if(file.println("Your MQTT payload")){
    Serial.println("File was written");
} else {
    Serial.println("File write failed");
}
 
file.close();

and do not forget to close after the operation.

Open the file in the loop or the subroutine, but not in setup as file is then a local var to setup

In your code it should be

file = SPIFFS.open("/testfile", "a"); // a ... append

a Open for appending (writing at end of file). The file is created if it does not exist. The stream is positioned at the end of the file.

r+ positions stream at the beginning of the file

and to get lines you can afterwards analyze you should use

if(file){
   file.println("Your MQTT payload");
   Serial.println("File was written");
   file.close();
} else {
    Serial.println("File write failed");
}
 

and do not forget to close after the operation.
EDIT
As reaction to the comments here a "clean solution" as a function:

bool writeLog(const char fileName[],  char payloadMqtt[]) {
  File logFile = SPIFFS.open(fileName, "a");
  if (!logFile)  {
        Serial.println("logFile open failed");
    return false;
  }
  else {
    //Write data to file
    Serial.println("writing Data to logFile");
    logFile.print(payloadMqtt);
    logFile.close();
    return true;
  }
}

You call it with:

writeLog("/myfilepath", payload);

and can also be used like

if (writeLog("/myfilepath", payload)) Serial.println("Log save success");
else Serial.println("Log save failure");

Hope this helps to understand the nature of SPIFFS/LittleFS/SD operations

Source Link
Codebreaker007
  • 1.3k
  • 1
  • 7
  • 14

Open the file in the loop or the subroutine, but not in setup as file is then a local var to setup

In your code it should be

file = SPIFFS.open("/testfile", "a"); // a ... append

a Open for appending (writing at end of file). The file is created if it does not exist. The stream is positioned at the end of the file.

r+ positions stream at the beginning of the file

and to get lines you can afterwards analyze you should use

if(file.println("Your MQTT payload")){
    Serial.println("File was written");
} else {
    Serial.println("File write failed");
}
 
file.close();

and do not forget to close after the operation.