Go Down

Topic: Setting Arduino's date and time? (Read 1 time) previous topic - next topic

NickPyner

#5
Jan 02, 2013, 02:03 am Last Edit: Jan 02, 2013, 03:29 am by NickPyner Reason: 1

Whenever I create a file on the SD card, through the Arduino and the WiFi shield, the date and time of the file creation is set to the standard January 1, 2000 00:00. I have found how to connect to an NTP server and read the current date and time.  But how can I set the current date and time on the Arduino for it to use in it's other operations such as file I/O?


I think the simplest approach is to do your own stamping using a DS1307 clock module. You can use it to name the file and/or include date/time in the data. In my case, the clock is used to create a new file at midnight  - 20130102,csv - and thus only the time need be recorded therein.

in preamble:
Code: [Select]
File myFile;
char filename[] = "00000000.CSV";


in Setup
Code: [Select]
getFileName();


the condition in the loop:
Code: [Select]
 if (hour == 0 && minute == 0 && second <2)
 {
   getFileName();
 }


the subroutine:
Code: [Select]
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()

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



Maybe you could get the same result with your NTP server.

DerekErb

For most of my uses I'm not worried about the date/time surviving a board reset as each of my current and foreseen boards is either Ethernet or WiFi based. I will get the current time, via NTP, in setup() every time the board is reset.

The date and time of the file creation, and modification, are not primordial for me. I just wanted to know where and how to be able to control that for future operations and, most importantly, if it is possible. The date and time recordings I need will be in fields inside my files, mostly CSVs, and that's not a problem as I control that inside the files. But when transferring those files from the SD card to my computers I would prefer it is the last modification date and time were correct.

My next project, as soon as I get some more time, is to start working on moving my SD routines from the SD library to the SdFat library as I already need that to correctly return the amount of space available on the SD card. At that time I'll look in to how to possibly set both the file creation date/time and the file modification date/time ... if it's possible.

Thanks all.

juycce

hi there can i ask a question,the DS1307 RTC have battery backup so it doesnt lose the time...but what about hour change?(when clock turn fwd and bckwd) do i have to change the arduino values allways?or is there any way to do it buy it self..thankyou

DerekErb


hi there can i ask a question,the DS1307 RTC have battery backup so it doesnt lose the time...but what about hour change?(when clock turn fwd and bckwd) do i have to change the arduino values allways?or is there any way to do it buy it self..thankyou


For my network (NTP) version I always use a timer which reverifies and resets the time. Once every 24 hours, for example, it gets the time from the NTP server and sets the internal clock. The frequency of your timer reset would be based on how much, or little, overhead you want to add to your loop()

For a version which isn't using the network you would have to build-in DST calculations, with additional location-based and timezone-based modifications, in to your code...

PeterH


what about hour change?(when clock turn fwd and bckwd) do i have to change the arduino values allways?


DST is usually only relevant when the time value is rendered for display, and if you're going to support DST you need to provide a way to choose which timezone you're going to render the time for (perhaps hard-coded) and to apply the DST adjustments applicable to that TZ. This is not changing the value of the underlying timestamp (nor the clock that it came from) but just how the date/time value is displayed.
I only provide help via the forum - please do not contact me for private consultancy.

Go Up