How to combine RTCzero.h and Timelib.h to store a timestamp?

Working with the IOT carrier with a MKR1010 but also with separate MKR1000. Trying to understand the real time clock…

Using RTCzero.h I can set and access the RTC. But if I have a separate timestamp and want to convert that to day, month, it would be nice if RTCzero could do that but I don’t find that. Instead, I tried with Timelib.h but then it does not communicate with the RTC? (I also read about the two not being compatible, but it seem to work in my example).

My question is: how to get a timestamp from the RTC of MKR1000, process it (e.g. store it away), and then get the date, month again…

#include <Arduino_MKRIoTCarrier.h>
#include <RTCZero.h>
#include <TimeLib.h>

MKRIoTCarrier carrier;
RTCZero rtc;

void setup() {

  rtc.begin();

  //Initialize the MKR IoT carrier
  CARRIER_CASE = true;
  carrier.begin();

  //Connect serial
  Serial.begin(9600);
  while (!Serial);    
  Serial.println("Connected to serial");
}

void loop() {

  int sec;
  char cstr[16] = "\0";

  time_t t = now();
  sec = second(); // The second now (0-59)
  Serial.println(sec);
  Serial.println(second(t));
  
  // make time from current RTC timestamp
  sprintf(cstr, "%02d",rtc.getHours());
  sprintf(cstr + strlen(cstr), "%s",":");
  sprintf(cstr + strlen(cstr), "%02d",rtc.getMinutes());
  sprintf(cstr + strlen(cstr), "%s",":");
  sprintf(cstr + strlen(cstr), "%02d",rtc.getSeconds()); 

  Serial.println(cstr);

  delay(1000);
}

Joakim

I use RTCZero with TimeLib

rtc.begin();
setTime(rtc.getEpoch());

after time update to TimeLib

 rtc.setEpoch(now());

Thanks! Here is the code I modified with your suggestions! Works like a charm!

I was first confused as the command getEpoch() was not listed at Arduino - RTC.

#include <Arduino_MKRIoTCarrier.h>
#include <RTCZero.h>
#include <TimeLib.h>

MKRIoTCarrier carrier;
RTCZero rtc;

char timeCstr[16] = "\0";

void setup() {

  rtc.begin();

  //Initialize the MKR IoT carrier
  CARRIER_CASE = true;
  carrier.begin();

  //Connect serial
  Serial.begin(9600);
  while (!Serial);    
  Serial.println("Connected to serial");
}

void loop() {

  time_t t = now();

  Serial.print("Initial timelib time ");
  Serial.println(timeFormat(t));
  
  Serial.print("RTC time " );
  Serial.println(timeFormat(rtc.getEpoch()));

  t = rtc.getEpoch();
  
  setTime(t);
  Serial.print("New timelib time ");
  Serial.println(timeFormat(t));

  delay(5000);
}

char* timeFormat(time_t t) 
{
  static char cstr[16] = "\0";
  sprintf(cstr, "%02d",hour(t));
  sprintf(cstr + strlen(cstr), "%s",":");
  sprintf(cstr + strlen(cstr), "%02d",minute(t));
  sprintf(cstr + strlen(cstr), "%s",":");
  sprintf(cstr + strlen(cstr), "%02d",second(t));
  return cstr;
}

jeniara:
Thanks! Here is the code I modified with your suggestions! Works like a charm!

I was first confused as the command getEpoch() was not listed at Arduino - RTC.

I always look into the source code of libraries

...but today, the code in #2 that worked 2nd of January does not work any more... I get the errors like below...

I read that the two libraries were not compatible, so I tested and it worked. I had a notice about updated libraries, it must med the cause of this problem, but both RTCzero.h 1.6.0 and Timelib.h 1.6.0 are old, no updates there...

I'm really disappointed that working code stops working... Suggestions? Can I go back in time to before the update?

Best regards.
Joakim

C:\Users\Joakim\Documents\Arduino\libraries\RTCZero\src\RTCZero.cpp: In member function 'uint32_t RTCZero::getEpoch()':
C:\Users\Joakim\Documents\Arduino\libraries\RTCZero\src\RTCZero.cpp:379:13: error: aggregate 'RTCZero::getEpoch()::tm tm' has incomplete type and cannot be defined
struct tm tm;
^~
C:\Users\Joakim\Documents\Arduino\libraries\RTCZero\src\RTCZero.cpp:391:10: error: 'mktime' was not declared in this scope
return mktime(&tm);
^~~~~~
C:\Users\Joakim\Documents\Arduino\libraries\RTCZero\src\RTCZero.cpp:391:10: note: suggested alternative: 'mktemp'
return mktime(&tm);
^~~~~~
mktemp
C:\Users\Joakim\Documents\Arduino\libraries\RTCZero\src\RTCZero.cpp: In member function 'void RTCZero::setAlarmEpoch(uint32_t)':
C:\Users\Joakim\Documents\Arduino\libraries\RTCZero\src\RTCZero.cpp:407:22: error: 'gmtime' was not declared in this scope
struct tm* tmp = gmtime(&t);
^~~~~~
C:\Users\Joakim\Documents\Arduino\libraries\RTCZero\src\RTCZero.cpp:407:22: note: suggested alternative: 'setTime'
struct tm* tmp = gmtime(&t);
^~~~~~
setTime
C:\Users\Joakim\Documents\Arduino\libraries\RTCZero\src\RTCZero.cpp:409:21: error: invalid use of incomplete type 'struct RTCZero::setAlarmEpoch(uint32_t)::tm'
setAlarmDate(tmp->tm_mday, tmp->tm_mon + 1, tmp->tm_year - EPOCH_TIME_YEAR_OFF);
^~
C:\Users\Joakim\Documents\Arduino\libraries\RTCZero\src\RTCZero.cpp:407:12: note: forward declaration of 'struct RTCZero::setAlarmEpoch(uint32_t)::tm'
struct tm* tmp = gmtime(&t);
^~
C:\Users\Joakim\Documents\Arduino\libraries\RTCZero\src\RTCZero.cpp:409:23: error: invalid use of incomplete type 'struct RTCZero::setAlarmEpoch(uint32_t)::tm'
setAlarmDate(tmp->tm_mday, tmp->tm_mon + 1, tmp->tm_year - EPOCH_TIME_YEAR_OFF);

try to compile the RTCZero Epoch example

Problem solved! In forums I found several references that it is the file Time.h in Time causing problem. As I understand now, Time.h is no longer used in Time library. All files reference the TimeLib.h but the file Time.h is left in the library for backwards compatibility. The RTCZero lib is using the standard library time.h, and it seems as the Arduino IDE at random (depending on updates of libraries or something) chooses to use Time.h or the correct time.h when compiling RTCZero library. If is chooses Time.h (that only contains #include "TimeLib.h") RTCZero obviously doesn't work.

By simply deleting Time.h from the Time library it works again. On my Windows 10, it can be found at \Documents\Arduino\libraries\Time

A simple update to the IDE would be to submit a warning if there are several libraries with the same name that could give random errors...

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.