Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    Interrupt and write to file

    Scheduled Pinned Locked Moved MicroPython
    interruptwrite to filepytrackmicropysleep
    4 Posts 3 Posters 1.1k Views 3 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • A Offline
      alexpul
      last edited by

      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!

      1 Reply Last reply Reply Quote 0
      • S Offline
        Sympatron
        last edited by

        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)
        
        J 1 Reply Last reply Reply Quote 0
        • J Offline
          jcaron @Sympatron
          last edited by

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

          A 1 Reply Last reply Reply Quote 0
          • A Offline
            alexpul @jcaron
            last edited by alexpul

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

            1 Reply Last reply Reply Quote 0

            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
            • 1 / 1
            • First post
              Last post

            Pycom on Twitter

            Pycom