Go Down

Topic: arduino due rtc and unixtime ? (Read 5180 times)previous topic - next topic

tevroc

Feb 28, 2013, 12:14 am
My old sketch was written for DS1307 which can give you unixtime and a great portion of my sketch used it.  I have not found a way to get unixtime from the RTC on Due.  Does anyone know how to do this?

If not, I will either re-write my sketch from scratch or just write a function to convert to unix time and use that.

I would rather be able to just get unixtime directly off the chip when i need it though.   It actually makes certain calculations easier like if you wanted to track cycles that do not line up with months, days, weeks, etc... such as the lunar cycle.  You just use modulus.  To find out exactly what point you are during a cycle you simply :

Code: [Select]
`current_cycle_position  = now.unixtime() % total_cycle_length;   `

and you get what seconds you are at in any time period you want.  It REALLY simplifies certain things.

I tried looking at the files buried in "C:\...\arduino-1.5.2\hardware\arduino\sam\system\CMSIS\Device\ATMEL\sam3xa\include\component" named component_rtc.h and instance_rtc.h (in the \instance folder) but didn't see anything called or anything having to do with it unixtime.  (but i admit i only quickly look at at it)

I was using #include "RTClib.h" from ladyada and everything was working great.  After looking at the source code for that it seems that the DS1307 only gives you a timestamp from 2000 (which would also work just fine!) and to convert to unixtime they add the amount of seconds from 1970 to 2000.

Code: [Select]
`uint32_t DateTime::unixtime(void) const {  uint32_t t;  uint16_t days = date2days(yOff, m, d);  t = time2long(days, hh, mm, ss);  t += SECONDS_FROM_1970_TO_2000;  // seconds from 1970 to 2000  return t;}`

Now if i could only figure out how to do this with the sam3x rtc.  I'm looking at the code for the rtc library for the due located here " https://github.com/MarkusLange/Arduino-Due-RTC-Library/blob/master/rtc_clock.cpp " and im having troubles figuring out how it works.

I do see this part :
Code: [Select]
`uint32_t RTC_clock::current_time(){uint32_t dwTime;/* Get current RTC time */dwTime = RTC->RTC_TIMR ;while ( dwTime != RTC->RTC_TIMR ) { dwTime = RTC->RTC_TIMR ; } return (dwTime);}`

There is also this line in the ladyada lib that was super convenient!

Code: [Select]
`RTC.adjust(DateTime(__DATE__, __TIME__));`

putting that in the setup function would set the current time to the time the sketch was compiled.  Very nice feature I want to figure out how to get to work on the due.

Actually been searching the entire time while typing this thread, but its time to eat dinner now, so i'm hitting the post button.  Will update thread if/as i solve these problems, after dinner.

Markus_L811

#1
Feb 28, 2013, 11:32 amLast Edit: Feb 28, 2013, 07:31 pm by Markus_L811 Reason: 1
Did you have a link to the Adafruit library if I find some time I will convert it. It's my fault I hadn't any use for the unixtime so i don't implement it sorry...

So the use of the compiletime is implemented, sort of this to use it:
Code: [Select]
`  rtc_clock.set_time(__TIME__);  rtc_clock.set_date(__DATE__);`

So unixtime implemented like the Adafruit library solved the problem:
Code: [Select]
`rtc_clock.unixtime()`
gives you the time in unixtime style.

Sample and Updated code will follow...

And now completed.

Sample for this:
Code: [Select]
`#include <rtc_clock.h>// Select the Slowclock source//RTC_clock rtc_clock(RC);RTC_clock rtc_clock(XTAL);char* daynames[]={"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};void setup() {  Serial.begin(9600);  rtc_clock.init();  rtc_clock.set_time(__TIME__);  rtc_clock.set_date(__DATE__);}void loop() {  Serial.print("Unixtime: ");  Serial.println(rtc_clock.unixtime());  Serial.println("And in plain for everyone");  Serial.print("Time: ");  digitprint(rtc_clock.get_hours(), 2);  Serial.print(":");  digitprint(rtc_clock.get_minutes(), 2);  Serial.print(":");  digitprint(rtc_clock.get_seconds(), 2);  Serial.println("");  Serial.print("Date: ");  Serial.print(daynames[rtc_clock.get_day_of_week()-1]);  Serial.print(" ");  digitprint(rtc_clock.get_days(), 2);  Serial.print(".");  digitprint(rtc_clock.get_months(), 2);  Serial.print(".");  Serial.println(rtc_clock.get_years());  Serial.println("");}void digitprint(int value, int lenght){  for (int i = 0; i < (lenght - numdigits(value)); i++){    Serial.print("0");  }  Serial.print(value);}int numdigits(int i){  int digits;  if (i < 10)    digits = 1;  else    digits = (int)(log10((double)i)) + 1;  return digits;}`

New library is attached, tevroc would you like to test it.

tevroc

#2
Feb 28, 2013, 07:09 pm
WOW!     What fast service!  Thank you!  I would definitely like to try it but i have to leave for work.  I will try it tonight when i get home.

Markus_L811

#3
Mar 01, 2013, 02:42 pm

WOW!     What fast service!  Thank you!  I would definitely like to try it but i have to leave for work.  I will try it tonight when i get home.

No not for that, I needed an reason to get an hand on it thanks, it's my pleasure if someone used it, btw there is an new version online.

tevroc

