DS3231 RTC setsyncprovider headache

Hello everybody.

I'm having a real problem trying to get an RTC module to sync its time to an arduino mega2560.
The RTC is the ds3231.
I'm using this library..GitHub - jarzebski/Arduino-DS3231: DS3231 Real-Time-Clock
The code is a hydroponics controller that i've kinda spliced/modified together because untill several months ago, i didn't understand any of this. Thanks to this forum and many others i have learned alot all ready and enjoying it tbh.
The time library here...Time Library, Timekeeping and Time/Date Manipulation on Teensy
The code works well but the system time is crucial to the design of teh controller because of the alarms necessary.

Thanks in advance and sorry if its been asked before. i did look.

comeonenow.ino (9.34 KB)

Hello there and welcome to the forum!

I'm having a real problem trying to get an RTC module to sync its time to an Arduino mega2560.

The code works well but the system time is crucial to the design of teh controller

The post indicates that there is an issue "syncing" with the DS3231.
It is also mentioned that "the code well but ..."

Would it be possible to declare what is happening ? Is the code working well but needs improvement, or is there an issue ?
Do you see / experiment an error messages of some sort when "syncing" with the DS3231?

Best regards,

  • dan

Thanks for the reply dan. much appreciated.

Yeah sorry ill go in more detail. the sketch compiles fine. But the clocks do not sync.
i have attached the code to the previous post.
Im trying to use the setsyncprovider() function to sync the clocks so the system time and the rtc time matches.
The DS3231 has only 2 alarms, and i need 3. So i need the Time library for its alarm.repeat() function.
Any when i run the sketch the controller runs as it should but the 2nd and 3rd alarms(The ones that fire from the system time) dont trigger. Plus the system time shown on the serial monitor is 0:00:00 0 0 1970. So its not sync-ing properly so my alarms wont trigger.

Here is where the problem is i think.
[code dt = clock.getDateTime();

Serial.println(clock.dateFormat("d-m-Y H:i:s - l", dt));
RTCDateTime now = clock.getDateTime();
setSyncProvider(getExternalTime()); // the function to get the time from the RTC
if (timeStatus() != timeSet)
Serial.println("Unable to sync with the RTC");
else
Serial.println("RTC has set the system time");][/code]

not even sure if i need this.

time_t syncProvider();

I am not familiar with the library you use, but I believe your syncProvider function should be returning unixtime. I think this is the syntax for your library and your class definition "clock".

time_t syncProvider()
{
return clock.unixtime();
}

If clock.unixtime() is not the correct call, substitute the commands from the library which call the unixtime.

Then in the body of your code

  setSyncProvider(syncProvider);   // the function to get the time from the RTC
  if (timeStatus() != timeSet) 
     Serial.println("Unable to sync with the RTC");
  else
     Serial.println("RTC has set the system time");

cattledog:
I am not familiar with the library you use, but I believe your syncProvider function should be returning unixtime. I think this is the syntax for your library and your class definition "clock".

time_t syncProvider()

{
return clock.unixtime();
}




If clock.unixtime() is not the correct call, substitute the commands from the library which call the 




Then in the body of your code


setSyncProvider(syncProvider);  // the function to get the time from the RTC
  if (timeStatus() != timeSet)
    Serial.println("Unable to sync with the RTC");
  else
    Serial.println("RTC has set the system time");

Thank you cattledog.
I tried you suggestion but because im at work im using arduinodroid to try and im getting a compilation error. On the curly brace line above return clock.unixtime(); arduino droid doesnt let me copy the error to here.
I had a look through the library for unixtime time and i cant see any calls that stand out.
Im using jarzebskis ds3231 library.

Looking through the library .cpp file i can see
`` uint32_t DS3231::unixtime(void)
{
uint32_t u;

u = time2long(date2days(t.year, t.month, t.day), t.hour, t.minute, t.second);
u += 946681200;

return u;
}
[/code]
Is that the call for unixtime that you mentioned?
If thats the case then what you told me to try is that?
I have looked everywhere online for an answer, its gettinh frustrating.. nobody has a clear answer.
Do i need to learn the c++ langauge just to get it to sync.

Instead of this setsyncprovider that just does not want to work at all could i not save a lot of time if i sync the time manually? Is it possible?

