Skip to main content

imp.deepsleepfor(sleepTime)

Puts the imp into deep sleep for the specified period

Availability

Device

Parameters

Name Type Description
sleepTime Integer The deep sleep duration in seconds (0—2419198)

Returns

Nothing

Description

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, the current network interface is disabled.

Note This method is very similar in operation to server.sleepfor(). However, unlike server.sleepfor(), imp.deepsleepfor() does not perform 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, its device code is loaded from its 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 your imp’s 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 imp.deepsleepfor() only when the imp is idle, ie. when impOS is ready for this to take place. This is best achieved by incorporating your imp.deepsleepfor() code inside a function that is registered to called when the imp goes idle. To do so, use the method imp.onidle():

imp.onidle(function(){
    imp.deepsleepfor(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.

Module-specific Information

imp003

This method is not available on imp003-based devices with no 32kHz crystal.

imp004m

This method is not available on imp004m-based devices with no 32kHz crystal.

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

imp005

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

Example Code

This example wakes the imp up for about 0.05 seconds each minute to do some data logging. Once an hour it also fires up WiFi and dumps that hour’s readings to the server. Once DHCP and so on are taken into account, the hourly wake-up can last for several seconds. This technique enables the imp to use only tiny amounts of power overall which is useful when running from a battery.

// Process incoming data from the device. In this instance,
// we just log the received data
device.on("hourlydata", function(data) {
// Log the data
server.log(data);
});
// Log an ADC value each minute, only tell the server once an hour
// NOTE This example is written for the imp001, but can be easily
// modified for any other imp (but not the imp005, which does
// not support deep sleep)
local t = time();
function sendDataHourly() {
// Every hour relay stored data to the agent
agent.send("hourlydata", nv.data);
nv.data = "";
}
function readData() {
// Configure the imp001's Pin 9 for analog input
// NOTE Change the pin if you are using a different imp
hardware.pin9.configure(ANALOG_IN);
// Add the value read on the pin to the data store
nv.data = nv.data + format(" %d", hardware.pin9.read());
// Is it time to contact the server?
// If we're at minute 59, call 'sendDataHourly()' to send the data
local min = (t / 60) % 60;
if (min == 59) sendDataHourly();
// Now put the imp to sleep when impOS is ready
imp.onidle(function() {
// imp.deepsleepfor() does not flush the server buffer
server.flush(10);
imp.deepsleepfor(60 - (time() % 60));
});
}
// Set up the data store in 'nv' if it has not yet been created
if (!("nv" in getroottable())) nv <- { data = "" };
// At a cold or warm boot, go and get data
readData();