class InactivitySensor(PeriodicSensor): def __init__(self, queue, period, wait_before_sleep=WAIT_BEFORE_SLEEP, time_to_sleep=TIME_TO_SLEEP): self._wait_before_sleep = wait_before_sleep self._time_to_sleep = time_to_sleep self._queue = queue self._py = Pytrack() message = "Wake up reason: " + str(self._py.get_wake_reason()) # display the reset reason code and the sleep remaining in seconds # possible values of wakeup reason are: # WAKE_REASON_ACCELEROMETER = 1 # WAKE_REASON_PUSH_BUTTON = 2 # WAKE_REASON_TIMER = 4 # WAKE_REASON_INT_PIN = 8 self.printout(message) time.sleep(0.5) # enable wakeup source from INT pin self._py.setup_int_pin_wake_up(False) self.value = 0 # enable activity and also inactivity interrupts, using the default callback handler self._py.setup_int_wake_up(True, True) PeriodicSensor.__init__(self, queue, period) def read(self): self.value = self._queue.get_timeout() if self.value > self._wait_before_sleep: self._sleep() def _sleep(self): self.printout('Sleeping') EventSensor._event_handler(self) #self._queue._client.publish(self._queue._topic, 'Sleeping') self._py.setup_sleep(self._time_to_sleep) self._py.go_to_sleep() pass
# display the reset reason code and the sleep remaining in seconds # possible values of wakeup reason are: # WAKE_REASON_ACCELEROMETER = 1 # WAKE_REASON_PUSH_BUTTON = 2 # WAKE_REASON_TIMER = 4 # WAKE_REASON_INT_PIN = 8 print("Wakeup reason: " + str(py.get_wake_reason()) + "; Aproximate sleep remaining: " + str(py.get_sleep_remaining()) + " sec") time.sleep(0.5) # enable wakeup source from INT pin py.setup_int_pin_wake_up(False) # enable activity and also inactivity interrupts, using the default callback handler py.setup_int_wake_up(True, True) acc = LIS2HH12() # enable the activity/inactivity interrupts # set the accelereation threshold to 2000mG (2G) and the min duration to 200ms acc.enable_activity_interrupt(2000, 200) # check if we were awaken due to activity if acc.activity(): pycom.rgbled(0xFF0000) else: pycom.rgbled(0x00FF00) # timer wake-up Green time.sleep(2) pycom.rgbled(0x000000) time.sleep(1) pycom.rgbled(0xFF0000) # Red
from pytrack import Pytrack from LIS2HH12 import LIS2HH12 #from deepsleep import DeepSleep import deepsleep ACCEL_TRESHOLD = 2000 # shake deteted when acceleration is more than 0.001xG ACCEL_DURATION = 200 # shake duration is more than 200ms ds = deepsleep.DeepSleep() py = Pytrack() py.setup_int_wake_up(True, False) acc = LIS2HH12() def handler(pin_o): print('Acc handler event', pin_o()) acc.enable_activity_interrupt(ACCEL_TRESHOLD, ACCEL_DURATION, handler=handler) # get the wake reason and the value of the pins during wake up wake_s = ds.get_wake_status() print(wake_s) if wake_s['wake'] == deepsleep.PIN_WAKE: print("Pin wake up") elif wake_s['wake'] == deepsleep.TIMER_WAKE: print("Timer wake up") else: # deepsleep.POWER_ON_WAKE: print("Power ON reset")
from pytrack import Pytrack #from pysense import Pysense from LIS2HH12 import LIS2HH12 import pycom import time pycom.heartbeat(False) py = Pytrack() # py = Pysense() # enable activity and also inactivity interrupts, using the default callback handler py.setup_int_wake_up(True, True) acc = LIS2HH12() # enable the activity/inactivity interrupts # set the accelereation threshold to 2000mG (2G) and the min duration to 200ms acc.enable_activity_interrupt(2000, 200) # check if we were awaken due to activity if acc.activity(): pycom.rgbled(0xFF0000) else: pycom.rgbled(0x00FF00) # timer wake-up time.sleep(0.1) # go to sleep for 5 minutes maximum if no accelerometer interrupt happens py.setup_sleep(300) py.go_to_sleep()