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.


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() {
  // Publish every 5 minutes at :00, :05, :10, ...

loop() {
  localTimeConvert_NOW.withCurrentTime().convert(); //  use UTC to get local time with LocalTimeRK library
  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.


@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() {
  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
  Log.info("RTC Time is %s", Time.timeStr(Time.now()).c_str());
  // Set up the local time - EAST COAST TIME
  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();

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

        LocalTimeConvert 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));


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.



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