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