Skip to content

Instantly share code, notes, and snippets.

@vermiculus
Last active January 3, 2016 04:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vermiculus/8411540 to your computer and use it in GitHub Desktop.
Save vermiculus/8411540 to your computer and use it in GitHub Desktop.
2014-01-14 05:20:40,950 INFO: Starting monitoring system
2014-01-14 05:20:50,169 INFO: The system has been activated.
2014-01-14 05:20:50,174 INFO: Motion detection will begin in ten minutes.
2014-01-14 05:21:00,187 INFO: Motion detection active.
2014-01-14 05:21:07,140 INFO: Detected motion.
2014-01-14 05:21:07,145 INFO: The recording "record4snort1.wav" will play in twenty seconds.
2014-01-14 05:21:27,170 INFO: Playing "record4snort1.wav"...
2014-01-14 05:21:37,064 INFO: Playing "record4snort1.wav"... Done.
2014-01-14 05:22:09,609 INFO: Detected motion.
2014-01-14 05:22:09,613 INFO: The recording "recorded snort1.wav" will play in twenty seconds.
2014-01-14 05:22:29,638 INFO: Playing "recorded snort1.wav"...
2014-01-14 05:22:37,680 INFO: Playing "recorded snort1.wav"... Done.
2014-01-14 05:23:37,636 INFO: Removing callbacks...
2014-01-14 05:23:37,640 INFO: Exiting
#!/usr/bin/sudo python3.2
pin_pir = 12
pin_led = 11
pin_btn_nc = 15
pin_btn_no = 13
system_active = False
snorts = {'record2snort2.wav': 'some criterion',
'record3snort1.wav': 'some criterion',
'record3snort2.wav': 'some criterion',
'record4snort1.wav': 'some criterion',
'record4snort2.wav': 'some criterion',
'recorded snort1-3.wav': 'some criterion',
'recorded snort1.wav': 'some criterion',
'recorded snort2.wav': 'some criterion'}
import random
import signal
import time
import subprocess
import logging
logging.basicConfig(
filename='/home/pi/log',
format='%(asctime)s %(levelname)8s: %(message)s'
)
console = logging.StreamHandler()
console.setLevel(logging.INFO)
console.setFormatter(logging.Formatter('%(asctime)s %(levelname)-8s: %(message)s'))
log = logging.getLogger('')
log.addHandler(console)
log.setLevel(logging.INFO)
import RPIO
RPIO.setmode(RPIO.BOARD)
pir_interrupt_set = False
def blink(n, pause=.1):
for i in range(2*n):
RPIO.output(pin_led, not RPIO.input(pin_led))
time.sleep(pause)
def toggle_system_active(pin, val):
global system_active
system_active = not system_active
RPIO.output(pin_led, system_active)
if system_active:
log.info('The system has been activated.')
else:
log.info('The system has been deactivated.')
if sound['proc'] is not None and sound['proc'].poll() is None:
log.debug('Killing sound')
sound['proc'].kill()
sound['proc'] = None
sound['file'] = None
sound['time'] = None
sound = {'proc': None,
'file': None,
'time': None}
def play_random_sound(pin, val):
if system_active:
if sound['time'] is None:
log.info('Detected motion.')
blink(3)
sound['file'] = random.choice([key for key in snorts if snorts[key]])
sound['time'] = time.time()
sound['proc'] = None
log.info('The recording "%s" will play in twenty seconds.', sound['file'])
log.debug('Setting up LED on pin %d', pin_led)
RPIO.setup(pin_led, RPIO.OUT, initial=system_active)
log.debug('Setting up button (NC) on pin %d', pin_btn_nc)
RPIO.setup(pin_btn_nc, RPIO.IN, pull_up_down=RPIO.PUD_DOWN)
try:
log.info('Starting monitoring system')
log.info('Please note that timestamps are in universal coordinated time.')
blink(5)
log.debug('Adding callback for the on/off button')
RPIO.add_interrupt_callback(pin_btn_nc,
toggle_system_active,
edge='rising',
pull_up_down=RPIO.PUD_DOWN)
RPIO.wait_for_interrupts(threaded=True)
while True:
time.sleep(1)
if system_active and not pir_interrupt_set:
pir_interrupt_set = True
log.debug('Adding callback for the motion sensor')
RPIO.add_interrupt_callback(pin_pir,
play_random_sound,
pull_up_down=RPIO.PUD_DOWN,
edge='rising',
debounce_timeout_ms=60000)
log.info('Motion detection active.')
if not system_active and pir_interrupt_set:
pir_interrupt_set = False
log.debug('Removing callback for the motion sensor')
RPIO.del_interrupt_callback(pin_pir)
log.info('Motion detection inactive.')
if system_active and sound['time'] is not None:
delta = time.time() - sound['time']
if delta > 60:
sound['time'] = None
sound['file'] = None
if sound['proc'] is not None and sound['proc'].poll() is None:
sound['proc'].kill()
sound['proc'] = None
log.debug('Killed process')
elif delta > 40:
if sound['proc'] is not None and sound['proc'].poll() is None:
sound['proc'].terminate()
sound['proc'] = None
blink(2)
log.debug('Terminated process')
elif delta > 20 and sound['proc'] is None:
log.info('Playing "%s".', sound['file'])
sound['proc'] = subprocess.Popen(['mplayer', '-msglevel',
'all=-1', '/home/pi/snort/sounds/recordings/{!s}'\
.format(sound['file'])])
elif system_active:
log.debug('Ready for new motion')
except KeyboardInterrupt:
log.debug('Caught C-c')
finally:
log.info('Removing callbacks...')
RPIO.del_interrupt_callback(pin_btn_nc)
if system_active:
RPIO.del_interrupt_callback(pin_pir)
log.info('Exiting')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment