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 |
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() sleep(.5) 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() sleep(10) 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+' ') sleep(2) LCD1602.write(0,0,' ') LCD1602.clear() sleep(1) GPIO.cleanup() LCD1602.clear() print('GPIO Good to Go') |