Date and time outputs from a GPS module will not combine to form a filename in an Arduino Nano

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!

very useful hint:

  fileName += String(".kml");

:wink:

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());

 // Create filename using date and time

  char fileName[30];
  sprintf("GPSData_%04d%02d%02d_%02d%02d%02d.kml", gps.date.year(), gps.date.month(), gps.date.day(), gps.time.hour(), gps.time.minute(), gps.time.second());

  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>");
    dataFile.close();
  }
  else Serial.println("Error opening file!");
}
1 Like

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 :slight_smile: ). That worked and I see the filename as I want it. However, it cannot open the file for editing.

use 8+3 characters file name

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.

1 Like

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'?

You could always save it with an 8+3 file name and then rename it later.

If you save the filename that you have generated at the beginning of the file, then you will know what to rename it as

1 Like

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.

Maybe using an ESP32 is a good idea...

Do you actually need the files to have these exact names, or do you simply want each file to have a unique name?

You could use the least significant 8 digits of epoch time. That would not repeat for over 3 years.

1 Like

You could use the ASCII representation of the epoch time in hexidecimal, that would be exactly eight characters.

3 Likes

that will work nicely.

I'm using following:

  char val[30] = {'\0'};          // max filename 8.3 + 1 Nulllimiter
  uint32_t epoche = now();
  ultoa(epoche, val, 16);         // filename can only hold 8.3, hence we convert the timestamp to HEX
  strcat(val, ".SAF");
  File dataFile = SD.open(val, FILE_WRITE);

in my "Store and Forward" Example:
http://werner.rothschopf.net/microcontroller/202208_arduino_webclient_sd_saf_ntp.htm

So did some more digging and another learned user on this forum suggested using the SdFat library. And the long file name now works.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.