In this lesson we add an audible alarm to our security system. I am connecting to a Bluetooth speaker, and then using the pygame library to play an alarm sound. The pygame library will play any .mp3 file, so search the internet and find a suitable alarm sound for your system. I amusing an air raid siren, and really like it! Below for your convenience is the code we develop in this video:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# ==================================================================== # DISCLAIMER: # This code is provided as-is for educational and experimental # purposes only. The author makes no representations or warranties of # any kind concerning the safety, suitability, or accuracy of this # code. Use at your own risk. The author assumes no liability for any # damages, system failures, security breaches, or network issues # resulting from the use or implementation of this script. # ==================================================================== import RPi.GPIO as GPIO GPIO.setmode(GPIO.BOARD) PIRpin=12 GPIO.setup(PIRpin,GPIO.IN) import LCD1602 import KPLIB from time import sleep import threading myPad=KPLIB.keypad(retChar='D') LCD1602.init(0x27,1) myString='' pwd='1234' from pygame import mixer mixer.init() mixer.music.load('alarm.mp3') def readKP(): global myString while myString != '*': myString=myPad.readKeypad() ts=.5 sleep(ts) readThread=threading.Thread(target=readKP,) readThread.daemon=True readThread.start() while myString != '*': CMD=myString if CMD=='A'+pwd: LCD1602.write(0,0,'Armed ') moveVal=GPIO.input(PIRpin) if moveVal==1: LCD1602.write(0,1,'Intruder Alert') mixer.music.play() ts=10 sleep(ts) if moveVal==0: LCD1602.write(0,1,'All Clear ') if CMD=='B'+pwd: LCD1602.write(0,0,'UnArmed ') LCD1602.write(0,1,' ') if CMD=='C'+pwd: LCD1602.write(0,0,'Password? ') LCD1602.write(0,1,' ') while myString=='C'+pwd: pass pwd=myString LCD1602.write(0,0,pwd+' ') ts=2 sleep(ts) LCD1602.write(0,0,' ') LCD1602.clear() ts=1 sleep(ts) GPIO.cleanup() LCD1602.clear() print('GPIO Good to Go') |