Interrupt and write to file
-
I'm using a Pytrack and want to set the accelerometer to wake up (interrupt) the board from sleep and then write the current time to a file located on either the flash or SD. I have an example of a basic accelerometer interrupt:
from pytrack import Pytrack from LIS2HH12 import LIS2HH12 py = Pytrack() py.setup_int_pin_wake_up(False) py.setup_int_wake_up(True, True) acc = LIS2HH12() acc.enable_activity_interrupt(2000, 200)and an example of how to write the time to a file on a SD card:
from pytrack import Pytrack import time import machine import pycom import os from machine import SD import os import uos ā ## Mount SD card sd = SD() #create SD card object os.mount(sd, '/sd') #mount object ## Initialize time rtc = machine.RTC() rtc.init((2018, 8, 27, 2, 10, 30, 0, 0)) ## Write time to file f = open('/sd/test.txt', 'a') #create file (append mode) f.write("{}\n".format(rtc.now())) f.close() uos.unmount('/sd')but I don't know how to combine the two actions. Bonus would be to also turn the LED on when an interrupt happens. Any pointers, links or working code would be greatly appreciated!
-
You need to pass a handler to
enable_activity_interrupt()like so:def my_activity_handler(pin_o): # your code saving to file here acc = LIS2HH12() acc.enable_activity_interrupt(2000, 200, handler=my_activity_handler) -
@sympatron That handler is only for interrupts received while the module is awake.
@alexpul In the first code snippet, you don't actually go to deep sleep. What module do you have (WiPy 2, WiPy 3, LoPy, LoPy 4, FiPy, GPy...)? This will decide whether you should use "native" deep sleep or Pytrack-controller deep sleep.
-
@jcaron I have a WiPy 3. What do you recommend using for deep sleep?
And for the handler interrupt, do you know how to integrate it while the device is sleeping?
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better š
Register Login