RTClib; using adjust and passing unixtime

For other supported Arduino products from Adafruit: Shields, accessories, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
tech500
 
Posts: 199
Joined: Wed Dec 04, 2013 3:53 pm

RTClib; using adjust and passing unixtime

Post by tech500 »

Does the RTClib offer setting the time using adjust and passing unixtime? Perhaps once a day or some yet to be determine interval.

Project will be remotely located with GPS module; would like to be able to set DS3231 via GPS.

Code: Select all

void RTC_UPDATE()  //This code was used with a different DS3231 library; does not support the ESP32, as far as I can tell.
{

    ESP.wdtDisable();
        
    Serial.println("");
    Serial.print(F("GPS UTC Date/Time: "));
    
    if (gps.date.isValid())
    {
      Serial.print(gps.date.month());
      Serial.print(F("/"));
      Serial.print(gps.date.day());
      Serial.print(F("/"));
      Serial.print(gps.date.year());
    }
    else
    {
        Serial.print(F("INVALID"));
    }
  
    Serial.print(F(" "));
    
    if (gps.time.isValid())
    {
  
      if (gps.time.hour() < 10) Serial.print(F("0"));
      Serial.print(gps.time.hour());
      Serial.print(F(":"));
      
      if (gps.time.minute() < 10) Serial.print(F("0"));
      Serial.print(gps.time.minute());
      Serial.print(F(":"));
      
      if (gps.time.second() < 10) Serial.print(F("0"));
      Serial.print(gps.time.second());
      Serial.print(F("."));
      
      if (gps.time.centisecond() < 10) Serial.print(F("0"));
      Serial.print(gps.time.centisecond());
      Serial.println("");
      
    }
    else
    {
        Serial.print(F("INVALID"));
    }
    
       struct tm  timeinfo;
      unsigned long int unixtime; 

      timeinfo.tm_year =  gps.date.year() - 1900;
      timeinfo.tm_mon = gps.date.month() - 1;
      timeinfo.tm_mday =  gps.date.day();
      timeinfo.tm_hour =  gps.time.hour();
      timeinfo.tm_min =  gps.time.minute();
      timeinfo.tm_sec = gps.time.second();

      unixtime = mktime(&timeinfo);
      Serial.println("");
      printf("unixtime = %u\n", unixtime);
      
      Clock.setDateTime(unixtime);

      Serial.println("RTC updated");
      
     ESP.wdtEnable(1000);

}  
Using Adafruit's Espressif ESP32 Development Board - Developer Edition, for project development.

William

User avatar
adafruit_support_carter
 
Posts: 29056
Joined: Tue Nov 29, 2016 2:45 pm

Re: RTClib; using adjust and passing unixtime

Post by adafruit_support_carter »

The adjust function takes a DateTime object. Here are the available constructors:
https://github.com/adafruit/RTClib/blob ... Clib.h#L32
This one looks promising:
https://github.com/adafruit/RTClib/blob ... ib.cpp#L74

User avatar
tech500
 
Posts: 199
Joined: Wed Dec 04, 2013 3:53 pm

Re: RTClib; using adjust and passing unixtime

Post by tech500 »

adafruit_support_carter wrote:The adjust function takes a DateTime object. Here are the available constructors:
https://github.com/adafruit/RTClib/blob ... Clib.h#L32
This one looks promising:
https://github.com/adafruit/RTClib/blob ... ib.cpp#L74
I do not understand C++ well enough to make use of this information; please, an example would be helpful and appreciated.
Have search here and with "Google" to find an example; without success.

William

User avatar
tech500
 
Posts: 199
Joined: Wed Dec 04, 2013 3:53 pm

Re: RTClib; using adjust and passing unixtime

Post by tech500 »

I think this is what I was looking for: rtc.adjust(DateTime(unixtime)); Here is the ESP32 sketch that uses the method:

Code: Select all

// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"
#include <TinyGPS++.h> //http://arduiniana.org/libraries/tinygpsplus/ Used for GPS parsing

HardwareSerial uart(1);

TinyGPSPlus gps;

RTC_DS3231 rtc;

int DOW, MONTH, DATE, YEAR, HOUR, MINUTE, SECOND;

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

void setup ()
{

#ifndef ESP8266
     while (!Serial); // for Leonardo/Micro/Zero
#endif

     Serial.begin(9600);
     uart.begin(9600, SERIAL_8N1, 17, 16);

     delay(3000); // wait for console opening

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

     if (rtc.lostPower())
     {
          Serial.println("RTC lost power, lets set the time!");
          // following line sets the RTC to the date & time this sketch was compiled
          //rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
          // This line sets the RTC with an explicit date & time, for example to set
          // January 21, 2014 at 3am you would call:
          rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
     }


}

