how can i create txt file from the date of RTC DS3231

hello all,

i want to create txt files like "01032018.txt" automatically from the date of RTC DS3231 and save using sd card module.
i try this program but it can't, in this program i use sht 11 sensor to monitor temp and humidity.

#include <SD.h>
#include <SPI.h>
#include <DS3231.h>
#include <SHT1x.h>
#define dataPin  6   //pin 10 arduino to pin 1 SHT11
#define clockPin 7   // pin 11 arduino to pin 3 clock SHT11
SHT1x sht1x(dataPin, clockPin);
DS3231 rtc(SDA,SCL);

int suhu; 
int kelembaban; 
File myFile;
int pinCS = 10; // Pin 10 on Arduino Uno

void setup() {
  rtc.begin();
  Serial.begin(9600);
  pinMode(pinCS, OUTPUT);
  
  // SD Card Initialization
  if (SD.begin())
  {
    Serial.println("SD card is ready to use.");
  } else
  {
    Serial.println("SD card initialization failed");
    return;
  }
  rtc.begin();    
}

void loop() {
suhu = sht1x.readTemperatureC(); 
kelembaban = sht1x.readHumidity(); 

Serial.print(rtc.getDOWStr());
Serial.print(",");

Serial.print(rtc.getDateStr());
Serial.print(",");

//Read time
Serial.print(rtc.getTimeStr());
Serial.print(",");
Serial.print(suhu);
Serial.print(",");
Serial.println(kelembaban);

String filename=String(rtc.getDateStr() + ".txt");
char str[16] = {0};
filename.toCharArray(str,16);
myFile = SD.open(str, FILE_WRITE);
  
if (myFile) {    
myFile.print(rtc.getDOWStr());
myFile.print(",");

myFile.print(rtc.getDateStr());
myFile.print(",");

//Read time
myFile.print(rtc.getTimeStr());
myFile.print(",");

myFile.print(suhu);
myFile.print(",");
myFile.println(kelembaban);

    myFile.close(); // close the file
  }
  // if the file didn't open, print an error:
  else {
    Serial.println("error opening test.txt");
  }
  delay(3000);
}

thank you for your help :slight_smile:

i try this program but it can't

It can't what? Can't tie your shoes?

What IS in str when that code executes?

omg sorry. the error is "invalid operands of types 'char*' and 'const char [5]' to binary 'operator+'"

anindyadwi:
omg sorry. the error is "invalid operands of types 'char*' and 'const char [5]' to binary 'operator+'"

That is only part of the error message. The rest includes the file name and the line number where that is happening.

String filename=String(rtc.getDateStr() + ".txt");

Apparently, rtc.getDateStr() returns a char * - a pointer to a char array. You are then trying to increment that pointer by ".txt", which makes no sense at all.

If you insist on pissing away memory using Strings,

String filename = rtc.getDateStr();
filename += ".txt";