LocalTimeRK Library stopped working?

Haven’t changed my time management class yet it just stopped working. Below is the code used to set flags so that when reached later it will do something. Im sure what ever I did in some other file some how affected this but I have no idea how that would be, it complies and runs perfeclty fine. However when I put log statements in these ifs the only ones that run are the ones using Millis, none of the ones using the localtimeconvert ever trigger.

void TimingScheduler::updateTime()
{

    time_t now = Time.now();

    unsigned long nowMillis = millis(); // for half second intervals
    if (nowMillis - nextQuaterSecond >= 250)
    {
        nextQuaterSecond = nowMillis;
        markTimeFlagsOnTrackers(TimingInterval::QuaterSecond);
    }
    if (nowMillis - nextHalfSecond >= 500)
    {
        nextHalfSecond = nowMillis;
        markTimeFlagsOnTrackers(TimingInterval::HalfSecond);
    }
    if (!nextSecond || nextSecond <= now)
    {
        LocalTimeConvert conv;
        conv.withCurrentTime().convert();
        conv.addSeconds(1);
        nextSecond = conv.time;
        markTimeFlagsOnTrackers(TimingInterval::Second);
    }
    if (!nextHalfMinute || nextHalfMinute <= now)
    {
        LocalTimeConvert conv;
        conv.withCurrentTime().convert();
        conv.addSeconds(30);
        nextHalfMinute = conv.time;
        markTimeFlagsOnTrackers(TimingInterval::HalfMinute);
    }

    if (!nextMinutely || nextMinutely <= now)
    {
        LocalTimeConvert conv;
        conv.withCurrentTime().convert();
        conv.nextMinute();
        nextMinutely = conv.time;
        markTimeFlagsOnTrackers(TimingInterval::Minute);
    }
    if (!nextTenMinute || nextTenMinute <= now)
    {
        LocalTimeConvert conv;
        conv.withCurrentTime().convert();
        conv.nextMinuteMultiple(10);
        nextTenMinute = conv.time;
        markTimeFlagsOnTrackers(TimingInterval::TenMinute);
    }
    if (!nextHalfHour || nextHalfHour <= now)
    {
        LocalTimeConvert conv;
        conv.withCurrentTime().convert();
        conv.nextMinuteMultiple(30);
        nextHalfHour = conv.time;
        markTimeFlagsOnTrackers(TimingInterval::HalfHour);
    }
    if (!nextHourly || nextHourly <= now)
    {
        LocalTimeConvert conv;
        conv.withCurrentTime().convert();
        conv.nextHour();
        nextHourly = conv.time;
        markTimeFlagsOnTrackers(TimingInterval::Hour);
    }
    if (!nextDaily || nextDaily <= now)
    {
        LocalTimeConvert conv;
        conv.withCurrentTime().convert();

        conv.nextDayMidnight();
        nextDaily = conv.time;
        markTimeFlagsOnTrackers(TimingInterval::Daily);
    }
    if (!nextMonth || nextMonth <= now)
    {
        LocalTimeConvert conv;
        conv.withCurrentTime().convert();
        conv.nextDayOfNextMonth(1);
        nextMonth = conv.time;
        markTimeFlagsOnTrackers(TimingInterval::Monthly);
    }
    if (!timeSyncOClock || timeSyncOClock <= now)
    {
        LocalTimeConvert conv;
        conv.withCurrentTime().convert();
        conv.nextDay(LocalTimeHMS("03:30:30")); // dont run anything super importnat around 3:30:30
                                                // as the particle time sync will mess with time related things for a short period
        timeSyncOClock = conv.time;
        Particle.syncTime();
    }
}

Does the device have valid RTC time from the cloud? You should check Time.isValid() before doing the whole block of things that rely on LocalTimeRK, which requires valid clock time.

1 Like
0000044201 [comm] INFO: Sending TIME request
0000046188 [app] INFO: Time not valid yet
0000046188 [app] INFO: Time sync already pending
0000048189 [app] INFO: Time not valid yet
0000048198 [app] INFO: Time sync already pending
0000049046 [comm.protocol] INFO: Received TIME response: 1759496481
0000050190 [app] INFO: Time not valid yet
0000050201 [comm] INFO: Sending TIME request
0000050523 [comm.protocol] INFO: Received TIME response: 1759496482
0000052191 [app] INFO: Time not valid yet
0000052202 [comm] INFO: Sending TIME request
0000052402 [comm.protocol] INFO: Received TIME response: 1759496484
0000054192 [app] INFO: Time not valid yet
0000054203 [comm] INFO: Sending TIME request
0000054528 [comm.protocol] INFO: Received TIME response: 1759496486
0000056193 [app] INFO: Time not valid yet
0000056208 [comm] INFO: Sending TIME request
0000056416 [comm.protocol] INFO: Received TIME response: 1759496488
0000019670 [app] INFO: Time not valid yet
0000019670 [app] INFO: Time sync already pending

  static unsigned long lastTimeCheck = 0;
  if (millis() - lastTimeCheck > 2000 && !Time.isValid())
  {
    lastTimeCheck = millis();
    Log.info("Time not valid yet");
    if (Particle.syncTimePending())
    {
      Log.info("Time sync already pending");
      return;
    }
    Particle.syncTime();

    return;
  }

Is it not able to valid the time for some reason ?

0000009482 [app] INFO: Not connected to cloud yet
0000009482 [app] INFO: Not connected to cloud yet
0000009483 [system] INFO: Cloud connected
0000009482 [app] INFO: Not connected to cloud yet
0000009494 [comm] INFO: Sending TIME request
0000009603 [app] INFO: Sys free mem: 77328 bytes
0000010109 [comm.protocol] INFO: Received TIME response: 1759497813

Seems to have fixed itself? Not sure if it was a spotty connection or what but, now the code has more robust checks for the future, Thanks!

You shouldn't call syncTime that often. It happens automatically when you connect to the cloud, so you don't need to do that again. If you are continuously connected (not using sleep mode) you can call it once a day to keep the clock more accurate. Because syncTime is handled from the system thread I think you may be running into a race condition where the time hasn't been updated yet, but then you syncTime again, which invalidates it.

2 Likes

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