I am trying to save power by putting the IMP into a low power state.
Can someone point me to a document that explains the difference between imp.sleep, imp.deepsleepfor, and server.sleepfor, imp.wakeup (xx), and others that I am sure I haven’t found yet…
I have been trying many of these out, with surprising results; some of these seem to be equivalent to a ‘coldboot’, while others continue the code from the statement position, some reset variables and others do not. Many of these do not help with power consumption at all.
The documentation doesn’t seem to detail all this in one place, which makes this topic pretty confusing. Is there a single table?
Did I miss something obvious? again? help please!
Not sure there’s a single document, because they’re all very different things.
imp.sleep() is a busy wait. The imp spins. No other code (including the network stack) runs. You should avoid using this for anything but very small delays (I’d avoid it for anything over about 10ms personally, but tastes vary)
Scheduling events to happen in the future:
imp.wakeup() registers a function to be called after a certain time has elapsed. You can have many of these registered at once. A wakeup cannot interrupt running squirrel code. The system manages itself into as low power a state as possible when waiting for an event (a wakeup, a GPIO change handler, data from server, etc) by idling the CPU, clock gating peripherals, dropping bus clocks, etc.
imp.deepsleepfor() puts the imp totally to sleep (microamp mode). When it wakes, code restarts from the top. The only thing preserved is the nv table. This does not cleanly close the connection, so is most often used when the wifi is not connected. Note that on wake, the wifi is not connected until you specifically request it (how you do this depends on your sendtimeout policy)
server.sleepfor() is like deepsleepfor but it cleanly closes the connection.
If you configure pin1/pinW for wakeup, then this can bring the imp out of sleep before the requested sleep time elapses.
You can use the sleepfor() to get years of battery life.
If you’re trying to just drop the power whilst still being connected to wifi, I’d suggest using imp.setpowersave(true); - this drops down to vaguely 1/10th of the normal operational power, with only a small impact on incoming message latency.
Technically, synchronous sleeps using imp.sleep() are a busy-wait only for short sleeps (<20ms). Longer sleeps are lower-power, although (in current releases) still not as low as asynchronous sleeps using imp.wakeup(). And as Hugo says, longer sleeps (certainly ones measured in seconds) are better done using imp.wakeup() anyway, as doing so keeps the code responsive to event-handlers, incoming network messages, etc.
For some uses (waiting for a peripheral to come ready, for instance), you might also care about how accurate the sleep duration is. And it turns out there’s another trade-off to be made here: short, synchronous sleeps are rounded up to microseconds, asynchronous sleeps to centiseconds, and deep sleeps (imp.deepsleepfor()) only to the next second. (In the latter two cases there is hardware room for improvement in the accuracy, which may be enabled in some future release.)
Thanks guys, this was a big help and put me in the right direction and I now have control of my bucking converter heat generation.
I was very surprised at the amount of current that imp.setpowersave saves. It is very significant. I will use it more regularly to control heat production.