Search code examples
pebble-watchpebble-sdkwakeup

Wakup API: waking timing is not consistent


I wrote a very basic app "Nag for Pebble" - all it does is wakes up at given intervals buzzes the vibe, reschedules next wake up and immediately exits the application. So the effect is - "background" buzz at given intervals.

The problem is - time between wakeups keeps increasing even though actual time is consistent. I am doing something like this:

time_t next= time(NULL);  
next  += buzz_interval*60;  
wakeup_schedule(next, 0, false); 

So if buzz_interval is 5min it would wake up every 5min. The problem is after a while time between wakeups keep inreasing, first imperceptible but then error accumulates. Not sure what's causing it and how to compensate for it.

Here's complete source if needed.

Update: Here's a log showing scheduled time of next wake up (Next Time) following by actual wake up time (Current Time). As you can see after a few wake ups it gains a second, then two. Welp,

[DEBUG] main.c:24: Current time = 20:49:59
[DEBUG] main.c:25: Buzz Interval = 1, buzz_start = 0
[DEBUG] main.c:66: Next time = 20:50:59
[DEBUG] main.c:24: Current time = 20:50:59
[DEBUG] main.c:25: Buzz Interval = 1, buzz_start = 0
[DEBUG] main.c:66: Next time = 20:51:59
[DEBUG] main.c:24: Current time = 20:52:00
[DEBUG] main.c:25: Buzz Interval = 1, buzz_start = 0
[DEBUG] main.c:66: Next time = 20:53:00
[DEBUG] main.c:24: Current time = 20:53:00
[DEBUG] main.c:25: Buzz Interval = 1, buzz_start = 0
[DEBUG] main.c:66: Next time = 20:54:00
[DEBUG] main.c:24: Current time = 20:54:00
[DEBUG] main.c:25: Buzz Interval = 1, buzz_start = 0
[DEBUG] main.c:66: Next time = 20:55:00
[DEBUG] main.c:24: Current time = 20:55:01
[DEBUG] main.c:25: Buzz Interval = 1, buzz_start = 0
[DEBUG] main.c:66: Next time = 20:56:01
[DEBUG] main.c:24: Current time = 20:56:01
[DEBUG] main.c:25: Buzz Interval = 1, buzz_start = 0
[DEBUG] main.c:66: Next time = 20:57:01
[DEBUG] main.c:24: Current time = 20:57:03

Solution

  • I think it makes sense that it would be increasing slightly, and the increase is from the time that it takes for the app to open and execute code.

    One way to make sure the wakeups are scheduled at consistent intervals is to keep a persistent variable (https://developer.pebble.com/guides/pebble-apps/app-structure/persistent-storage/) that contains when the first buzzer was buzzed, and with some calculation, you can figure out the exact time that the next buzzer should be scheduled.