# Have System.sleep SLEEP\_MODE\_DEEP wakeup at the same time everday

**URL:** <https://community.particle.io/t/have-system-sleep-sleep-mode-deep-wakeup-at-the-same-time-everday/18720>\
**Category:** Cloud\
**Created:** [January 2, 2016, 1:48pm UTC](https://community.particle.io/t/have-system-sleep-sleep-mode-deep-wakeup-at-the-same-time-everday/18720 "2016-01-02T13:48:33Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![autolib](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@autolib](https://community.particle.io/u/autolib)\
**Post date:** [January 2, 2016, 1:48pm UTC](https://community.particle.io/t/have-system-sleep-sleep-mode-deep-wakeup-at-the-same-time-everday/18720/1 "2016-01-02T13:48:33Z")

</div>

I have the code segment below that reads sensors between the hours of 13 and 24. Everything works fine looping around every hour. The problem aries at the end of the day because as the day progressed with the built in delays each hour the code is running a few minutes later in time so if I set the sleep at the XXXX point in the code for 13 hours the unit will not wake up exactly at 13 but the number of minutes that got delayed from the previous day. Eventually over a few days the unit is waking up later and later. If you look at a snap shot of my log for one period of time the unit came online at 10:35:19 and went offline at 10:36:27.

 ![](https://us1.discourse-cdn.com/flex026/uploads/particle/original/2X/e/ea10fbe76a4f67bfaf8479685ba07dc8ba666fdf.png)  
The next iteration the unit will come on two minutes later and so on and so on.

What I am looking for is something similar to in the electric imp API server.sleepuntil(13, 0) where I am guaranteed that the  
unit will wakeup at hour 13.

```auto
if ((Time.hour() >= 13) && (Time.hour() <= 24) )
{
.
.
. 
 
 delay(10000); // to get ready for sensors 

  .
  .
  .
  
	
 
  delay(10000); // Let sensors read values
  Serial.println("Read Sensors...");
  
  .
  .
  .
  
    delay(10000);
       .
       .
       .
                      // Contact IFTTT
    delay(10000);

	.
	.
	.

System.sleep(SLEEP_MODE_DEEP,3600); // Sleep for one hour
  // *********************************
	.
	.
	.
	// Start process all over

else 
{

 **System.sleep(SLEEP_MODE_DEEP,XXX); // I would Like XXX to be a sleep until and wakeup at the start time of 13**

}//time loop

```

---

<div class="post-metadata">

**Author:** ![Moors7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/moors7/32/588_2.png) [@Moors7](https://community.particle.io/u/Moors7)\
**Post date:** [January 2, 2016, 2:24pm UTC](https://community.particle.io/t/have-system-sleep-sleep-mode-deep-wakeup-at-the-same-time-everday/18720/2 "2016-01-02T14:24:16Z")

</div>

A quick solution might be to do `24*3600-(combined delay time)` then it should wake at the same time. Alternatively, you could calculate the time difference between the current time and the time of the next wakeup, and use that to set the sleep time.

---

<div class="post-metadata">

**Author:** ![autolib](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@autolib](https://community.particle.io/u/autolib)\
**Post date:** [January 2, 2016, 2:35pm UTC](https://community.particle.io/t/have-system-sleep-sleep-mode-deep-wakeup-at-the-same-time-everday/18720/3 "2016-01-02T14:35:47Z")

</div>

Thanks. I will give both options a try.

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [January 2, 2016, 2:56pm UTC](https://community.particle.io/t/have-system-sleep-sleep-mode-deep-wakeup-at-the-same-time-everday/18720/4 "2016-01-02T14:56:00Z")

</div>

I’d definetly go with @Moors7’s aproach as this is the easiest and already for everybody doable way, with enough precision.

But just for completeness, if you had a battery backup for the RTC there would even be a wake-on-RTC possible for the STM32 µC, but it’s not (yet) baked into a nice and easy API.

---

<div class="post-metadata">

**Author:** ![mdma](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/mdma/32/1924_2.png) [@mdma](https://community.particle.io/u/mdma)\
**Post date:** [January 3, 2016, 1:55am UTC](https://community.particle.io/t/have-system-sleep-sleep-mode-deep-wakeup-at-the-same-time-everday/18720/5 "2016-01-03T01:55:00Z")

</div>

The wake on RTC is how the sleep for X seconds is implemented. For now, calculating the difference between your desired wake up time and the current time is the simplest approach.

In future, we might have an API like:

```auto
System.sleepUntil(time);

```

But this would just be syntactic sugar for calling System.sleep() with a duration.

---

<div class="post-metadata">

**Author:** ![autolib](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@autolib](https://community.particle.io/u/autolib)\
**Post date:** [January 3, 2016, 2:17pm UTC](https://community.particle.io/t/have-system-sleep-sleep-mode-deep-wakeup-at-the-same-time-everday/18720/6 "2016-01-03T14:17:29Z")

</div>

Thanks for all of the input.

How can I determine the integer value for a particular hour. For example in the documentation:  
// Print the hour for the given time, in this case: 4  
Serial.println(Time.hour(1400647897));

What would the integer value be for hour 13 or 14? What is the formula to determine the integer value of a particular hour?

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [January 3, 2016, 2:56pm UTC](https://community.particle.io/t/have-system-sleep-sleep-mode-deep-wakeup-at-the-same-time-everday/18720/7 "2016-01-03T14:56:06Z")

</div>

The _“UNIX epoch time”_ used here is a count of seconds since midnight 1/1/1970 (with no account for leap seconds).

There are some C functions (like [`localtime`](http://www.cplusplus.com/reference/ctime/localtime/) and its relatives) you could use, or you do the relative maths with the functions you get with the `Time` library (e.g. `(Time.now()) - (Time.now() % 86400) + (86400) + (14*3600)` = “current second” - “seconds since midnight” + “seconds of **one** day” + “seconds of fourteen hours”)

If you want to wake at a given time (hh:mm:ss) on the _following_ day, you would do something like this

```cpp
  // ( till midnight ) and ( another x seconds ) 
  int secs2sleep = (86400 - Time.now() % 86400) + (hh * 3600 + mm * 60 + ss); 
  System.sleep(SLEEP_MODE_DEEP, secs2sleep);

```

---

<div class="post-metadata">

**Author:** ![autolib](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@autolib](https://community.particle.io/u/autolib)\
**Post date:** [January 3, 2016, 3:10pm UTC](https://community.particle.io/t/have-system-sleep-sleep-mode-deep-wakeup-at-the-same-time-everday/18720/8 "2016-01-03T15:10:56Z")

</div>

Thanks.

---

<div class="post-metadata">

**Author:** ![LukeUSMC](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/lukeusmc/32/6519_2.png) [@LukeUSMC](https://community.particle.io/u/LukeUSMC)\
**Post date:** [January 3, 2016, 3:25pm UTC](https://community.particle.io/t/have-system-sleep-sleep-mode-deep-wakeup-at-the-same-time-everday/18720/9 "2016-01-03T15:25:31Z")

</div>

I would use UTC or Epoch which is seconds based and while the numbers can be large the math is simple. Here is some untested code similar to something I did in one of my apps. I am sure there are more elegant ways but hopefully this helps.

```cpp
uint32_t wakeUpUTC;
uint32_t wakeUpSecs;

//When you first enter your testing period at 1300
void setWakeup(){
wakeUpUTC = Time.now() + 24*3600;
}

//When you exit the last check at 2300
void calcWakeupSecs(){
wakeUpSecs = wakeUpUTC - Time.now();
}

else 
{

 System.sleep(SLEEP_MODE_DEEP,wakeUpSecs); // I would Like XXX to be a sleep until and wakeup at the start time of 13**

}//time loop

```

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [January 3, 2016, 3:30pm UTC](https://community.particle.io/t/have-system-sleep-sleep-mode-deep-wakeup-at-the-same-time-everday/18720/10 "2016-01-03T15:30:21Z")

</div>

You won’t need `long long` (`int64_t`) - at least not till 18/1/2038 😉

---

<div class="post-metadata">

**Author:** ![LukeUSMC](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/lukeusmc/32/6519_2.png) [@LukeUSMC](https://community.particle.io/u/LukeUSMC)\
**Post date:** [January 3, 2016, 3:31pm UTC](https://community.particle.io/t/have-system-sleep-sleep-mode-deep-wakeup-at-the-same-time-everday/18720/11 "2016-01-03T15:31:43Z")

</div>

Thanks…corrected.

---

<div class="post-metadata">

**Author:** ![autolib](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@autolib](https://community.particle.io/u/autolib)\
**Post date:** [January 5, 2016, 1:52pm UTC](https://community.particle.io/t/have-system-sleep-sleep-mode-deep-wakeup-at-the-same-time-everday/18720/12 "2016-01-05T13:52:14Z")

</div>

Hi @ScruffR ,

I have tried the following code segment the last two days but your suggestion:

```auto
// ( till midnight ) and ( another x seconds ) 
  int secs2sleep = (86400 - Time.now() % 86400) + (hh * 3600 + mm * 60 + ss); 
  System.sleep(SLEEP_MODE_DEEP, secs2sleep);

```

is not working.

Here is what I have:

```auto
int secs2sleep = 0;

//--------------------------------
void setup() {
.
.
.
}

void loop(){

if ((Time.hour() >= 13) && (Time.hour() <= 23) && (Time.weekday() != 1 ) 

// read sensors
.
.
.

System.sleep(SLEEP_MODE_DEEP,3600); //3600 1 hour
else if (Time.weekday() != 1) 
{

 //calcWakeupSecs();

 secs2sleep = (86400 - Time.now() % 86400) + (12 * 3600 + 00 * 60 + 00); 
 
 Particle.publish("After Hours - Store Closed", String(Time.second(secs2sleep)));
 
 System.sleep(SLEEP_MODE_DEEP, secs2sleep);

}//time loop
.
.
.
}

```

I am getting an erroneous result and consequently the Photon is not waking up at 13 (8:00 am local time).  
Last night secs2sleep returned the following:

 ![](https://us1.discourse-cdn.com/flex026/uploads/particle/original/2X/7/7978688e279a8d354d289ff65a34cdda8216b05a.png)/

The previous night I did not use “String(Time.second” and the result was a large number of seconds which when divided by 60 multiple times came out to the number 35.

What I am I doing wrong and what needs to be done to return the the correct number of sleeps seconds to wakeup at 13?

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [January 5, 2016, 2:04pm UTC](https://community.particle.io/t/have-system-sleep-sleep-mode-deep-wakeup-at-the-same-time-everday/18720/13 "2016-01-05T14:04:30Z")

</div>

I’m not sure what time zone you are in, but while `Time.hh()` returns the hour of your local zone set in `setup()` `Time.now()` returns the epoch time relative to UTC/GMT (zone 0).  
And the whole setup only works _after_ your RTC has at least synced once before.

---

<div class="post-metadata">

**Author:** ![autolib](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@autolib](https://community.particle.io/u/autolib)\
**Post date:** [January 5, 2016, 2:54pm UTC](https://community.particle.io/t/have-system-sleep-sleep-mode-deep-wakeup-at-the-same-time-everday/18720/14 "2016-01-05T14:54:46Z")

</div>

> [@ScruffR](#):
>
> RTC

I am in the Eastern time zone - US. I assume since the original setup of the Photon that RTC has at least synced once. How can I force a sync?  
This morning when I removed power to the Photon, after it never woke up, the code started executing where it recognized that the time was \>= 13. What do suggest I do?

---

<div class="post-metadata">

**Author:** ![autolib](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@autolib](https://community.particle.io/u/autolib)\
**Post date:** [January 5, 2016, 8:29pm UTC](https://community.particle.io/t/have-system-sleep-sleep-mode-deep-wakeup-at-the-same-time-everday/18720/15 "2016-01-05T20:29:33Z")

</div>

@ScruffR I think I got it to work in trial runs. I will know for sure tomorrow morning if it wakes up at the proper time in an actual run.  
Thanks for all of your help and insight.

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [January 5, 2016, 9:14pm UTC](https://community.particle.io/t/have-system-sleep-sleep-mode-deep-wakeup-at-the-same-time-everday/18720/16 "2016-01-05T21:14:44Z")

</div>

Good to hear you’re getting somewhere 👍

Just one not on the code you’ve given above

```cpp
  Particle.publish("After Hours - Store Closed", String(Time.second(secs2sleep));

```

If you wanted to know how many seconds your device will sleep, you’d rather write

```cpp
  Particle.publish("After Hours - Store Closed", String(secs2sleep));

```

The way you did it you’d just get the seconds part of the time that is `secs2sleep`seconds past midnight 1/1/1970 which would be the same as `secs2sleep % 60`.

If you wanted to publish the time string your device will wake you’d something like this

```cpp
float timeZone = +5.0; // your local time offset (without DST)
void setup()
{
  ...
  Time.zone(timeZone); 
  if (Time.year() <= 1970) Particle.syncTime();
}

void loop()
{
  uint32_t _now = Time.now();
  // ( till midnight UTC ) and ( time to wake but UTC ) and ( make local )
  int _secs2sleep = (86400 - _now % 86400) + (12 * 3600 + 00 * 60 + 00) + (timeZone * 3600); 
  
  Particle.publish("After Hours - Store Closed", Time.format(_now + _secs2sleep));
 
  System.sleep(SLEEP_MODE_DEEP, _secs2sleep);
}

```

---

<div class="post-metadata">

**Author:** ![autolib](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@autolib](https://community.particle.io/u/autolib)\
**Post date:** [January 5, 2016, 10:36pm UTC](https://community.particle.io/t/have-system-sleep-sleep-mode-deep-wakeup-at-the-same-time-everday/18720/17 "2016-01-05T22:36:22Z")

</div>

Thanks for the update.

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [January 6, 2016, 6:16am UTC](https://community.particle.io/t/have-system-sleep-sleep-mode-deep-wakeup-at-the-same-time-everday/18720/18 "2016-01-06T06:16:08Z")

</div>

One thing to be aware of.  
Since the “midnight finding” is done on the UTC side, you might find that the above will not wake on the first day if you do this after midnight UTC (so after 7pm your time).  
To deal with that you’d have to add the time zone correction to `_now` but might have to correct other bits of above code accordingly.

---

<div class="post-metadata">

**Author:** ![autolib](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@autolib](https://community.particle.io/u/autolib)\
**Post date:** [January 6, 2016, 3:09pm UTC](https://community.particle.io/t/have-system-sleep-sleep-mode-deep-wakeup-at-the-same-time-everday/18720/19 "2016-01-06T15:09:27Z")

</div>

Yes I started getting strange results after 7:00pm local time. Earlier in the day when I said I was close I was using your earlier code:

_secs2sleep = (86400 - Time.now() % 86400) + (12 \* 3600 + 00 \* 60 + 00);_  
_Particle.publish(“After Hours - Store Closed”, String(Time.second(secs2sleep)));_  
_System.sleep(SLEEP\_MODE\_DEEP, secs2sleep);_

and I had everything theoretically working ok where the seconds returned corresponded to a store opening at the right time the next day.

When I added the new code you suggested later in the day where the time zone was set:

_float timeZone = +5.0; // your local time offset (without DST)_  
_void setup()_  
_{_  
_…_  
Time.zone(timeZone);  
_if (Time.year() \<= 1970) Particle.syncTime();_  
_}_  
.  
.  
.

I got really strange results in seconds returned. Results of a store opening a few days in the future were returned!

 ![](https://us1.discourse-cdn.com/flex026/uploads/particle/original/2X/9/997c0137424debb3434aff37e62030b7b97908df.png)

I am going play with it more today and see if I can something going.

Again thanks for all of your input.

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [January 6, 2016, 3:25pm UTC](https://community.particle.io/t/have-system-sleep-sleep-mode-deep-wakeup-at-the-same-time-everday/18720/20 "2016-01-06T15:25:49Z")

</div>

@autolib, timeZone should be an INT, not a float 😉

[Next page](https://community.particle.io/t/have-system-sleep-sleep-mode-deep-wakeup-at-the-same-time-everday/18720.md?page=2)
