I'm trying to build a GPS datalogger which has the following function that is supposed to start recording coordinates into a file on the attached SD card. The SD card check passes and I can see valid GPS date and time too.
I'm trying to create a file on the SD card with the filename: "GPSData_currentdate_currenttime.kml". I was able to get just the filename with the code below but it does not append the KML extension. Also when I try to open the file for writing, I get an error (the else loop in the code below). Any suggestions on how to get this working?
I'm using PlatformIO on VSCode to build and upload the code to an Arduino Nano.
void startRecording()
{
Serial.println("Recording started.");
blinkLED(1); // Blink three times to indicate recording started
Serial.println(gps.date.day());
Serial.println(gps.date.month());
Serial.println(gps.date.year());
Serial.println(gps.time.hour());
Serial.println(gps.time.minute());
Serial.println(gps.time.second());
// Extract date and time from GPS data
// String gpsDate = String(gps.date.day()) + String(gps.date.month()) + String(gps.date.year());
// String gpsTime = String(gps.time.hour()) + String(gps.time.minute()) + String(gps.time.second());
// // Create filename using date and time
// String fileName = String("GPSData_") + gpsDate + String("_") + gpsTime + String(".kml");
String fileName;
fileName = String("GPSData_");
fileName += String(gps.date.day());
fileName += String(gps.date.month());
fileName += String(gps.date.year());
fileName += String("_");
fileName += String(gps.time.hour());
fileName += String(gps.time.minute());
fileName += String(gps.time.second());
fileName += String(".");
fileName += String("kml");
Serial.print("Filename: ");
Serial.println(fileName);
// Open the file
dataFile = SD.open(fileName, FILE_WRITE);
if (dataFile)
{
isRecording = true;
Serial.println("File opened: " + fileName);
// Write KML header
dataFile.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
dataFile.println("<kml xmlns=\"http://www.opengis.net/kml/2.2\">");
dataFile.println("<Document>");
}
else
{
Serial.println("Error opening file!");
}
}
I see the following output when the function is run.
SD card initialised.
Waiting for valid GPS signal...
Fix status: 1
Valid GPS signal established. Press the button to start recording.
Recording started.
28
12
2023
10
37
33
Filename: GPSData_28122023_103733.
Error opening file!
I tried that before the current form and that did not work. Tried the sprintf solution but with the name of the buffer included (you missed that in the code above ). That worked and I see the filename as I want it. However, it cannot open the file for editing.
This is the reason your code does not work. The SD library is limited to using the 8.3 format file names for the FAT file system. There can be a maximum of eight characters in the file name and three characters in the extension, and as far as I know the file system is not case-sensitive so there would be no distinction between upper and lower case letters.
Bingo! This is indeed why it wasn't working. Any other libraries you can recommend that will switch to a different filesystem and overcome this 'limit'?
I think on Nano you don't have a much options. Most of arduino boards uses the same SD library and, therefore, has the same filename limits.
To see different behaviour you can try a SD_MMC library, but it for ESP32 boards
This is a good idea but I don't want there to be some form of manual intervention. I will eventually automate exporting these files and importing them to Google Earth.