Skip to main content

server.sleepuntil(hour, minute, second, dayOfWeek)

Puts the imp into a deep sleep state until a specified time

Availability

Device

Parameters

Name Type Description
hour Integer The wake-up hour in 24-hour format (midnight = 0)
minute Integer The wake-up minute (0—60)
second Integer Optional, the wake-up second (0—60, default = 0)
dayOfWeek Constant Optional, the day of week on which to wake (0—6, default = any)

Returns

Nothing — does not return

Description

This method causes the imp to disconnect from the server, enter a very low power mode (‘deep sleep’) and stop executing code until the specified time of day, expressed in UTC using 24-hour notation. The imp will also wake, superseding this call, if the wake-up pin has been set and is subsequently triggered.

Note This method is very similar in operation to imp.deepsleepuntil(). However, unlike imp.deepsleepuntil(), sever.sleepuntil() performs a server.flush() operation to ensure that any pending messages are sent before sleep.

When the imp enters deep sleep, the execution of Squirrel code is stopped entirely. When the imp wakes up, your device code is loaded from the imp’s flash storage and restarted. No state data is preserved, with the exception of the special nv table, which will be maintained. The nv table is not preserved through a power-cycle.

In deep sleep, all of the imp’s pins are tri-stated, unless you have enabled its wake-up pin. The wake-up pin (and only the wake-up pin) has a weak pull-down.

Optionally, the day of the week can also be given, which means this function can cause the imp to go into deep sleep for up to a week. The imp API provides numeric constants for SUNDAY through to SATURDAY, enumerated from 0 to 6. These values are consistent with Squirrel’s date() function, which returns a table where the wday field is the number of days since Sunday. This means that no normalizations have to be done in the Squirrel code.

dayOfWeek Constant Value
SUNDAY 0
MONDAY 1
TUESDAY 2
WEDNESDAY 3
THURSDAY 4
FRIDAY 5
SATURDAY 6

If no day of the week is given — only three parameters are passed — then the wake-up matches any day of the week, ie. the disconnection can last a maximum of one day.

Deep sleep mode draws approximately 6μA of current on the imp001 card, or 4μA on imp modules (the imp005 does not support deep sleep). The processor is effectively shut down. Upon waking from deep sleep, the device will be in a ‘warm boot’ state with code execution returning to the start of the script. Booting and reconnecting to the network will take a second or more, so this method may not be appropriate for very short sleep periods.

It is recommended practice to call server.sleepuntil() only when the imp is idle, ie. when impOS is ready for this to take place. This is best achieved by incorporating your server.sleepuntil() code in a function that is registered to called when the imp goes idle. To do so, use the method imp.onidle():

imp.onidle(function(){
    server.sleepuntil(0, 0);
});

Note Using imp.wakeup() as an alternative to imp.onidle() is not recommended in this case, as any timeout provided can be no guarantee that the imp has become idle at that point. imp.onidle() is on the only way to be sure.

Module-specific Information

imp003

This method is not available on imp003-based devices with no 32kHz crystal and running impOS 34 and up. The method’s presence can be used to indicate whether or not a crystal has been detected:

if ("sleepuntil" in server) {
   // Device has a 32kHz xtal
}

imp004m

This method is not available on imp004m-based devices with no 32kHz crystal and running impOS 34 and up.

You will not be able to preserve data in the nv table during sleep: the imp004m does not implement nv table functionality. Please see Designing Hardware with the imp004m for workarounds.

imp005

imp005 devices are not currently capable of deep sleep, so server.sleepuntil() has been removed from the imp005. Calling this method on an imp005 will cause an exception to be thrown.

Example Code

This example demonstrates how to have your imp wake up every day and trigger some action (turn on lights, a coffee machine, etc.)