#!/usr/bin/env python import signal import socket import time import string import sys import RPi.GPIO as GPIO import time import math from array import * import logging import os GPIO_SWITCH = 15 GPIO_PIR = 18 GPIO_SOUNDER = 22 GPIO_LED = 12 alarm_on = False intruder_alert = False sleep_time = 60.0 sig_count = 0 def SignalHandler(signal, frame): logger.info("SigHandler called") global keep_spinning global sig_count global intruder_alert sig_count += 1 if not (intruder_alert and sig_count == 1): logger.info("Shutdown interuder_alert = %d, sig_count = %d", intruder_alert, sig_count) keep_spinning = False def switch_callback(channel): global alarm_on global sleep_time global intruder_alert global sig_count #----------------------------------------------------------------------------------- # Rising edge detected on the switch, toggle alarm_on #----------------------------------------------------------------------------------- logger.info("Switch Callback") alarm_on = not alarm_on if alarm_on: #--------------------------------------------------------------------------- # Set the LED on, beep for 30s, then turn the LED off #--------------------------------------------------------------------------- logger.info("Alarm turned on") GPIO.output(GPIO_LED, GPIO.HIGH) for loop_count in range(0, 30): GPIO.output(GPIO_SOUNDER, not GPIO.input(GPIO_SOUNDER)) time.sleep(0.01) GPIO.output(GPIO_SOUNDER, not GPIO.input(GPIO_SOUNDER)) time.sleep(0.49) GPIO.output(GPIO_LED, GPIO.LOW) #------------------------------------------------------------------- # The alarm has just been turned on, enable the PIR #------------------------------------------------------------------- GPIO.add_event_detect(GPIO_PIR, GPIO.RISING, callback = PIR_callback) else: logger.info("Alarm turned off") GPIO.remove_event_detect(GPIO_PIR) intruder_alert = False sleep_time = 60 sig_count = 0 GPIO.output(GPIO_LED, GPIO.LOW) GPIO.output(GPIO_SOUNDER, GPIO.LOW) def PIR_callback(channel): global intruder_alert global sleep_time #----------------------------------------------------------------------------------- # Rising edge detected on PIR, set intruder alert, and wake the main thread #----------------------------------------------------------------------------------- logger.info("Intruder alert!!!") GPIO.remove_event_detect(GPIO_PIR) intruder_alert = True sleep_time = 1.0 os.kill(os.getpid(), signal.SIGINT) #------------------------------------------------------------------------------------------- # Create a base level logger #------------------------------------------------------------------------------------------- logger = logging.getLogger('drone logger') logger.setLevel(logging.INFO) #------------------------------------------------------------------------------------------- # Create file and console logger handlers #------------------------------------------------------------------------------------------- file_handler = logging.FileHandler('drone.log', 'w') file_handler.setLevel(logging.WARNING) console_handler = logging.StreamHandler() console_handler.setLevel(logging.DEBUG) #------------------------------------------------------------------------------------------- # Create a formatter and add it to both handlers #------------------------------------------------------------------------------------------- # create formatter and add it to the handlers formatter = logging.Formatter('[%(levelname)s] (%(threadName)-10s) %(message)s') console_handler.setFormatter(formatter) file_handler.setFormatter(formatter) #------------------------------------------------------------------------------------------- # Create a formatter and add it to both handlers #------------------------------------------------------------------------------------------- logger.addHandler(console_handler) logger.addHandler(file_handler) signal.signal(signal.SIGINT, SignalHandler) GPIO.setmode(GPIO.BOARD) GPIO.setup(GPIO_SWITCH, GPIO.IN, pull_up_down = GPIO.PUD_DOWN) GPIO.setup(GPIO_PIR, GPIO.IN, pull_up_down = GPIO.PUD_DOWN) GPIO.setup(GPIO_SOUNDER, GPIO.OUT) GPIO.output(GPIO_SOUNDER, GPIO.LOW) GPIO.setup(GPIO_LED, GPIO.OUT) GPIO.output(GPIO_LED, GPIO.LOW) #------------------------------------------------------------------------------------------- # OK, all set up, setup the switch interrupt #------------------------------------------------------------------------------------------- GPIO.add_event_detect(GPIO_SWITCH, GPIO.RISING, callback = switch_callback, bouncetime = 16000) keep_spinning = True #------------------------------------------------------------------------------------------- # Sleep until the Alarm is turned on #------------------------------------------------------------------------------------------- while keep_spinning: time.sleep(sleep_time) logger.info("Who woke me up?") if intruder_alert: logger.info("Intruder Alert") GPIO.output(GPIO_SOUNDER, not GPIO.input(GPIO_SOUNDER)) elif alarm_on: #--------------------------------------------------------------------------- # The alarm is enable - one short flash a minute #--------------------------------------------------------------------------- logger.info("Alarm On") GPIO.output(GPIO_LED, GPIO.input(GPIO_LED)) time.sleep(0.1) GPIO.output(GPIO_LED, GPIO.input(GPIO_LED)) GPIO.cleanup()