SD dayfile and windows file date

Hi, For my wheater station I will log weather data. This is no problem so far. Only one problem is annoing namely that on each file (CSV)I will have a header on the first line for data names. This is only possible in the first data file I make.??? All other files do not have the header. How can I abort the file and make a new file midnight ( on clock time) with a header in each file?

Then the time stamp in windows showed Always a date somewhere in 2000 not the date the file is made? also strange.

Help is appreciated. ilioss

You can extract this to use the date as a filename

#include <SD.h>
#include "RTClib.h"
#include <Wire.h>
#include <string.h>

RTC_DS1307 RTC;
char filename[] = "00000000.CSV";
File myFile;

void setup()
{
Serial.begin(9600);
Wire.begin(); //Important for RTClib.h
RTC.begin();

if (! RTC.isrunning()) {

Serial.println("RTC is NOT running!");
return;
}

Serial.print("Initializing SD card...");

pinMode(10, OUTPUT);

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

void loop()
{

getFileName();
createFileName();
delay(3000);
}

void getFileName(){

DateTime now = RTC.now();

filename[0] = (now.year()/1000)%10 + '0'; //To get 1st digit from year()
filename[1] = (now.year()/100)%10 + '0'; //To get 2nd digit from year()
filename[2] = (now.year()/10)%10 + '0'; //To get 3rd digit from year()
filename[3] = now.year()%10 + '0'; //To get 4th digit from year()
filename[4] = now.month()/10 + '0'; //To get 1st digit from month()
filename[5] = now.month()%10 + '0'; //To get 2nd digit from month()
filename[6] = now.day()/10 + '0'; //To get 1st digit from day()
filename[7] = now.day()%10 + '0'; //To get 2nd digit from day()
}

void createFileName(){
myFile = SD.open(filename, FILE_WRITE);
myFile.close();
}

This is the start of my loop. It checks the time and changes the filename at midnight

void loop() {
  running();
  GetClock();
  if (hour == 0 && minute == 0 && second <2)
  {
    getFileName();
  }

Hi Nick_Pyner,

Thanks for your code for filename. However the file name is no problem. The problem is the first line in the .csv file. While I have a data analize program which will see the first line with a header.This is tekst. related to the data in the file like temp,humidity,dewpoint

Thanks any way.

ilioSS

Aha, misunderstood you! I think you should just get rid of the first line. If you are using a programme to analyse the data, the headers are redundant. All the programme should need is the order of the data. Headers are for humans.

The same programme should be able to stick the headers on and issue a human readable version if required.

Only one problem is annoing namely that on each file (CSV)I will have a header on the first line for data names. This is only possible in the first data file I make.??? All other files do not have the header. How can I abort the file and make a new file midnight ( on clock time) with a header in each file?

Posting your code using the invisible font is hardly a good idea. Of course, not posting your code is not a good idea, either.

Creating a new file and writing a record to the file is easy. It makes no difference if the first line is the header information or data. That you can't get a header in the second file means that you are doing something wrong in your code.

The Arduino doesn't have an operating system to time stamp files on the SD card.. If the time stamp is important, it must be in the name of the file.

Hi all,

The problem with the daily file with header is solved. Finnaly I tested the change on the day make present day the oldday and test for day change. If changed life to be closed and new file open.

Thanks for the reply.

The windows file date stamp is no problem.

Greetz, ilioSS

ilioSS:
Hi all,

The problem with the daily file with header is solved.
Finnaly I tested the change on the day make present day the oldday and test for day change.
If changed life to be closed and new file open.

Thanks for the reply.

The windows file date stamp is no problem.

Greetz,
ilioSS

Here is some logic from my application. I write a heading in each new log file by testing for the existence of a file before I open it.

  int l_dd, l_mm, l_yyyy = 0;
  long l_date = Date();
  DateDecode(l_date, &l_dd, &l_mm, &l_yyyy);
  String l_SD_filename = String(EPSR(E_HIST_2274) + ZeroFill(l_yyyy,4) + ".TXT");

  SPIDeviceSelect(DC_SDCardSSPin);
  boolean l_heading = false;
  if (!SD.exists(&l_SD_filename[0]))
    l_heading = true;
  //
  File l_SD_file = SD.open(&l_SD_filename[0], FILE_WRITE);
  if (! l_SD_file) {
    return;
  }

  //Write a heading line when needed
  if (l_heading)
    l_SD_file.println(F("DATE\tSENSOR\tMAXTEMP\tMINTEMP\tMAXHUM\tMINHUM"));
  //
  
  //Write the detail lines for each sensor
  //Backup yeserday's daily maximum and minimum climate stats
  //These are in index 1 - since we have already done a daily rollover
  //and are using index 0 for "today's" climate stats.
  //We are just a second (or less) after midnight.
  for (byte l_sensor = 0; l_sensor < DC_TempSensorCount; l_sensor++) {
    l_SD_file.print(DateToString(l_date));
    l_SD_file.print('\t');
    l_SD_file.print(C_SensorList[l_sensor]);
    l_SD_file.print('\t');
    l_SD_file.print(G_TempHumArray[l_sensor].TempMax[1] - DC_Temp_UpShift);
    l_SD_file.print('\t');
    l_SD_file.print(G_TempHumArray[l_sensor].TempMin[1] - DC_Temp_UpShift);
    l_SD_file.print('\t');
    l_SD_file.print(G_TempHumArray[l_sensor].HumMax[1]);
    l_SD_file.print('\t');
    l_SD_file.println(G_TempHumArray[l_sensor].HumMin[1]);
  } //for each sensor
  l_SD_file.close();

Cheers

Catweazle NZ