void loop ()
{

     // This sketch displays information every time a new sentence is correctly encoded.
     while (uart.available() > 0)
          if (gps.encode(uart.read()))
               displayInfo();


     if (millis() > 5000 && gps.charsProcessed() < 10)
     {
          Serial.println(F("No GPS detected: check wiring."));
          while(true);
     }

     Now_Time();

     if((MINUTE % 5 == 0) && (SECOND == 0))  //Change update interval here...
     {
          RTC_UPDATE();

          delay(1000 * 10);

     }



}

void displayInfo()
{

     Serial.print(F("Location: "));
     if (gps.location.isValid())
     {
          Serial.print(gps.location.lat(), 6);
          Serial.print(F(","));
          Serial.print(gps.location.lng(), 6);
     }
     else
     {
          Serial.print(F("INVALID"));
     }

     Serial.print(F("  Date/Time: "));
     if (gps.date.isValid())
     {
          Serial.print(gps.date.month());
          Serial.print(F("/"));
          Serial.print(gps.date.day());
          Serial.print(F("/"));
          Serial.print(gps.date.year());
     }
     else
     {
          Serial.print(F("INVALID"));
     }

     Serial.print(F(" "));
     if (gps.time.isValid())
     {
          if (gps.time.hour() < 10) Serial.print(F("0"));
          Serial.print(gps.time.hour());
          Serial.print(F(":"));
          if (gps.time.minute() < 10) Serial.print(F("0"));
          Serial.print(gps.time.minute());
          Serial.print(F(":"));
          if (gps.time.second() < 10) Serial.print(F("0"));
          Serial.print(gps.time.second());
          Serial.print(F("."));
          if (gps.time.centisecond() < 10) Serial.print(F("0"));
          Serial.print(gps.time.centisecond());
     }
     else
     {
          Serial.print(F("INVALID"));
     }

     Serial.println();


}

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

     YEAR = now.year();
     MONTH = now.month();
     DATE = now.day();
     DOW = now.dayOfTheWeek();
     HOUR = now.hour();
     MINUTE = now.minute();
     SECOND = now.second();

}

void RTC_READ()
{

     DateTime now = rtc.now();

     Serial.print("RTC Date/Time  ");
     Serial.println("");
     Serial.print(now.year(), DEC);
     Serial.print('/');
     Serial.print(now.month(), DEC);
     Serial.print('/');
     Serial.print(now.day(), DEC);
     Serial.print(" (");
     Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
     Serial.print(") ");
     Serial.print(now.hour(), DEC);
     Serial.print(':');
     Serial.print(now.minute(), DEC);
     Serial.print(':');
     Serial.print(now.second(), DEC);
     Serial.println();

}

void RTC_UPDATE()
{

     RTC_READ();

     Serial.println("");
     Serial.print(F("GPS UTC Date/Time: "));

     struct tm  timeinfo;
     unsigned long int unixtime;

     timeinfo.tm_year =  gps.date.year() - 1900;
     timeinfo.tm_mon = gps.date.month() - 1;
     timeinfo.tm_mday =  gps.date.day();
     timeinfo.tm_hour =  gps.time.hour();
     timeinfo.tm_min =  gps.time.minute();
     timeinfo.tm_sec = gps.time.second();

     unixtime = mktime(&timeinfo);
     Serial.println("");
     printf("unixtime = %u\n", unixtime);

     rtc.adjust(DateTime(unixtime));

     Serial.println("RTC updated");
     Serial.println("");

     RTC_READ();

}
Serial Monitor:

Code: Select all

Location: 39.760929,-85.994214  Date/Time: 2/27/2019 21:20:00.00
Location: 39.760929,-85.994214  Date/Time: 2/27/2019 21:20:00.00
Location: 39.760929,-85.994214  Date/Time: 2/27/2019 21:20:00.00
RTC Date/Time  
2019/2/27 (Wednesday) 21:20:0

GPS UTC Date/Time: 
unixtime = 1551302400
RTC updated

RTC Date/Time  
2019/2/27 (Wednesday) 21:20:0
Location: 39.760929,-85.994214  Date/Time: 2/27/2019 21:20:00.00
Location: 39.760929,-85.994214  Date/Time: 2/27/2019 21:20:00.00
Location: 39.760929,-85.994214  Date/Time: 2/27/2019 21:20:00.00
Location: 39.760930,-85.994214  Date/Time: 2/27/2019 21:20:01.00
Location: 39.760930,-85.994214  Date/Time: 2/27/2019 21:20:01.00
Location: 39.760939,-85.994203  Date/Time: 2/27/2019 21:20:11.00
Location: 39.760939,-85.994203  Date/Time: 2/27/2019 21:20:11.00
GPS module used: NEO-M8N Works great inside house; very good sensitivity!

