Pages: [1]   Go Down
Author Topic: arduino due rtc and unixtime ?  (Read 2432 times)
0 Members and 1 Guest are viewing this topic.
NY
Offline Offline
Newbie
*
Karma: 0
Posts: 12
simulate nature using math and technology
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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.   
 smiley-red smiley-red  help please!   smiley-red smiley-red

I do see this part :
Code:
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:
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.   smiley-mr-green
Logged

Germany
Offline Offline
Full Member
***
Karma: 10
Posts: 230
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
 rtc_clock.set_time(__TIME__);
  rtc_clock.set_date(__DATE__);
re-used the code from Lady Ada, unixtime will be the next.

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

Sample and Updated code will follow...

And now completed.

Sample for this:
Code:
#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.

* RTC_for_Due.zip (6 KB - downloaded 23 times.)
« Last Edit: February 28, 2013, 01:31:56 pm by Markus_L811 » Logged

NY
Offline Offline
Newbie
*
Karma: 0
Posts: 12
simulate nature using math and technology
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

WOW!   smiley  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.
Logged

Germany
Offline Offline
Full Member
***
Karma: 10
Posts: 230
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

WOW!   smiley  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.
Logged

NY
Offline Offline
Newbie
*
Karma: 0
Posts: 12
simulate nature using math and technology
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!   smiley-money

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:
Unixtime: 1362130550
And in plain for everyone
Time: 10:35:51
Date: Fri 01.03.2013

but if you punch that timestamp into the converter it gives you
Code:
TIME STAMP: 1362130550

DATE (M/D/Y @ h:m:s): 03 / 01 / 13 @ 3:35:50am EST
« Last Edit: March 01, 2013, 10:37:22 am by tevroc » Logged

Germany
Offline Offline
Full Member
***
Karma: 10
Posts: 230
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!   smiley-money

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:
Unixtime: 1362130550
And in plain for everyone
Time: 10:35:51
Date: Fri 01.03.2013

but if you punch that timestamp into the converter it gives you
Code:
TIME STAMP: 1362130550

DATE (M/D/Y @ h:m:s): 03 / 01 / 13 @ 3:35:50am EST

Whats your timezone?

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

Come from the first link.

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
« Last Edit: March 01, 2013, 11:15:56 am by Markus_L811 » Logged

NY
Offline Offline
Newbie
*
Karma: 0
Posts: 12
simulate nature using math and technology
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

My timezone is (UTC-05:00) Eastern Time (US & Canada).  Lets see...  google translate does a pretty good job on that page.  smiley
Code:
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:
Unixtime: 1362148403
And in plain for everyone
Time: 15:33:23
Date: 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:
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.
Logged

Germany
Offline Offline
Full Member
***
Karma: 10
Posts: 230
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

* RTC_for_Due_v1.3.zip (9.04 KB - downloaded 18 times.)
Logged

NY
Offline Offline
Newbie
*
Karma: 0
Posts: 12
simulate nature using math and technology
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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.
« Last Edit: March 02, 2013, 06:03:13 pm by tevroc » Logged

Germany
Offline Offline
Full Member
***
Karma: 10
Posts: 230
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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.

Sorry for your lost, bad sandy.

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

Germany
Offline Offline
Full Member
***
Karma: 10
Posts: 230
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pages: [1]   Go Up
Jump to: