Skip to main content

Wake An imp From Deep Sleep

Learn How To Make Use Of The imp’s Wake Pin Facility

All imps have at least one pin which can be used to wake a module from the Deep Sleep state. Check out the pin mux table for the imp you’re using to find out which one it is — it’s usually pin W:

imp imp001 imp002 imp003 imp004m imp005 imp006 impC001
Wake Pin(s) 1 1 W W N/A V, W W

The Wake pin is not enabled by default. Instead you use the imp API method hardware.pin.configure() to set the pin up for waking. This method must be called before the imp goes into Deep Sleep, like this:

hardware.pinW.configure(DIGITAL_IN_WAKEUP, onPinStateChangeCallback);

The onPinStateChangeCallback parameter takes a reference to a function that may be triggered when the wake pin is asserted. Waking the imp from deep sleep restarts the Squirrel virtual machine afresh, so the registered callback will not be called. However, the callback will be called if the pin is triggered while the imp is awake.

Once the Wake pin configured, an imp in Deep Sleep will be woken by applying logic high (3V3) to the pin.

You should bear in mind a couple of points when working with an imp’s Wake pin:

  • Squirrel wakes into an offline state. If the imp is in RETURN_ON_ERROR mode, only an explicit call to server.connect() or server.connectwith() will cause it to reconnect, if it can. In SUSPEND_ON_ERROR mode, the default, any attempted network activity (such as a call to agent.send() or server.log()) will cause it to attempt to connect. You set an imp’s mode using server.setsendtimeoutpolicy()
  • The imp005 does not support Deep Sleep or offer a Wake pin.

Sample Code

The following example code can be run on any imp — we recommend using a breakout board. On power up, it will report its reason for waking:

2020-10-07 13:37:03.961 +01:00: [Server]    Device connected
2020-10-07 13:37:04.112 +01:00: [Device]    imp woken (but not by wake pin)

The app will now configure the Wake pin and then go into Deep Sleep for 30 minutes. It will restart automatically after that period, but in the meantime you'll see log output like:

2020-10-07 13:37:43.119 +01:00: [Device]    Sleeping until 2020-10-07 13:07:42Z
2020-10-07 13:37:43.119 +01:00: [Server]    Device disconnected

To wake the imp up, just bridge your board’s 3V3 pin and the Wake pin. The imp will wake and report the fact:

2020-10-07 13:37:03.961 +01:00: [Server]    Device connected
2020-10-07 13:37:04.112 +01:00: [Device]    imp woken by wake pin

Remove the wire between the 3V3 and Wake quickly: if it’s still connected when the imp sleeps again, the imp will wake immediately.

The Code

API Methods Used

Further Reading