#4
Mar 01, 2013, 04:11 pmLast Edit: Mar 01, 2013, 04:37 pm by tevroc Reason: 1
The example did not compile at first.  Then i realized it was using the old header file from the other folder for some reason.  after uninstalling your old library, it worked!  thanks again!   \$)

edit: upon further inspection something is off.  The unix time stamp that the example gives is off by a few hours.  i used a couple of different websites to verify this.

http://www.epochconverter.com/
http://www.unixtimestamp.com/index.php

its giving me
Code: [Select]
`Unixtime: 1362130550And in plain for everyoneTime: 10:35:51Date: Fri 01.03.2013`

but if you punch that timestamp into the converter it gives you
Code: [Select]
`TIME STAMP: 1362130550DATE (M/D/Y @ h:m:s): 03 / 01 / 13 @ 3:35:50am EST`

Markus_L811

#5
Mar 01, 2013, 05:08 pmLast Edit: Mar 01, 2013, 05:15 pm by Markus_L811 Reason: 1

The example did not compile at first.  Then i realized it was using the old header file from the other folder for some reason.  after uninstalling your old library, it worked!  thanks again!   \$)

edit: upon further inspection something is off.  The unix time stamp that the example gives is off by a few hours.  i used a couple of different websites to verify this.

http://www.epochconverter.com/
http://www.unixtimestamp.com/index.php

its giving me
Code: [Select]
`Unixtime: 1362130550And in plain for everyoneTime: 10:35:51Date: Fri 01.03.2013`

but if you punch that timestamp into the converter it gives you
Code: [Select]
`TIME STAMP: 1362130550DATE (M/D/Y @ h:m:s): 03 / 01 / 13 @ 3:35:50am EST`

Convert epoch to human readable date and vice versa
1362130550   [batch convert timestamps to human dates]
GMT: Fri, 01 Mar 2013 09:35:50 GMT
Your time zone: Freitag, 1. März 2013 10:35:50 GMT+1

Sorry it's german but on this page you can change the timezone http://www.aritso.net/aritso-tools/timestampconverter/ and you will get other times for the unixtime

tevroc

#6
Mar 01, 2013, 09:45 pm
My timezone is (UTC-05:00) Eastern Time (US & Canada).  Lets see...  google translate does a pretty good job on that page.
Code: [Select]
`The timestamp 1362170004 corresponds to the 01.03.2013 at 15:33:24 clock. (applies to UTC-5)`

And the output from the arduino:
Code: [Select]
`Unixtime: 1362148403And in plain for everyoneTime: 15:33:23Date: Fri 01.03.2013`

You can see that the two unix timestamps are quite a bit different.  If i plug the arduino's timestamp into the converter (making sure to choose the UTC-5 timezone) it gives me this
Code: [Select]
`Der Timestamp 1362148403 entspricht dem 01.03.2013 um 09:33:23 Uhr.(gilt für UTC-5)`  which seems to be slow by exactly 6 hours.

Markus_L811

#7
Mar 02, 2013, 09:07 pm
So tevroc I have make some changes in the rtc_clock.unixtime() function as I had to find out that not all people live in my timezone so you can change it, you can insert rtc_clock.unixtime(UTC-5) or some else UTC+1230 or other known timezones to make an addoption to your time zone.

New one attached please test if I shift in the right direction.

tevroc

#8
Mar 02, 2013, 11:48 pmLast Edit: Mar 03, 2013, 12:03 am by tevroc Reason: 1
I'm sorry i forgot to give you karma until today so i will have to keep giving you karma now for helping me.

I have already done this in a much dirtier way by  adding six years worth of seconds to the line in rtc_clock.h #define SECONDS_FROM_1970_TO_2000 946,702,800.  I like yours better though, cleaner and works for everybody.  I will install and test.

You know, this has me thinking...  I never checked the unixtime from my old sketch (which i ran on uno w/ DS1307 rtc).  I wonder if it was accurate because it didn't have a way to put the timezone in afaik.  oh well.. i'm too lazy to check because the old project is now a heap of crap in the garage because hurricane sandy knocked my power out for 17 days and the entire aquarium died and it started to reek of low tide. anyways, enough babbling..  kudos!

edit:  It does seem to be working perfectly now...  karma ++ for you..  as soon as i can again because it says i cant do it more than one time per hours.

Markus_L811

#9
Mar 04, 2013, 09:45 am

I'm sorry i forgot to give you karma until today so i will have to keep giving you karma now for helping me.

I have already done this in a much dirtier way by  adding six years worth of seconds to the line in rtc_clock.h #define SECONDS_FROM_1970_TO_2000 946,702,800.  I like yours better though, cleaner and works for everybody.  I will install and test.

You know, this has me thinking...  I never checked the unixtime from my old sketch (which i ran on uno w/ DS1307 rtc).  I wonder if it was accurate because it didn't have a way to put the timezone in afaik.  oh well.. i'm too lazy to check because the old project is now a heap of crap in the garage because hurricane sandy knocked my power out for 17 days and the entire aquarium died and it started to reek of low tide. anyways, enough babbling..  kudos!

edit:  It does seem to be working perfectly now...  karma ++ for you..  as soon as i can again because it says i cant do it more than one time per hours.

Thanks for the karma. So new version only ready for Summertime

Markus_L811

#10
Mar 16, 2013, 11:37 am
For all who looking for new versions I will post new ones there:

http://arduino.cc/forum/index.php/topic,144446.msg1084672.html#msg1084672

And on the git link to the git is also in the link above.

Questions and wishes will still answered and discuss there.

Markus

Go Up

Please enter a valid email to subscribe