DS3231RTC outputs as long integers

If you are not interested in reading why I want to know what, simply read the bottom :slight_smile:

I want to make a simple scheduler for my aunt, something that reminds her of her pills every hour/day etc ... or maybe I want to make something useful once in a while.

The idea is

  • have a .txt on a SD card where I write, line after line, the Unix time and the "reminder" to display on the lcd ( I find very easy to work in Unix time because most pills are ever day (246060=86400 secs or 12 hours and so on, but there are several pills ant different hours of the day)
  • have the UNO confront the "next" Unix time of the "next" line with the Unix time from the DS3231RTC
  • display the reminder, sing a song and then delete the last reminder in order to have a new one available

I wanted to make something similar as the "millis() counter", something like

void loop{
if (millis()-lastTime > 1000){
lastTime=millis();
}

This works because the millis() from the controller outputs an integer, so i can save it like a long int. Hovewer, the Unix time out of the RTC acts differently.

Serial.println(clock.dateFormat("U",  dt)); prints the Unix time, but

unixTime = clock.dateFormat("U",  dt);
Serial.println(unixTime)

always print 1964.

Clearly, the clock.dateFormat command only output a format/information, not an actual number.

Is there a way to get, out of the DS3231RTC, the Unix time as a long integer?

Which library are you using? What data type does the dateFormat() method return?

dt is presumably the unsigned long integer.
Try:

Serial.println(dt);

Pete

You can use Paul Stoffregen's Time library. Pass it the values from the DS3231 and it will return the time in that format, time_t.

https://www.pjrc.com/teensy/td_libs_Time.html

groundFungus:
Which library are you using? What data type does the dateFormat() method return?

I'm currently using the DS3231.h library from jarzebski.
As for the dateFormat() method, I have no clue what method you are referring to :-[
please teach me :slight_smile:

el_supremo:
dt is presumably the unsigned long integer.
Try:

Serial.println(dt);

Pete

It doesn't even compile :frowning:

exit status 1
no matching function for call to 'HardwareSerial::println(RTCDateTime&)'

avr_fred:
You can use Paul Stoffregen's Time library. Pass it the values from the DS3231 and it will return the time in that format, time_t.

Time Library, Timekeeping and Time/Date Manipulation on Teensy

Paul's library is for the DS1307RTC... mine is the DS3231... different RTC need different libraries, am I wrong? And still, Paul's Time.h doesn't seem to include Unix time.
I know I may sound crazy, but using Unix I can program the .txt scheduler using an excel macro, without the Unix time I need to teach my aunt English, or she won't be able to add a new reminder...(she know excel pretty good but can't understand English at all).

char* dateFormat(const char* dateFormat, RTCDateTime dt);

This is the method (member function) from the header file (DS3231.h). The method returns a pointer to a character array (the char* before dateFormat is the return data type).
So try:

Serial.println(atol(unixTime));

Or

longUnixTime = atol(unixTime);

The atol() function returns a long int with a char* as input.

The header file(s) will contain the declarations of all of the member functions (methods). The declarations show what data types the function expects as input and the type of data returned by the function, if any.

Some information on functions.
https://startingelectronics.org/software/arduino/learn-to-program-course/15-functions/

http://playground.arduino.cc/Code/Function

groundFungus, we are getting there.
Just a couple question:

When you say

char* dateFormat(const char* dateFormat, RTCDateTime dt);

you mean

char* unixTime(const char* unixTime, RTCDateTime dt);

right? because we are declaring and defining the input for the atol option, right?

I made this simple code out of the big one, without the scheduler, buzzers etc.

#include <Wire.h>
#include <DS3231.h>

DS3231 clock;
RTCDateTime dt;
long int longUnixTime;
char* unixTime(const char* unixTime, RTCDateTime dt);
//char* dateFormat(const char* dateFormat, RTCDateTime dt); //This gives error because we've not declared unixTime int the scope.

void setup()
{
  Serial.begin(9600);

  Serial.println("Initialize DS3231");;
  clock.begin();
  // clock.setDateTime(__DATE__, __TIME__);
}

void loop()
{
  dt = clock.getDateTime();
  Serial.println(clock.dateFormat("U", dt));
  //longUnixTime = atol(unixTime);
  Serial.println(atol(unixTime));
  delay(1000);
}

this gives an error:

cannot convert ‘char* ()(const char, RTCDateTime)’ to ‘const char*’ for argument ‘1’ to ‘long int atol(const char*)’

The line that I posted is the function definition from the .h file. It shows the syntax of the function. The function is called with a date format specifier and a RTCDateTime object (that contains the date time information) as parameters. The function returns a character array with the Unix time.

This code compiles. See the comments for changes. I can’t test cause I don’t have the right RTC. Note that you don’t declare the function (it is declared in the .h file) just use it with the right input parameters and return type.

#include <Wire.h>
#include <DS3231.h>

DS3231 clock;
RTCDateTime dt;
char* unixTime;  // a string for the output (returned value) of the dateFormat function
long longUnixTime;  // a long variable to hold the long value of unixTime
//char* unixTime(const char* unixTime, RTCDateTime dt); // the function is alreadt declared in the .h file
// no need put anything here.
//char* dateFormat(const char* dateFormat, RTCDateTime dt); //This gives error because we've not declared unixTime int the scope.

void setup()
{
  Serial.begin(9600);

  Serial.println("Initialize DS3231");;
  clock.begin();
  // clock.setDateTime(__DATE__, __TIME__);
}

void loop()
{
  dt = clock.getDateTime();
  unixTime = clock.dateFormat("U", dt); // get the unixTime string (unixTime declared char* above)
  Serial.println(unixTime); // print the unixTime string
  //longUnixTime = atol(unixTime);  // convert to long
  Serial.println(atol(unixTime));  // print the long value of the unixTime string
  delay(1000);
}

It doesn't even compile

Nor does the code you posted.

Pete

Use Jack's DS3232 library (which works on a DS3231) combined with the Time library.
https://github.com/JChristensen/DS3232RTC
And if you want to use timezones, you can use his timezone library to help handle the localtime time shifts and the DST/summer time adjustments:
https://github.com/JChristensen/Timezone

Those will allow you to work with unix epoch timestamps.
You may also want to look at some of the other libraries that work with the Time library such as TimeAlarms.

But for something as vital as medication notifications, I'd recommend using something like the medisafe application:

It is much more than a simple reminder application.
It can track all the medications, can work with a smart watch, and even provides alerts for caregivers in case medications have been missed to name a few of the additional capabilities.

--- bill

groundFungus:
The line that I posted is the function definition from the .h file. It shows the syntax of the function. The function is called with a date format specifier and a RTCDateTime object (that contains the date time information) as parameters. The function returns a character array with the Unix time.

This code compiles. See the comments for changes. I can't test cause I don't have the right RTC. Note that you don't declare the function (it is declared in the .h file) just use it with the right input parameters and return type.

Works flawlessly. Exactly what I was looking for. Thank you for your patience and the explanation :wink: