Wake machine.deepsleep using pin input from accelerometer [SOLVED]
-
I'm trying to create an accelerometer interrupt for
machine.deepsleep(). From what I can tell there is no built-in function for that. Since deep sleep can be interrupted with a pin I was thinking that there might be a pin that allows the accelerometer to relay an interrupt to the device.I've tried P13, which is denoted as "Accelerometer Interrupt" on the Pytrack pinout but I didn't get a response (even when the device was not asleep). Here is the code I was using
from machine import Pin ā def pin_handler(arg): print("got an interrupt in pin %s" % (arg.id())) ā p_in = Pin('P13', mode=Pin.IN, pull=Pin.PULL_UP) p_in.callback(Pin.IRQ_FALLING, pin_handler)Any ideas or suggestions would be great!
My system specs:
(sysname='WiPy', nodename='WiPy', release='1.18.0.r1', version='v1.8.6-849-9569a73 on 2018-07-20', machine='WiPy with ESP32', pybytes='0.9.0')
-
Ah ha. I was using the wrong interrupt script.
This is the correct working code for an accelerometer interrupt (P13) of
machine.deepsleep():import machine machine.pin_deepsleep_wakeup(['P13'], machine.WAKEUP_ANY_HIGH, False) machine.deepsleep(10*1000)So for pin interrupts there is a
callback()function to create interrupts of active and sleep modes but thepin_deepsleep_wakeup()is needed to interrupt deep sleep (is that what you meant by "wake-on-pin" @jcaron?). There is no interrupt handler forpin_deepsleep_wakeup()because the working memory is cleared after deep sleep (as mentioned by @jcaron below).@jcaron I had tested my interrupt handler without sleep and it did work. In my
main.pyI had the following script to test that it woke from deep sleep and ranmain.py.import pycom import utime pycom.heartbeat(False) #turn off LED pycom.rgbled(0xFF0000) #red LED utime.sleep(2) pycom.heartbeat(False) #turn off LED
-
@alexpul In that code you don't enable wake-on-pin, and you don't do anything visible before going back to sleep, so even if it wakes up you won't see anything...
Note that as far as I know, the handler will only be called if the interrupt happens while the device is awake, not during sleep.
Start by using
acc.enable_activity_interruptwithout any deep sleep to see if the interrupt gets triggered (i.e. just remove themachine.deepsleepin your code).Once you have confirmed that, you can add the wake-on-pin, and do something visible when you wake up before going back to sleep.
-
@jcaron This is the code I've tried to test if the accelerometer can wake from
machine.deepsleep()from LIS2HH12 import LIS2HH12 import pycom import machine import utime pycom.heartbeat(False) def accel_activity_handler(pin_o): pycom.rgbled(0x800080) utime.sleep_ms(200) pycom.heartbeat(False) ## ACCELEROMETER INTERRUPT acc = LIS2HH12() acc.enable_activity_interrupt(1000, 200, handler=accel_activity_handler) machine.deepsleep(100*1000)When I shake the device nothing happens...
(Note that this script does allow for a deep sleep interrupt but only when
pytrack.go_to_sleep()is used to enter deep sleep. I need to usemachine.deepsleep()though..)
-
@alexpul Did you configure the accelerometer (enable it, set it to monitor events and send interrupts)?
You need to at least instantiate the
LIS2HH12class, and call theenable_activity_interruptmethod.There may be more stuff to do. Check the sources of the PyTrack library for more information.