Get you a Pi, quantify your vermin.

sensor_test.py 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/usr/bin/env python
  2. import time
  3. import RPi.GPIO as io
  4. def send_value(field, value):
  5. print(field, value)
  6. io.setmode(io.BCM)
  7. pir_pin = 18
  8. door_pin = 23
  9. # Activate input:
  10. io.setup(pir_pin, io.IN)
  11. # Activate input with PullUp:
  12. io.setup(door_pin, io.IN, pull_up_down=io.PUD_UP)
  13. trap_closed = False
  14. motion_detected = False
  15. motion_instances = []
  16. recent_problem_count = 0
  17. while True:
  18. previous_trap_value = trap_closed
  19. previous_recent_problem_count = recent_problem_count
  20. motion_detected = False
  21. trap_closed = False
  22. if io.input(pir_pin):
  23. motion_detected = True
  24. print "MOTION DETECTED!"
  25. if motion_detected:
  26. motion_instances.append(1)
  27. else:
  28. motion_instances.append(0)
  29. if len(motion_instances) > 6:
  30. motion_instances.pop(0)
  31. if io.input(door_pin):
  32. trap_closed = False
  33. else:
  34. trap_closed = True
  35. if (previous_trap_value != trap_closed):
  36. if trap_closed:
  37. send_value('Trap State', 'Trap Closed')
  38. else:
  39. send_value('Trap State', 'Trap Open')
  40. recent_problem_count = sum(motion_instances)
  41. if previous_recent_problem_count != recent_problem_count:
  42. send_value('Mouse Problems', sum(motion_instances))
  43. time.sleep(0.5)