The reason for returning xt was I had set a shorter array for the path, where it couldn't fit in the file name. expanding it has solved the problem.
Now the file is getting created, but the appendFile is unable to run.
Seems I am making a mistake here
appendFile(SD, "path", "dataMessage");
here is the complete code below.
#include "FS.h"
#include "SD.h"
#include "SPI.h"
#include <time.h>
#include <WiFi.h>
int second=0;
int date = 0;
struct tm timeinfo;
char buff[50];
char filename[40];
const char* ssid = "SSID";
const char* password = "xxxx";
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 19800;
const int daylightOffset_sec = 0;
String dataMessage;
String date;
char path[40];
//=========================== WRITE FUNCTION ============================//
// Write to the SD card (DON'T MODIFY THIS FUNCTION)
void writeFile(fs::FS &fs, const char * path, const char * message) {
Serial.printf("Writing file: %s\n", path);
File file = fs.open(path, FILE_WRITE);
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();
}
//=========================== APPEND FUNCTION ============================//
// Append data to the SD card (DON'T MODIFY THIS FUNCTION)
void appendFile(fs::FS &fs, const char * path, const char * message) {
Serial.printf("Appending to file: %s\n", path);
File file = fs.open(path, FILE_APPEND);
if(!file) {
Serial.println("Failed to open file for appending");
return;
}
if(file.print(message)) {
Serial.println("Message appended");
} else {
Serial.println("Append failed");
}
file.close();
}
//============================= LOGGING FUNCTION ================================//
void logSDCard() {
dataMessage = (buff);
Serial.print("Path: ");
Serial.println(path);
Serial.print("Save data: ");
Serial.println(dataMessage);
// appendFile(SD, path.c_str(), dataMessage.c_str());
appendFile(SD, "path", "dataMessage");
}
//================================ SETUP ================================//
void setup() {
pinMode(5, INPUT_PULLUP);
pinMode(18, INPUT_PULLUP);
pinMode(19, INPUT_PULLUP);
pinMode(23, INPUT_PULLUP);
Serial.begin(115200);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.println("Contacting Time Server");
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
Serial.print("Initializing SD card...");
// Initialize SD card
SD.begin(5);
if(!SD.begin(5)) {
Serial.println("Card Mount Failed");
return;
}
uint8_t cardType = SD.cardType();
if(cardType == CARD_NONE) {
Serial.println("No SD card attached");
return;
}
Serial.println("Initializing SD card...");
if (!SD.begin(5)) {
Serial.println("ERROR - SD card initialization failed!");
return; // init failed
}
} //End of SETUP
//================================ LOOP ================================//
void loop() {
if(!getLocalTime(&timeinfo)){
Serial.println("Failed to obtain time");
return;
}
sprintf(buff, PSTR("%d-%02d-%02d,%02d:%02d:%02d \n"),timeinfo.tm_year+1900, timeinfo.tm_mon+1, timeinfo.tm_mday, timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec );
if(date != timeinfo.tm_mday){
sprintf(filename, PSTR("%d-%02d-%02d.txt"), timeinfo.tm_year+1900, timeinfo.tm_mon+1, timeinfo.tm_mday);
sprintf(path, PSTR("/%s"), filename );
date=timeinfo.tm_mday;
}
if(second != timeinfo.tm_sec){
logSDCard();
second=timeinfo.tm_sec;
}
} // End of LOOP
The return in the terminal
Appending to file: path
Failed to open file for appending
Path: /2021-06-16.txt
Save data: 2021-06-16,00:50:18