Easymen1971
Member
Hello everyone,
I receive the time via a GPS receiver (neo6mv2 /TinyGPS++.h).
The time (UTC) is then converted with the library (Timezone.h) and rules (CET & CE) to my local time (in Germany).
I would also like to use the internal RTC if the GPS fails or after a power outage.
I've tried a few things (the whole procedure with setSyncProvider(getTeensy3Time) etc. but nothing works.
The RTC always shows the UTC time.
I think it's because of the time_t that's already in use.
What else can I do to ensure that the RTC is synchronized with the correct time (local)?
Can you please help me ?
I almost suspect that I need another RTC because of the TimeLib.h .
CU Martin
I receive the time via a GPS receiver (neo6mv2 /TinyGPS++.h).
The time (UTC) is then converted with the library (Timezone.h) and rules (CET & CE) to my local time (in Germany).
I would also like to use the internal RTC if the GPS fails or after a power outage.
I've tried a few things (the whole procedure with setSyncProvider(getTeensy3Time) etc. but nothing works.
The RTC always shows the UTC time.
I think it's because of the time_t that's already in use.
What else can I do to ensure that the RTC is synchronized with the correct time (local)?
Can you please help me ?
I almost suspect that I need another RTC because of the TimeLib.h .
CU Martin
Code:
EXTMEM char bigBuffer[1000000];
#include <Timezone.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <TimeLib.h>
const int offset = +2; // Central European Time
SoftwareSerial ss();
#define ss Serial1
#include <circular_buffer.h>
#include <isotp.h>
#include <kinetis_flexcan.h>
#include <isotp_server.h>
#include <FlexCAN_T4.h>
#include <imxrt_flexcan.h>
FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> can1;
static CAN_message_t msg;
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
// Software SPI (slower updates, more flexible pin options):
// pin 7 - Serial clock out (SCLK)
// pin 6 - Serial data out (DIN)
// pin 5 - Data/Command select (D/C)
// pin 4 - LCD chip select (CS)
// pin 3 - LCD reset (RST)
Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3);
//define GPS variable
TinyGPSPlus gps;
// Change these two rules corresponding to your timezone, see https://github.com/JChristensen/Timezone
//Central European Time (Frankfurt, Paris) 120 = +2 hours in daylight saving time (summer).
TimeChangeRule CEST = {"CEST", Last, Sun, Mar, 2, 120};
//Central European Time (Frankfurt, Paris) 60 = +1 hour in normal time (winter)
TimeChangeRule CET = {"CET ", Last, Sun, Oct, 3, 60};
Timezone CE(CEST, CET);
// time variables
time_t local, utc, prev_set;
int timesetinterval = 30; //set microcontroller time every 60 seconds
void setup() {
Serial.begin(9600);
ss.begin(9600); //This opens up communications to the GPS
can1.begin();
can1.setBaudRate(125000);
Serial.print("GPS Test");
display.begin();
display.setContrast(75);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(BLACK);
display.setCursor(0, 15);
display.print("Teensy GPS Uhr");
display.display();
smartDelay(1000);
while (!gps.time.isValid()) // wait a valid GPS UTC time (valid is defined in the TinyGPS++ library)
{
smartDelay(1000);
}
setthetime();
prev_set = now();
}
void loop()
{
if (now() - prev_set > timesetinterval && gps.time.isValid()) // set the microcontroller time every interval, only if there is a valid GPS time
{
setthetime();
prev_set = now();
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(BLACK);
display.setCursor(0, 15);
display.println("time is set");
display.display();
smartDelay(1000);
can1.write(msg);
}
displaythetime();
smartDelay(1000); // update the time every second
digitalClockDisplay();
} //end loop
void digitalClockDisplay() {
// digital clock display of the time
//adjustTime(local);
Serial.print("RTC ");
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" ");
Serial.print(day());
Serial.print(" ");
Serial.print(month());
Serial.print(" ");
Serial.print(year());
Serial.println();
}
void printDigits(int digits) {
// utility function for digital clock display: prints preceding colon and leading 0
Serial.print(":");
if (digits < 10)
Serial.print('0');
Serial.print(digits);
}
static void smartDelay(unsigned long ms)
{
unsigned long start = millis();
do
{
// If data has come in from the GPS module
while (ss.available())
gps.encode(ss.read()); // Send it to the encode function
// tinyGPS.encode(char) continues to "load" the tinGPS object with new
// data coming in from the GPS module. As full NMEA strings begin to come in
// the tinyGPS library will be able to start parsing them for pertinent info
} while (millis() - start < ms);
}
void setthetime(void)
{
int Year = gps.date.year();
byte Month = gps.date.month();
byte Day = gps.date.day();
byte Hour = gps.time.hour();
byte Minute = gps.time.minute();
byte Second = gps.time.second();
// Set Time from GPS data string
setTime(Hour, Minute, Second, Day, Month, Year); // set the time of the microcontroller to the UTC time from the GPS
}
void displaythetime(void)
{
utc = now(); // read the time in the correct format to change via the TimeChangeRules
local = CE.toLocal(utc);
if (gps.location.isValid())
{
Serial.print("Latitude: ");
Serial.println(gps.location.lat(), 6);
Serial.print("Longitude: ");
Serial.println(gps.location.lng(), 6);
Serial.print("Altitude: ");
Serial.println(gps.altitude.meters());
Serial.println();
Serial.print("Satelliten");
Serial.print(":");
Serial.print(gps.satellites.value()); // display the number of satellites
Serial.println();
}
else
{
Serial.println("Location: Not Available");
}
Serial.print("Date: ");
if (gps.date.isValid())
{
Serial.print(gps.date.month());
Serial.print("/");
Serial.print(gps.date.day());
Serial.print("/");
Serial.println(gps.date.year());
}
else
{
Serial.println("Not Available");
}
Serial.print("Time: ");
if (gps.time.isValid())
{
if (gps.time.hour() < 10) Serial.print(F("0"));
Serial.print(gps.time.hour() + offset);
Serial.print(":");
if (gps.time.minute() < 10) Serial.print(F("0"));
Serial.print(gps.time.minute());
Serial.print(":");
if (gps.time.second() < 10) Serial.print(F("0"));
Serial.print(gps.time.second());
Serial.print(".");
if (gps.time.centisecond() < 10) Serial.print(F("0"));
Serial.println(gps.time.centisecond());
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(BLACK);
display.setCursor(0, 0);
display.print("Satelliten");
display.setCursor(64, 0);
display.print(gps.satellites.value()); // display the number of satellites
display.setCursor(0, 10);
display.print("Time:");
display.setCursor(30, 10);
if (hour(local) < 10) // add a zero if hour is under 10
display.print("0");
//display.print(gps.time.hour() + offset);
display.print(hour(local));
display.setCursor(40, 10);
display.print(":");
display.setCursor(45, 10);
if (minute(local) < 10) // add a zero if minute is under 10
display.print("0");
display.print(minute(local));
display.setCursor(55, 10);
display.print(":");
display.setCursor(60, 10);
if (second(local) < 10) // add a zero if minute is under 10
display.print("0");
display.print(second(local));
display.setCursor(0, 20);
display.print("Date:");
display.setCursor(30, 20);
display.print(gps.date.day());
display.setCursor(40, 20);
display.print(".");
display.setCursor(48, 20);
display.print(gps.date.month());
display.setCursor(54, 20);
display.print(".");
display.setCursor(60, 20);
display.println(gps.date.year());
display.setCursor(0, 30);
display.print("Lat:");
display.setCursor(25, 30);
display.print(gps.location.lat(), 6);
display.setCursor(0, 40);
display.print("Lon:");
display.setCursor(25, 40);
display.print(gps.location.lng(), 6);
display.display();
msg.flags.extended = 0;
msg.id = 0x40F;
msg.len = 6;
msg.buf[0] = 0x20;
msg.buf[1] = (hour(local)); //hour
msg.buf[2] = (minute(local)); //minute
msg.buf[3] = (gps.date.day()); //decToBcd(tm.Day); //day
msg.buf[4] = (gps.date.month()); //decToBcd(tm.Month); //month
msg.buf[5] = (gps.date.year()); //decToBcd(tm.Year + 1970);//year
}
else
{
Serial.println("Not Available");
}
}
Attachments
Last edited: