[SOLVED]RTC-GPS synchronization

Hi , I am creating a project, which consists of an RTC being reset each time the gps is catching some data , and the rtc taking over and lcd showing rtc data from the time when the GPS loses connection (due to bad weather or unavailable satellite). In each cycle that the GPS module receives data from a satellite, the rtc.set function would set the rtc value to the gps value in real time. I am using the NeoGPS library, which automatically converts the UTC to IST. I tried to code the logic of GPS not receiving signal , which is incorrect.
Could you kindly guide me on how to implement this logic

#include <NMEAGPS.h>
#include <SoftwareSerial.h>
#include <LiquidCrystal_I2C.h>
#include <DS3231.h>
#include<Wire.h>


SoftwareSerial gpsPort( 3, 2 );
LiquidCrystal_I2C lcd(0X27, 16, 2);
NMEAGPS gps;
DS3231 rtc(SDA, SCL);
gps_fix currentfix;
NeoGPS::time_t  localTime;



void setup()
{
  Serial.begin(115200);
  gpsPort.begin(9600);
  lcd.init();
  lcd.backlight();
  rtc.begin();

}

void loop()
{
  // Dispatch incoming characters

  while (gps.available( gpsPort))
  {
    currentfix = gps.read();

    if (currentfix.valid.date && currentfix.valid.time)
    {
      NeoGPS::clock_t localSeconds;
      NeoGPS::time_t  localTime;
      {
        using namespace NeoGPS;
        localSeconds = (clock_t) currentfix.dateTime; // convert structure to a second count
        localSeconds += 5 * SECONDS_PER_HOUR + 30 * SECONDS_PER_MINUTE; // shift timezone
        localTime = localSeconds;              // convert back to a structure
      }

      lcd.setCursor(3, 0);
      lcd.print( localTime.date );
      lcd.print( '/' );
      lcd.print( localTime.month );
      lcd.print( '/' );
      lcd.print( localTime.year );

      lcd.setCursor(3, 1);
      lcd.print( localTime.hours );
      Serial.print( localTime.hours );
      lcd.print( ':' );
      Serial.print( ':' );
      if (localTime.minutes < 10) lcd.print(F("0"));
      lcd.print( localTime.minutes );
      Serial.print( localTime.minutes );
      lcd.print( ':' );
      Serial.print(':');
      if (localTime.seconds < 10) lcd.print(F("0"));
      lcd.print(localTime.seconds);
      Serial.print( localTime.seconds );
      Serial.println();

    }
  }



  //    while(!gps.available(gpsPort)
  //   {
  //
  //            lcd.clear();
  //            //rtc.setTime(localTime.hours,localTime.minutes,localTime.seconds);
  //            lcd.setCursor(0,0);
  //            lcd.print(rtc.getTimeStr());
  //            delay(1000);
  //   }
}

Please explain what you expect to happen, and what happens instead.

Of course you must be outside, with a clear view of the sky, in order to get a satellite fix.

1 Like

I suspect this approach will always have logic problems. Consider only making changes to the clock is the time difference is over some value you pick.

1 Like

i found the solution now. There was a minor error in logic , which i couldn't pickup at first. Now the issue is resolved.
I wanted to set the rtc value in real time , fed by the GPS time input.

Hi.
You might find this project interesting or helpful:

1 Like

thank you

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