SD.open(filepath) Variable Filepath

I’m trying to write a sketch which logs humidity over time and records it to an SD card. It works fine with a fixed file name but I want it to write each record to a different file. I’ve used this to generate a new file name each time as a char[] string.

  dt = clock.getDateTime();
  sprintf(name, "%4d%02d%02d%02d%02d.csv", dt.year, dt.month, dt.day, dt.hour, dt.minute);
  Serial.println(name);

I then use
myFile = SD.open(name, FILE_WRITE);

to create the file.
When I run the sketch It doesn’t actually create the file and prints “error opening 202401121822.csv”

Is it a problem using a string to create a file name?

The full sketch is below.

#include <dht_nonblocking.h>
#define DHT_SENSOR_TYPE DHT_TYPE_11
#include <SD.h>
#include <Wire.h>
#include <DS3231.h>

DS3231 clock;
RTCDateTime dt;
static const int DHT_SENSOR_PIN = 2;
DHT_nonblocking dht_sensor(DHT_SENSOR_PIN, DHT_SENSOR_TYPE);
int humid;
char name[18];
File myFile;


/*
 * Initialize the serial port.
 */
void setup() {
  Serial.begin(9600);
  /*while (!Serial) {
    ;  // wait for serial port to connect. Needed for native USB port only
  */

  for (int x = 0; x < 10; x++) {
    Serial.println();
  }
  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while (1)
      ;
  }
  Serial.println("initialization done.");

  Serial.println("Initialize DS3231");
  // Initialize DS3231
  clock.begin();

  dt = clock.getDateTime();
  sprintf(name, "%4d%02d%02d%02d%02d.csv", dt.year, dt.month, dt.day, dt.hour, dt.minute);
  Serial.println(name);
  

  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);
}

/*
 * Poll for a measurement, keeping the state machine alive.  Returns
 * true if a measurement is available.
*/


static bool measure_environment(float *temperature, float *humidity) {
  static unsigned long measurement_timestamp = millis();


  if (millis() - measurement_timestamp > 5000ul) {
    if (dht_sensor.measure(temperature, humidity) == true) {
      measurement_timestamp = millis();
      return (true);
    }
  }

  return (false);
}



/*
 * Main program loop.
 */
void loop() {
  float temperature;
  float humidity;
  char data[10];

  /* Measure temperature and humidity.  If the functions returns
     true, then a measurement is available. */
  if (measure_environment(&temperature, &humidity) == true) {


    if (humidity != humid) {

      digitalWrite(LED_BUILTIN, HIGH);

      Serial.print(dt.hour);
      Serial.print(":");
      Serial.print(dt.minute);
      Serial.print(":");
      Serial.print(dt.second);
      Serial.print("\t");

      Serial.print("   ");
      Serial.print("T = ");
      Serial.print(temperature, 1);
      Serial.print(" deg. C, H = ");
      Serial.print(humidity, 1);
      Serial.println("%");

      myFile = SD.open(name, FILE_WRITE);

      if (myFile) {
        myFile.print(dt.year);
        myFile.print(",");
        myFile.print(dt.month);
        myFile.print(",");
        myFile.print(dt.day);
        myFile.print(",");
        myFile.print(",");
        myFile.print(dt.hour);
        myFile.print(",");
        myFile.print(dt.minute);
        myFile.print(",");
        myFile.print(dt.second);
        myFile.print(",");
        myFile.print(",");
        myFile.print(temperature);
        myFile.print(",");
        myFile.println(humidity);
        // close the file:
        myFile.close();



      } else {
        // if the file didn't open, print an error:
        Serial.print("error opening "); Serial.println(name);
      }
      digitalWrite(LED_BUILTIN, LOW);
      humid = humidity;
    }
  }
}

No, but that string is too long. At most eight characters are allowed before the '.csv'. The file name format is 8.3.

You're a star. I just took the year out of the file name and it works fine.

Thanks very much.

Just out of curiosity, why would you want to create hundreds (thousands?) of files, each containing a single line?

I don't want to create thousands of files. I just want all the files that i create to be indexed. If they are all sorted by name, they will be in date order.

Each file wont contain a single line, each file may contain hundreds.
Just a new file name (by date and time) for each entry.

Your code takes a reading every 5 seconds. You use seconds in the name of your file. You will only have 1 entry in the filename and the next time you sample, it will be in a different file.

The file name is generated in the setup. After that it just keeps writing to the same file name.