Hi @gadgetoid, @Gisky, @Jon and anyone else who has been following the evolving code.
I cannot thank you all enough for your help.
As mentioned previously, I am running the code through LXTerminal using sudo idle from XWindow.
So close… so very close.
I needed to apt-get install espeak for the voice to work.
I used the info supplied in the following link to do this.
http://elinux.org/RPi_Text_to_Speech_(Speech_Synthesis)
I have one final issue… the following lines near the bottom of the code need to request another DISARM input until it matches PIN (if an incorrect DISARM has been already entered) - this is currently not working. In BASIC, I would gosub to the start of the routine that checks for a DISARM entry. I have not been able to figure this out in Python.
The erroneous lines are:
#Keep system active if DISARM is not the same as PIN
if DISARM != PIN:
pass
The code for the whole program is as follows and, apart from the little glitch, works as intended. I have added comments to help others understand the processes. As usual, I feel this may be very clunky and could be streamlined by those with better coding skills than I currently possess. I welcome any feedback.
import explorerhat, time, os
#Set the PIN number
PIN = []
#Set the DISARM number
DISARM = []
#Check for keypad presses, print on screen and log the response into the PIN number
def onePressed(channel, event):
print("I got a touch on button: {}".format(channel))
PIN.append("1")
explorerhat.touch.one.pressed(onePressed)
def twoPressed(channel, event):
print("I got a touch on button: {}".format(channel))
PIN.append("2")
explorerhat.touch.two.pressed(twoPressed)
def threePressed(channel, event):
print("I got a touch on button: {}".format(channel))
PIN.append("3")
explorerhat.touch.three.pressed(threePressed)
def fourPressed(channel, event):
print("I got a touch on button: {}".format(channel))
PIN.append("4")
explorerhat.touch.four.pressed(fourPressed)
print ("Please input PIN number:")
#Check the PIN number has four digits or wait until it has
while len(PIN) < 4:
pass
#When PIN has four digits, display the PIN and continue
explorerhat.touch.pressed(None)
PIN = PIN[0:4]
print(PIN)
#Set up the speech voice parameters
def speak(text):
os.system("espeak -vm7 -p0,180 -s170 ' " + text + " ' ")
#Display a steady green LED indicating the system is armed
explorerhat.light.green.on()
print "*** SYSTEM ARMED ***"
speak("SSISSTEM ARMED")
#Detect an alert and respond with red, pulsing LED, screen prompt and voice alert
def alert(pin):
print "*** WARNING! INTRUDER ALERT! ***"
print("Zone : " + pin.name)
explorerhat.light.red.pulse()
explorerhat.light.green.off()
speak("IWARNING. INTRUDER ALERT.")
return
explorerhat.input.one.pressed(alert)
#Alarm resets awaiting further alert
def zeroalert(pin):
print "*** SYSTEM ARMED ***"
print "*** ENTER PIN TO DISARM SYSTEM ***"
explorerhat.light.red.off()
explorerhat.light.green.on()
explorerhat.input.one.released(zeroalert)
#Check for keypad presses to disarm, print on screen and log the response into the DISARM number
def onePressed(channel, event):
print("I got a touch on button: {}".format(channel))
DISARM.append("1")
explorerhat.touch.one.pressed(onePressed)
def twoPressed(channel, event):
print("I got a touch on button: {}".format(channel))
DISARM.append("2")
explorerhat.touch.two.pressed(twoPressed)
def threePressed(channel, event):
print("I got a touch on button: {}".format(channel))
DISARM.append("3")
explorerhat.touch.three.pressed(threePressed)
def fourPressed(channel, event):
print("I got a touch on button: {}".format(channel))
DISARM.append("4")
explorerhat.touch.four.pressed(fourPressed)
#Check the DISARM number has four digits or wait until it has
while len(DISARM) < 4:
pass
explorerhat.touch.pressed(None)
DISARM = DISARM[0:4]
#Keep system active if DISARM is not the same as PIN
if DISARM != PIN:
pass
#If DISARM code is same as PIN then shutdown system
if DISARM == PIN:
print "*** SYSTEM SHUTTING DOWN ***"
speak("SYSTEM SHUTTING DOWN")
explorerhat.light.off()
explorerhat.output.off()
explorerhat.input.clear_events()
Kind regards,
TankMan