Coder Social home page Coder Social logo

Comments (2)

AussieMakerGeek avatar AussieMakerGeek commented on September 22, 2024

I have come across this too and was hoping for an event that might be fired off when the time is synced with NTP successfully.

I have a project that requires very accurate time but may be offline. It is using an RTC as it's time source but synced/Set with ezTime. If the device boots with no internet, it relies on the battery backed RTC only, then theoretically, once the device has internet it will update the RTC. I was using the RTC time to set ezTime on boot but confused that when I called a function containing waitForSync() to update the time on the RTC after internet was connected, it did not update.

i.e

DateTime now = rtc.now();
UTC.setTime(now.unixtime());

if (waitForSync(10)){  //Get the time from the internet..
  rtc.adjust(DateTime(UTC.now())); //Set the time on the RTC
}

UTC.now() will actually be the rtc time and not the internet time, regardless of internet connectivity.

waitForSync() does not seem to actually check internet time if timeStatus() == timeSet (which seems to be the case if you have called setTime() less than 1hr ago.

Because my project will behave differently between RTC time and ezTime, i need to know when ezTime has synced with NTP rather than the RTC.

Author seems to be MIA this year...

Edit: This is confirmed by the source - Line 537 of ezTime.cpp...

if (_time_status != timeSet) {
				infoln(F("Waiting for time sync"));
				while (_time_status != timeSet) {
					if ( timeout && (millis() - start) / 1000 > timeout ) { triggerError(TIMEOUT); return false;};
					delay(250);
					events();
				}
				infoln(F("Time is in sync"));
			}

So because the time is set, regardless of the source, it's not actually checking internet time...

from eztime.

AussieMakerGeek avatar AussieMakerGeek commented on September 22, 2024

I managed to work around this issue by reviewing the source code of the library

If you call updateNTP() directly, rather than using waitForSync() (Which calls uptateNTP() but only if time_status !=timeSet) the time_status should now be either timeNeedsSync or timeSet, purely based on NTP success/fail.

Unfortunately, updateNTP() does not return a variable itself.

I ended up with:

void syncTimeToRTC(){
  //Force the ezTime library to try internet time
  updateNTP();
  // time_status should now be either timeNeedsSync or timeSet 
  if (timeStatus() == timeSet){
    rtc.adjust(DateTime(UTC.now())); //Set the time on the RTC
    rtcInUse = 0;
  }else{
    // Relying on RTC time
    DateTime now = rtc.now();
    UTC.setTime(now.unixtime());  //Set EZTime to whatever the RTC is.
    rtcInUse = 1;
    lastFailure = millis();	  
  }
}

By setting an if statement such as:
if (millis() - lastFailure > 60000 && rtcInUse == 1) syncTimeToRTC();
I can make sure it's always in sync.

I also run it if network gets disconnected/reconnected.

from eztime.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.