Get you a Pi, quantify your vermin.

mouse.py 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/env python
  2. import ConfigParser
  3. import os
  4. import sqlite3
  5. import time
  6. import RPi.GPIO as io
  7. from Adafruit_IO import *
  8. config = ConfigParser.ConfigParser()
  9. config.read('mousetrap.cfg')
  10. io_key = config.get('io', 'key')
  11. aio = Client(io_key)
  12. def send_value(field, value):
  13. print(field, value)
  14. aio.send(field, value)
  15. io.setmode(io.BCM)
  16. pir_pin = 18
  17. door_pin = 23
  18. # Activate input:
  19. io.setup(pir_pin, io.IN)
  20. # Activate input with PullUp:
  21. io.setup(door_pin, io.IN, pull_up_down=io.PUD_UP)
  22. trap_activity = 0
  23. previous_door_state = io.input(door_pin)
  24. motion_detected = False
  25. motion_instances = []
  26. recent_problem_count = 0
  27. while True:
  28. previous_trap_value = trap_activity
  29. previous_recent_problem_count = recent_problem_count
  30. motion_detected = False
  31. if io.input(pir_pin):
  32. motion_detected = True
  33. if motion_detected:
  34. motion_instances.append(1)
  35. else:
  36. motion_instances.append(0)
  37. if len(motion_instances) > 6:
  38. motion_instances.pop(0)
  39. if io.input(door_pin) != previous_door_state:
  40. previous_door_state = io.input(door_pin)
  41. trap_activity += 1
  42. if (previous_trap_value != trap_activity):
  43. send_value('Trap Activity', trap_activity)
  44. recent_problem_count = sum(motion_instances)
  45. if previous_recent_problem_count != recent_problem_count:
  46. send_value('Mouse Problems', sum(motion_instances))
  47. time.sleep(6)