LocalTimeRK - Usage question

I have been exploring the LocalTimeRK library and am very impressed with its scheduling capability.

I was wondering if anyone has used this library on a device that sleeps. I have searched the library documentation but I can’t seem to see how to do either of the following:

  • return the number of seconds until the next period so I know how long to sleep, or
  • have a call-back or event from the library as one of the wake triggers in the sleep 2.0 configuration

Without one of these (or another approach I have not thought of) it is hard to see how a device can sleep and still keep the schedule set by this library.

Any help would be appreciated. Otherwise, I will just go back to doing time math and use the library to keep track of daylight savings time - which is still a great benefit.

Chip

1 Like

I must confess I have not used the LocalTimeRK library with the latest scheduling capability available or combined this library with firmware that sleeps. So, I thought I would take a closer look at the scheduling tools in the library. Here goes:

I was able to get a simple schedule working following examples by @rickkas7 in the docs. But, I needed to go into the source code itself to figure this one out. The documentation seems to have changed or I don’t know where to look. Anyway, thanks for asking the question - I learned something today! :slightly_smiling_face:
The code below is a simplified version of what I actually ran to test so it won’t compile. It does show how to get the UTC for now and a future time using LocalTimeRK library. Then you can calculate the difference in seconds for your sleep time. Hope this helps.

#include "LocalTimeRK.h"

LocalTimeConvert localTimeConvert_NOW;
LocalTimeSchedule publishSchedule;

setup() {
  LocalTime::instance().withConfig(LocalTimePosixTimezone("PST8PDT,M3.2.0/2:00:00,M11.1.0/2:00:00"));
  // Publish every 5 minutes at :00, :05, :10, ...
  publishSchedule.withMinuteOfHour(5);
}

loop() {
  ...
  localTimeConvert_NOW.withCurrentTime().convert(); //  use UTC to get local time with LocalTimeRK library
  publishSchedule.getNextScheduledTime(localTimeConvert_NOW);
  uint32_t sleepInMilliseconds = (publishSchedule.nextTime - localTimeConvert_NOW.time) * 1000;
}

Thank you! I knew there must be a way but I could not find this function.

I will give this a try - appreciate the help in finding the way forward.

Chip

@robc ,

It took me longer than it should but I finally got the time to work through this. Couple things:

  1. I do think the documentation is incomplete as these functions are not included in the browsable documentation site. @Colleen perhaps this could be updated?

  2. It took me a bit to get this working and I needed to include the RTC as this is an important part of my implementation. I pasted my code below:

/*
 * Project LocalTimeRK-Test
 * Description: Just trying to wrap my head around this library
 * Author: Chip McClelland based on a post by @Robc
 * Date: 8/24/22
 */
#include "LocalTimeRK.h"
#include "AB1805_RK.h"

LocalTimeConvert localTimeConvert_NOW;
LocalTimeSchedule publishSchedule;

SerialLogHandler logHandler(LOG_LEVEL_INFO);
AB1805 ab1805(Wire);

SYSTEM_MODE(AUTOMATIC);                        // Don't connect
SYSTEM_THREAD(ENABLED);                     // Means my code will not be held up by Particle processes.

void setup() {
  delay(2000);
  Log.info("Starting setup");
  // Set up the Real Time Clock - UTC Time
  ab1805.setup();                           // Initialize the clock
  ab1805.resetConfig();                   // Reset the AB1805 configuration to default values
  waitFor(Time.isValid,90000);
  Log.info("RTC Time is %s", Time.timeStr(Time.now()).c_str());
  // Set up the local time - EAST COAST TIME
  LocalTime::instance().withConfig(LocalTimePosixTimezone("EST5EDT,M3.2.0/2:00:00,M11.1.0/2:00:00"));
  localTimeConvert_NOW.withCurrentTime().convert();
  Log.info("local time: %s", localTimeConvert_NOW.format(TIME_FORMAT_DEFAULT).c_str());
  // Finally set up a recurring timing schedule
  publishSchedule.withMinuteOfHour(5); // Publish every 5 minutes at :00, :05, :10, ...
}

void loop() {
    static unsigned long lastLog = 0;
    if (millis() - lastLog > 10000L && Time.isValid()) {
        lastLog = millis();

        localTimeConvert_NOW.withCurrentTime().convert();
        Log.info("local time: %s", localTimeConvert_NOW.format(TIME_FORMAT_DEFAULT).c_str());

        LocalTimeConvert localTimeConvert_NEXT;
        localTimeConvert_NEXT.withCurrentTime().convert();
        publishSchedule.getNextScheduledTime(localTimeConvert_NEXT);

        Log.info("time of next event is: %s which is %lu seconds away", localTimeConvert_NEXT.format(TIME_FORMAT_DEFAULT).c_str(), (long)(localTimeConvert_NEXT.time - localTimeConvert_NOW.time));
    }

  ab1805.loop();
}

I updated the LocalTimeRK documentation. Well, it actually was updated in the source, but the full API docs did not deploy in Github for some reason, so the API docs did not update.

getNextScheduledTime()

3 Likes

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