Go Down

Topic: Unix time from DS3231 (Read 165 times) previous topic - next topic

MSFfield

Hi, i have read a lot of posts about "normal time" and Unix time as it would be much easier for me to do some calculations using the Unix time, especially because i dont need to convert it back into normal time at the end. But it seems that it is not so easy (well at least needs a bit of code) to get the Unix time out of the DS3231 (or am i missing something here) i would have thought that it is working in Unix time anyway and it would be easy for it just to give you that value anyway...?? If not then i will just do it the long way... :)

cattledog

Quote
i would have thought that it is working in Unix time anyway and it would be easy for it just to give you that value anyway...??
The most simple thing to do when you have thoughts like that is to look at the data sheet for the device. You would see that there is no internal storage register for Unix time.

Quote
But it seems that it is not so easy (well at least needs a bit of code) to get the Unix time out of the DS3231 (or am i missing something here)
Many Arduino libraries for the DS3231 have a function to return the Unix time stamp derived from the internal time storage register.  Are you using a library?

Jack Christensens excellent library DS3232.h which works with the DS3231 returns the Unix time stamp with

Code: [Select]
rtc.get();

RTClib.h returns Unix time with two lines
Code: [Select]
DateTime now = rtc.now();
uint32_t timestamp = now.unixtime());

MSFfield

Thanks for the data sheet tip, i have never thought about looking there. :)

Sorry for not saying which library i am using, should have thought about that, i am using the <DS3231.h> library is there the same fetcher for this?? and where could i read up myself to find that out (sorry probably a very stupid question)?  If it does not have can i just include Jack Christensens DS3232.h one and then just use the rtc.get() to get the time or is it bad practice to have two libraries for the same hardware?

johnwasser

It is generally bad practice to have two separate libraries controlling the same hardware.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

MSFfield

Thanks John, yes i had a feeling that it might be, do you know where i might find out what the unix time stamp is for the DS3231.h library, or if it has one? if not i will have to change to Jack Christensens DS3232.h one...

jremington

Use the Arduino Time library to create unix time stamps. It can synchronize with the DS3231 to keep time, as well.

cattledog

Quote
Sorry for not saying which library i am using, should have thought about that, i am using the <DS3231.h> library is there the same fetcher for this??
There are several libraries with the same name. DS3231.h  You will have to be more specific.

Looking at library source code is a good habit to develop. Most are available for review on GitHub.

If you have actually added the library to the libraries folder. Any text editor should be able to open the .h and .cpp files for you to look at. The library code for user added libraries is located in the libraries folder along with your sketches in the Arduino documents folder.


MSFfield

Thanks! found it in the folders yes!

Anyone is reading this at a later date and using the Rinky-Dink Electronics, Henning Karlsen DS3231.h the line of  code you need for getting unix is rtc.getUnixTime(rtc.getTime())

:)

Go Up