Пример #1
0
def detect_and_record_motion():
    """
    Detects and records motion via interfacing with PIR sensor and camera.
    Saves the recording locally then triggers a function to upload/clean up
    """
    # Grab folder id from config file
    conf = configparser.ConfigParser()
    conf.read('config.ini')
    vid_pth = conf['tmp_dir']['path']
    drive_folder_id = conf['google_drive']['folder_id']

    # Create utilities instance
    drive_utils = DriveUtilities(drive_service, logger)

    # Motion sensor module connected to pin 4 (5v)
    pir = MotionSensor(4)
    camera = PiCamera()
    camera.resolution = (1280, 720)
    camera.vflip = True

    try:

        while True:

            # PIR sensor triggered, camera starts recording for a minimum of 10 seconds
            pir.wait_for_active()
            logger.info('>>>PIR sensor: Motion detected')
            triggered_datetime = datetime.datetime.today().strftime(
                '%Y-%m-%d_%H-%M-%S')
            file_path = vid_pth + triggered_datetime + '.h264'
            camera.start_recording(file_path)
            camera.annotate_background = Color('red')
            camera.annotate_text_size = 20
            camera.annotate_text = triggered_datetime
            time.sleep(10)

            # PIR sensor becomes inactive, camera stops recording and file is uploaded
            pir.wait_for_inactive()
            camera.stop_recording()
            logger.info('>>>PIR sensor: Motion ended')
            clean_and_upload(file_path, drive_utils, drive_folder_id)

    except KeyboardInterrupt:
        camera.close()
        sys.exit()
Пример #2
0
from gpiozero import MotionSensor, Buzzer
import time

pir = MotionSensor(4)
bz = Buzzer(3)

print("{} Waiting for PIR to stabilize".format(
    time.strftime("%B %d, %H:%M:%S")))
pir.wait_for_inactive()

while True:
    print("Ready")
    pir.wait_for_active()
    print("{0} Motion detected".format(time.strftime("%B %d, %H:%M:%S")))
    bz.beep(0.25, 0.5, 4)
    time.sleep(1)
import time
from gpiozero import LED, MotionSensor, Buzzer

motion_sensor = MotionSensor(4)
led = LED(2)
buzzer = Buzzer(17)

try:
    while True:
        motion_sensor.wait_for_active()
        print("Motion detected")
        buzzer.beep(n=2)
        led.blink(n=2)
        time.sleep(2)
finally:
    buzzer.off()
    led.off()
Пример #4
0
class Infrared():
    '''
        Infrared Task: reacts to infrared sensors.
        
        Parameters:
    
           pin: the GPIO pin used as an input
           event: the Event to be sent when the sensor is activated
           queue: the message queue receiving activation notifications
           level: the logging Level
    
        Usage:
    
           PIN = 12
           _infrared = Infrared(_queue, PIN, Orientation.PORT, Event.INFRARED_PORT, Level.INFO)
           _infrared.enable()

    '''

    THRESHOLD_VALUE = 0.1  # the value above which the device will be considered “on”
    DEACTIVATE_TIMEOUT_SEC = 0.01  # how long we wait to deactivate after being activated. If None (the default) then wait indefinitely.
    #   DEACTIVATE_TIMEOUT_SEC = None
    ACTIVATE_TIMEOUT_SEC = 0.2  # | 1.0 how long we wait to activate after being deactivated. If None (the default) then wait indefinitely.

    #   ACTIVATE_TIMEOUT_SEC   = None

    # ..........................................................................
    def __init__(self, queue, pin, orientation, event, level):
        self._queue = queue
        self._pin = pin
        self._orientation = orientation
        self._event = event
        self._label = event.description
        self._log = Logger("infrared:" + self._orientation.label, level)
        self._log.debug('initialising {} infrared:{} on pin {}.'.format(
            orientation.label, self._label, pin))

        self._closed = False
        self._enabled = False
        self._sensor = MotionSensor(pin,
                                    threshold=Infrared.THRESHOLD_VALUE,
                                    pull_up=True)
        self._sensor.when_motion = self._activated
        self._sensor.when_no_motion = self._deactivated
        self._log.info('ready.')

    # ..........................................................................
    def enable(self):
        if not self._closed:
            self._log.info('enabled infrared:{}'.format(self._label))
            self._enabled = True
        else:
            self._log.warning('cannot enable infrared:{} (closed)'.format(
                self._label))

    # ..........................................................................
    def disable(self):
        self._log.info('disabled infrared:{}'.format(self._label))
        self._enabled = False

    # ..........................................................................
    def _activated(self):
        '''
            The default function called when the sensor is activated.
        '''
        if self._enabled:
            if self._orientation is Orientation.PORT or self._orientation is Orientation.PORT_SIDE:
                self._log.info(
                    Fore.RED + 'activated {} infrared:{} on pin {}...'.format(
                        self._orientation.label, self._label, self._pin))
            elif self._orientation is Orientation.STBD or self._orientation is Orientation.STBD_SIDE:
                self._log.info(
                    Fore.GREEN +
                    'activated {} infrared:{} on pin {}...'.format(
                        self._orientation.label, self._label, self._pin))
            else:
                self._log.info(
                    Fore.YELLOW +
                    'activated {} infrared:{} on pin {}...'.format(
                        self._orientation.label, self._label, self._pin))
            self._queue.add(Message(self._event))
            if Infrared.ACTIVATE_TIMEOUT_SEC:
                self._sensor.wait_for_inactive(
                    timeout=Infrared.ACTIVATE_TIMEOUT_SEC)
        else:
            self._log.info(
                '[DISABLED] activated infrared:{} on pin {}...'.format(
                    self._label, self._pin))

    # ..........................................................................
    def _deactivated(self):
        '''
            The default function called when the sensor is deactivated.
        '''
        if self._enabled:
            self._log.debug(
                Style.DIM + 'deactivated {} infrared:{} on pin {}...'.format(
                    self._orientation.label, self._label, self._pin) +
                Style.RESET_ALL)
            if Infrared.DEACTIVATE_TIMEOUT_SEC:
                self._sensor.wait_for_active(
                    timeout=Infrared.DEACTIVATE_TIMEOUT_SEC)
        else:
            self._log.debug(
                '[DISABLED] deactivated infrared:{} on pin {}...'.format(
                    self._label, self._pin))

    # ..........................................................................
    def close(self):
        '''
            Permanently close and disable the infrared sensor.
        '''
        self._closed = True
        self._enabled = False
        self._log.info('closed.')