In this video lesson we show how to create an Alarm System based on a Raspberry Pi, with user input from a keypad, and user prompts on an I2C LCD Display. From the earlier lessons in this series, you must install the LCD1602 Library, and you must install the keypad library. We include the code that we develop in this lesson below for your convenience:
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 68 | 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('al1.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() if CMD=='CA1'+pwd: myString='A'+pwd mixer.music.load('al1.mp3') if CMD=='CA2'+pwd: myString='A'+pwd mixer.music.load('al2.mp3') if CMD=='CA3'+pwd: myString='A'+pwd mixer.music.load('al3.wav') if CMD=='CA4'+pwd: myString='A'+pwd mixer.music.load('al4.wav') if CMD=='CA5'+pwd: myString='A'+pwd mixer.music.load('al5.mp3') sleep(1) GPIO.cleanup() LCD1602.clear() print('GPIO Good to Go') |