arduino due rtc and unixtime ?

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 :

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.

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

I do see this part :

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!

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. :grin:

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:

  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:

rtc_clock.unixtime()

gives you the time in unixtime style.

Sample and Updated code will follow...

And now completed.

Sample for this:

#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)

WOW! :slight_smile: 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.

tevroc:
WOW! :slight_smile: 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.

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.

its giving me

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

TIME STAMP: 1362130550

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

tevroc:
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/
Unix Time Stamp - Epoch Converter

its giving me

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

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

My timezone is (UTC-05:00) Eastern Time (US & Canada). Lets see... google translate does a pretty good job on that page. :slight_smile:

The timestamp 1362170004 corresponds to the 01.03.2013 at 15:33:24 clock. 
(applies to UTC-5)

And the output from the arduino:

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

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.

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)

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

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.

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

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 :slight_smile:

For all who looking for new versions I will post new ones there:

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

Questions and wishes will still answered and discuss there.

Markus