Hi,
how can I get time stamp from RTC DS3231? I don't care if it is not classic UNIX but I need time as one data type (ulong). Or do you know about some better solve for programmable alarm clock with DS3231?
I tried to use in-build alarms but it was mismatch.
Thank you for reply!
I tried to use in-build alarms but it was mismatch.
Post what you tried and the error message(s)
Which library? With this one GitHub - JChristensen/DS3232RTC: Arduino Library for Maxim Integrated DS3232 and DS3231 Real-Time Clocks you can simply call RTC.get()
Sorry, it was some time ago and I deleted that code.
Thank for that library, and I think I will try this one: GitHub - sleemanj/DS3231_Simple: An Arduino Library for EASY communication with DS3231 I2C RTC Clock and Atmel AT24C32 I2C EEPROM commonly found on the same board. Implements setting, getting the time/date, setting, checking and clearing alarms, and dead-easy circular-buffered logging of data with timestamp.
how can I get time stamp from RTC DS3231? I don't care if it is not classic UNIX but I need time as one data type (ulong).
Can you try to build DS3231 Based Clock as per following diagram? The program codes (given below) collects Date and Time from the RTC Chip and shows them on the LCD. And then, you try yourself how to convert the Time string into integer; this is what you are looking for.
#include <Wire.h> //needed because DS3231 uses I2C Bus
#include <RTClib.h> //needed becuase we have ready-made functions of this librray
#include<LiquidCrystal_I2C.h> //neded by LCD
LiquidCrystal_I2C lcd(0x27, 16, 2); //lcd address; 16 characters 2 line
RTC_DS3231 rtc; //the object rtc is created from the class RTC_DS3231
char daysOfTheWeek[7][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
void setup()
{
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
if (! rtc.begin()) //DS3231 is intialized
{
while (1);
}
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));//auto update from computer time
//rtc.adjust(DateTime(2014, 1, 21, 3, 10, 17));//set date-time manualy:yr,mo,dy,hr,mn,se
}
void loop()
{
//--read Date and Tme from DS3231 using the method now()---------------------;
//--store the Date and Time into another user define object bamed nowTime
DateTime nowTime = rtc.now();
//---show Day and Date on Top Line of LCD--------------
int x = nowTime.dayOfTheWeek(); //dyOfTheWeek() is a pre-defined method
lcd.print(daysOfTheWeek[x]); //days of the Week[] is the user defined array
lcd.print(": ");
lcd.print(nowTime.day());
lcd.print("/");
lcd.print(nowTime.month());
lcd.print("/");
lcd.print(nowTime.year());
//------show Time on Bottom Line of LCD--------------
lcd.setCursor(0, 1);
lcd.print(nowTime.hour());
lcd.print(":");
lcd.print(nowTime.minute());
lcd.print(":");
lcd.print(nowTime.second());
lcd.setCursor(0, 0);
}
BTW: Second Field has some bugs; it comes like: 58,59 , 09, 19, ...., 99, 10, 11, .....57, 58? I have just seen it after submitting the post. Trying to fix it, If it is a library problem, then it will take some time to fix it!!
Alternate Codes that show Time correctly.
#include <Wire.h> //needed because DS3231 uses I2C Bus
#include <RTClib.h> //needed becuase we have ready-made functions of this librray
#include<LiquidCrystal_I2C.h> //neded by LCD
LiquidCrystal_I2C lcd(0x27, 16, 2); //lcd address; 16 characters 2 line
RTC_DS3231 rtc; //the object rtc is created from the class RTC_DS3231
char daysOfTheWeek[7][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
#define deviceAddress 0x68
void setup()
{
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
if (! rtc.begin()) //DS3231 is intialized
{
while (1);
}
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));//auto update from computer time
//rtc.adjust(DateTime(2014, 1, 21, 3, 10, 17));//set date-time manualy:yr,mo,dy,hr,mn,se
}
void loop()
{
//--read Date and Tme from DS3231 using the method now()---------------------;
//--store the Date and Time into another user define object bamed nowTime
DateTime nowTime = rtc.now();
//---show Day and Date on Top Line of LCD--------------
int x = nowTime.dayOfTheWeek(); //dyOfTheWeek() is a pre-defined method
lcd.print(daysOfTheWeek[x]); //days of the Week[] is the user defined array
lcd.print(": ");
lcd.print(nowTime.day());
lcd.print("/");
lcd.print(nowTime.month());
lcd.print("/");
lcd.print(nowTime.year());
/*
//------show Time on Bottom Line of LCD--------------
lcd.setCursor(8, 1);
lcd.print(nowTime.hour());
lcd.print(":");
lcd.print(nowTime.minute());
lcd.print(":");
lcd.print(nowTime.second());
lcd.setCursor(0, 0);
*/
//void showTimeOnLCD()
//{
Wire.beginTransmission(deviceAddress); //START, Roll Cal
Wire.write(0x00); //set SEC Register address
Wire.endTransmission(); //Execute the above queued data, ACK, STOP
Wire.requestFrom(deviceAddress, 3); //SEC, MIN, and HRS to read from RTC as BCD
byte bcdSeconds = Wire.read();
byte bcdMinutes = Wire.read();
byte bcdHours = Wire.read();
bcdHours = bcdHours & 0b00011111;
lcd.setCursor(0, 1);
//show HRS--
lcd.write((bcdHours>>4) + 0x30);
lcd.write((bcdHours & 0x0F) + 0x30);
lcd.write(':');
//show MIN--
lcd.write((bcdMinutes>>4) + 0x30);
lcd.write((bcdMinutes & 0x0F) + 0x30);
lcd.print(':');
//shiw SEC
lcd.write((bcdSeconds>>4) + 0x30);
lcd.write((bcdSeconds & 0x0F) + 0x30);
lcd.setCursor(0, 0);
}