Trying to creat a .txt file inside the sd card with current hour as the name of the file

Im trying to creat a .txt file inside the sd card with current hour as the name of the file,
but it just dont work. the esp32 gets the current hour from a gnss receiver.
here is the part of the code a get the hour from:

void GetTime(){
   if (millis() - lastTime > 1000)
  {
    lastTime = millis(); //Update the timer

    long latitude = myGNSS.getLatitude();
    Serial.print(F("Lat: "));
    Serial.print(latitude);

    long longitude = myGNSS.getLongitude();
    Serial.print(F(" Long: "));
    Serial.print(longitude);
    Serial.print(F(" (degrees * 10^-7)"));

    long altitude = myGNSS.getAltitude();
    Serial.print(F(" Alt: "));
    Serial.print(altitude);
    Serial.print(F(" (mm)"));

    byte SIV = myGNSS.getSIV();
    Serial.print(F(" SIV: "));
    Serial.print(SIV);

    Serial.println();
    Serial.print(myGNSS.getYear());
    Serial.print("-");
    Serial.print(myGNSS.getMonth());
    Serial.print("-");
    Serial.print(myGNSS.getDay());
    Serial.print(" ");
    Serial.print(myGNSS.getHour());
    Serial.print(":");
    Serial.print(myGNSS.getMinute());
    Serial.print(":");
    Serial.print(myGNSS.getSecond());

    Serial.print("  Time is ");
    if (myGNSS.getTimeValid() == false)
    {
      Serial.print("not ");
    }
    Serial.print("valid  Date is ");
    if (myGNSS.getDateValid() == false)
    {
      Serial.print("not ");
    }
    Serial.print("valid");

    Serial.println();

    sprintf(Date, "%04d-%02d-%02d", myGNSS.getYear(), myGNSS.getMonth(), myGNSS.getDay());

    sprintf(hour, "%02d:%02d:%02d", myGNSS.getHour(), myGNSS.getMinute(), myGNSS.getSecond());

    sprintf(FileName, "%s/%s.ubx", Date, hour);

    Serial.print(FileName);
  }
}

here is where I creat the directory:

void createDirectory() {

  Serial.println("");
  Serial.println("Creating directory test");

  // if the folder already exists, don't bother creating it
  if (!sd.exists(Date)) {

    // attempt to access the root directory information
    if (myDir.open("/")) {

      // Create a new folder called 'testFolder'
      // mkdir will return true if it completes successfully
      if (sd.mkdir(Date)) {

        Serial.println(F("Created 'testFolder' successfully"));
      
      } else {

        Serial.println("Create 'testFolder' failed");
      }
      
    } else {
      digitalWrite(buzzer, HIGH);
      delay(300);
      digitalWrite(buzzer, LOW);
      delay(300);
      digitalWrite(buzzer, HIGH);
      delay(300);
      digitalWrite(buzzer, LOW);
      Serial.println("Open 'testFolder' failed");
    }  
    
  } else {

    Serial.println("Folder 'testFolder' already exists!");
  }
}

and here is where the .txt file should be created:

GetTime();
        createDirectory();

        myFile = sd.open(FileName, FILE_WRITE);

        Serial.println("PPP Base enabled!");

        Serial.println("Iniciating...");

        delay(2000);

Explain. Describe what should happen, and what happens instead.

Instead of posting snippets, write a small, complete program that demonstrates the error, and post that.

I believe that file name format defaults to 8.3 unless you have enabled long file names.

1 Like

I think you need a simple subroutine like

void getFileName(){
sprintf(filename, "%02d%02d%02d.csv", year, month, day);
}

Adjust the variables according to your needs

1 Like

You have a bunch of Serial Monitor output. What does that show?

1 Like

thanks everyone that tried to help. Turns out that my original code was "correct".
the problem was that I was trying to creat a .txt file with this name format: "12:33:12"
representing the exact time of the day that it was creat, and you cant use the Character ":" .
changing the line:

sprintf(hour, "%02d:%02d:%02d", myGNSS.getHour(), myGNSS.getMinute(), myGNSS.getSecond());

to:

sprintf(hour, "%02d;%02d;%02d", myGNSS.getHour(), myGNSS.getMinute(), myGNSS.getSecond());

this solved the problem.

basically I changed : to ;

lol, this took me about 1:30 hour to figure out.

again, thanks everyone that tryed to help.

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