Like this
setTime(hour(dt), minute(dt), second(dt) etc etc..

And just put the code in the loop so it syncs manually through the loop?

Your help guys is much appreciated. Im seconds from throwing this arduino in the bin :frowning:

I have loaded the Jarzebski DS3231 library, and have tested this code to setSynchProvider for the Time library.

#include <Wire.h>         //http://arduino.cc/en/Reference/Wire (included with Arduino IDE)
#include <DS3231.h>    //Jarzebski DS3231

#include <Time.h>         //http://www.arduino.cc/playground/Code/Time  

DS3231 clock;
RTCDateTime dt;

uint32_t syncProvider()
{
  dt = clock.getDateTime();
  return dt.unixtime;
  //return dt.unixtime + 3600; 
 //there may be issues with the library's unix time and daylight or summer time and I had to add 1 hour //to match my rtc settings
}

void setup(void)
{
  Serial.begin(9600);
  Wire.begin();
  clock.begin();
 
  setSyncProvider(syncProvider);   // the function to get the time from the RTC
  if(timeStatus() != timeSet) 
    Serial.println("Unable to sync with the RTC");
  else
    Serial.println("RTC has set the system time");      
}

void loop(void)
{
  timeDateDisplay();  
  delay(1000);
}

void timeDateDisplay(void)
{
 
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(' ');
  Serial.print(month());
  Serial.print("/");
  Serial.print(day());
  Serial.print("/");
  Serial.print(year()); 
  Serial.println(); 
  Serial.print("Unix Time ");
  Serial.println(now());
  Serial.println();
}

void printDigits(int digits)
{
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(':');
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

Mate your a living legend thanks for this.

I got it working in the end by using 2 libraries simultaneously, DS3231(jarzebski) and 1307RTC(adafruit).
But its sloppy, so i tried your way and yes its syncing like a boss only its an hour slow.

{
dt = clock.getDateTime();
return dt.unixtime;
//return dt.unixtime + 3600;
//there may be issues with the library's unix time and daylight or summer time and I had to add 1 hour //to match my rtc settings
}

I un-commented the return dt.unixtime + 3600 but its still an hour slow.

I un-commented the return dt.unixtime + 3600 but its still an hour slow.

Did you comment out the other line when you did that?

uint32_t syncProvider()
{
  dt = clock.getDateTime();
  //return dt.unixtime;
  return dt.unixtime + 3600; 
 //there may be issues with the library's unix time and daylight or summer time and I had to add 1 hour //to match my rtc settings
}

ahh im an onion, thanks mate :wink:

with Sodaq_DS3231 Library

#include "Sodaq_DS3231.h"
#include <Time.h>
#include <TimeAlarms.h>

uint32_t syncProvider()
{
DateTime now = rtc.now(); //get the current date-time
uint32_t ts = now.getEpoch();
return ts;

}

void setup()
{
...
setSyncProvider(syncProvider);
...
}

Hello,

I tried the code cattledog posted above and I get error with the setSyncProvider. I'm a newbie so be patient. The error I get is

In function 'void setup()':
JarzebskiDS3231:28: error: invalid conversion from 'uint32_t ()() {aka long unsigned int ()()}' to 'getExternalTime {aka long int (*)()}' [-fpermissive]
setSyncProvider(syncProvider); // the function to get the time from the RTC
^
In file included from /var/folders/qf/rvfd4hfd2bgcny7cn9bctqpr0000gn/T/arduino_modified_sketch_102724/JarzebskiDS3231.ino:3:0:
/Users/Pete/Documents/Arduino/libraries/Time/TimeLib.h:134:9: error: initializing argument 1 of 'void setSyncProvider(getExternalTime)' [-fpermissive]
void setSyncProvider( getExternalTime getTimeFunction); // identify the external time provider

I'm using a Pixel (ATMEL ATSAMD21) which is incompatible with the DS1307 libraries from my experience. So working with the Time library has been a real headache. I obviously don't understand how to work with SetSyncProvider and the DS3231 or DS3231RTC libraries.

I sure would appreciate a little help to get me past this hurdle.

I think that you are running into issues with the different definition of the time_t unix time variable type in the newer architecture 32 bit systems. It is a signed 64 bit number and not a 32 bit number.

Try changing the data type returned by the syncProvider function to

long int syncProvider()
{
  dt = clock.getDateTime();
  return dt.unixtime;
  //return dt.unixtime + 3600; 
 //there may be issues with the library's unix time and daylight or summer time and I had to add 1 hour //to match my rtc settings
}

So working with the Time library has been a real headache

I have no experience with the 32 processors and the time library or the rtc libraries. If what I suggested doesn't work you may want to ask your question in the Arduino Zero section of the forum.