RTClib how to get epoch

im using adafruit RTClib. i had a look at the classes but it don't mention epoch all i know is how to retrieve datetime now(). how do i set time and get epoch??

I tried this but it did not work,


#include <Wire.h>
#include "RTClib.h"
RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};


void setup() 
{

Serial.begin(9600);

if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}

rtc.adjust(DateTime(__DATE__, __TIME__));

delay(3000);
}

void loop()
{
DateTime now = rtc.now();
DateTime epoch32 = rtc.secondstime();//

}

complains,

exit status 1
'class RTC_DS3231' has no member named 'secondstime'

Never mind i figured it out.


#include <Wire.h>
#include "RTClib.h"
RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};


void setup() 
{

Serial.begin(115200);

if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}

rtc.adjust(DateTime(__DATE__, __TIME__));

delay(3000);
}

void loop()
{
DateTime now = rtc.now();

unsigned long secondstime = now.secondstime();
Serial.println(secondstime);
delay(1000);
}

I realized secondstime is not a member of rtc it is infact a member of DateTime, right?

You might want unixTime() from your DateTime, see

1 Like

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