William
Attachments
RTC_with_GPS_Adjust.zip
GPS is used to update DS3231 RTC at a fixed interval.
(1.74 KiB) Downloaded 71 times
Last edited by tech500 on Wed Feb 27, 2019 6:14 pm, edited 1 time in total.

User avatar
adafruit_support_carter
 
Posts: 29056
Joined: Tue Nov 29, 2016 2:45 pm

Re: RTClib; using adjust and passing unixtime

Post by adafruit_support_carter »

That's generally what I was thinking.

Code: Select all

 rtc.adjust(DateTime(unixtime))
You are creating a new DateTime object using one input parameter - that uses the constructor that takes in Unix time.

And that new DateTime object is being passed in to adjust.

User avatar
tech500
 
Posts: 199
Joined: Wed Dec 04, 2013 3:53 pm

Re: RTClib; using adjust and passing unixtime

Post by tech500 »

Added a RTC time stamp to above sketch; in short-time testing and RTC updating appear to be working.

Code: Select all

RTC Timestamp:  2014/01/21 03:03:43
RTC Timestamp:  2014/01/21 03:03:44
RTC Timestamp:  2014/01/21 03:03:45
RTC Timestamp:  2014/01/21 03:03:46
RTC Timestamp:  2014/01/21 03:03:47
RTC Timestamp:  2014/01/21 03:03:48
RTC Timestamp:  2014/01/21 03:03:49
RTC Timestamp:  2014/01/21 03:03:50
RTC Timestamp:  2014/01/21 03:03:51
RTC Timestamp:  2014/01/21 03:03:52
RTC Timestamp:  2014/01/21 03:03:53
RTC Timestamp:  2014/01/21 03:03:54
RTC Timestamp:  2014/01/21 03:03:55
RTC Timestamp:  2014/01/21 03:03:56
RTC Timestamp:  2014/01/21 03:03:57
RTC Timestamp:  2014/01/21 03:03:58
RTC Timestamp:  2014/01/21 03:03:59
RTC Timestamp:  2014/01/21 03:04:00

Location: 39.760973,-85.994238  Date/Time: 3/3/2019 14:48:21.00  UTC
Date/Time:  
unixtime = 1551624501
RTC updated


RTC Timestamp:  2019/03/03 14:48:21
RTC Timestamp:  2019/03/03 14:48:22
RTC Timestamp:  2019/03/03 14:48:23
RTC Timestamp:  2019/03/03 14:48:24
RTC Timestamp:  2019/03/03 14:48:25
RTC Timestamp:  2019/03/03 14:48:26
RTC Timestamp:  2019/03/03 14:48:27
RTC Timestamp:  2019/03/03 14:48:28
RTC Timestamp:  2019/03/03 14:48:29
RTC Timestamp:  2019/03/03 14:48:30
RTC Timestamp:  2019/03/03 14:48:31
RTC Timestamp:  2019/03/03 14:48:32
RTC Timestamp:  2019/03/03 14:48:33
RTC Timestamp:  2019/03/03 14:48:34
RTC Timestamp:  2019/03/03 14:48:35
Occasional invalid GPS data; exits RTC_UPDATE function to avoid updating RTC, DS3231 with invalid data.

I am new to working with GPS modules and working with the Adafruit RTClib library. Looks to me; the
RTC_UPDATE function iterations are sometimes off several seconds, some instances loses a minute why?

Thoughts?

Feedback appreciated. Thank you.

William
Attachments

[The extension ino has been deactivated and can no longer be displayed.]

RTC Timrstamp Output.txt
Several minutes of sketch Serial Monitor output.
(88.2 KiB) Downloaded 72 times

User avatar
adafruit_support_carter
 
Posts: 29056
Joined: Tue Nov 29, 2016 2:45 pm

Re: RTClib; using adjust and passing unixtime

Post by adafruit_support_carter »

The info about RTClib was assuming you were using the Adafruit fork of the library - the links in the previous post.

The GPS library:

Code: Select all

#include <TinyGPS++.h> //http://arduiniana.org/libraries/tinygpsplus/ Used for GPS parsing
and the GPS module you are using:
GPS module used: NEO-M8N
are not ours.

The library we support is here:
https://github.com/adafruit/Adafruit_GPS
which works with our GPS modules:
https://www.adafruit.com/product/746

Locked
Please be positive and constructive with your questions and comments.

Return to “Other Arduino products from Adafruit”