I'm trying to integrate a button into a simple alarm project of mine but am having trouble activating/deactivating the alarm with a button press.
Using Raspberry Pi 2 - Raspbian Jessie
My wiring is ok; I've run single python scripts on each part to make sure of that.
some extracts of my current code:
from signal import SIGTERM
from gpiozero import LED, Button, MotionSensor, Buzzer
import json
import subprocess
import functions as fnc
import time
###########Config##############
fnc.logToConsole("Reading setup config")
with open('config.json') as data_file:
config = json.load(data_file)
buttonPin = config["input"]["buttonPin"]
global currentState
currentState= False
def resetLeds():
fnc.resetLed(blueLedPin)
fnc.resetLed(redLedPin)
###########Reset##############
fnc.logToConsole("Resetting leds")
resetLeds()
###########Setting up##############
fnc.logToConsole("Setting up events")
button = Button(buttonPin)
global pirProcess
def pirEvent():
global currentState
global pirProcess
if currentState:
fnc.logToConsole("Shutting down pir")
pirProcess.kill()
resetLeds()
currentState = False
else:
currentState = True
fnc.logToConsole("Activating pir")
pirProcess = subprocess.Popen(['python', 'scripts/pirActive.py'])
fnc.logToConsole("Script ready, awaiting input")
while True:
button.wait_for_press()
button.wait_for_release()
pirEvent()
time.sleep(1)
The import of functions is just a small batch of functions I reuse in different scripts, nothing fancy. It logs to the console and resets my leds to off in this tiny script.
a quick rundown of what happens:
i read my pin's from a json file
reset my pins to off (noticed that's not always the case in gpiozero)
next i wait for a button press followed by a release, it's the release bit that's my trigger.
then I fire up my movement script (pir) (i know it's running at this point since it's logging to console as well)
I press again to shut it down. and this is where it goes wrong. it does deactivate that script, but my original script (the one above) becomes unresponsive to any followup button presses
More info on the ending: I know the script ended because i placed a try - finally block in there. the finally part gets fired logging out a successful ending. however, I do believe the way I kill it inflicts some harm on my original script since it won't even respond to ctrl+c but It's still running when i check in another console with ps -ef | grep test.py
Can anyone offer some insight on what I'm doing wrong?