Hi all,
I need to exit a loop and shut down on a button press, the python script below works, in that it forces a sudo shutdown directly. Is this OK or do I need to find a way to exit the loop first? Ive tried sys.exit() and various attempts using kill -SIGSTOP but neither work. Might not be a problem but because its a headless outdoor project I'd like to reduce the risk of failure.
Thanks in advance
import time
import smtplib
import sys
import os
import datetime
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
os.system("cd /home/pi/detector")
GPIO.setup(17, GPIO.IN)
def stop(self):
print "Program stopping"
GPIO.cleanup()
os.system("sudo halt")
GPIO.add_event_detect(17, GPIO.FALLING, callback=stop, bouncetime=400)
try:
while True:
s = ''.join([ch if ch != ' ' else '_' for ch in str(datetime.datetime.now())])
os.system("AUDIODRIVER=alsa AUDIODEV=hw:0,0 rec /home/pi/detector/Wavs/" + s +".wav -G sinc 13k silence 1 0.1 20% trim 0 5")
time.sleep(4)
except KeyboardInterrupt:
print "You've stopped the program by CTRL-C"
GPIO.cleanup()
print "GPIO cleanup. All done."