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!



  • @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?



  • @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.



  • 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)
    


Pycom on Twitter