Puts the imp into a deep sleep state for the specified duration
Device
Name | Type | Description |
---|---|---|
sleepTime | Integer |
The deep sleep duration in seconds (0—2,419,198)
|
Nothing — does not return
This method causes the imp to enter a very low power mode (‘deep sleep’) and stop executing code for the specified period of time — or until the wake-up pin is triggered, if it has been activated. While the imp is in deep sleep, its network interfaces are disabled.
Note This method is very similar in operation to imp.deepsleepfor(). However, unlike imp.deepsleepfor(), sever.sleepfor() 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.
The maximum sleep time is 2,419,198 seconds (28 days minus two seconds).
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.sleepfor() only when the imp is idle, ie. when impOS is ready for this to take place. This is best achieved by incorporating your server.sleepfor() 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.sleepfor(3600);
});
For example, this snippet of code can be used to wake up every hour, on the hour:
imp.onidle(function(){
server.sleepfor(3600 - (time() % 3600));
});
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.
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 ("sleepfor" in server) {
// imp has a 32kHz xtal
}
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 Design Hardware With The imp004m for workarounds.
imp005 devices are not currently capable of deep sleep, so server.sleepfor() has been removed from the imp005. Calling this method on an imp005 will causes an exception to be thrown.
This example demonstrates how to get the imp to wake up once an hour, log some data (in this case, voltage), store it in its non-volatile memory, then go back to sleep.