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

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.logToConsole("Resetting leds")

###########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")
        currentState = False
        currentState = True
        fnc.logToConsole("Activating pir")
        pirProcess = subprocess.Popen(['python', 'scripts/pirActive.py'])

fnc.logToConsole("Script ready, awaiting input")
while True:

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?

  • It would be helpful for us to see the code for your pirActive.py script. The first thing I would do is add some logging to your "while True" loop so you can tell what state its in when it gets stuck. Does your pirActive script use GPIO also? I don't know how the RaspberryPi handles having multiple scripts using GPIO at the same time. Perhaps when your pirActive script exits it resets the state of your GPIO pins and breaks the wait_for_press or wait_for_release in your main script. – jwygralak67 Feb 11 '16 at 20:56
  • I believe you're right. The second script uses GPIO also and that seems to break it something. I'll try and post the second script tomorrow but my own logs showed that starting the script worked just fine and the original one waited for another button press. It registered that one as well but I believe, again I have to test, killing the pirActive process kills the GPIO import for the original script as well since it goes into its waiting mode just fine. But doesn't register any pressed anymore. – MrBramme Feb 11 '16 at 21:51
  • I delved into the gpiozero source code a bit and found that it uses multithreading. I know from experience, if there is a background thread running, ctrl-C will not kill it. The main thread will die, but you'll be left with an unresponsive terminal. You then have to open a new terminal and kill the original python script. Its not hard to shoot yourself in the foot with multithreaded python. In fact, with multithreaded python you can easily shoot yourself in all of your feet at once. – jwygralak67 Feb 11 '16 at 22:39
  • Thanks. It does appear like I'm shooting myself in the foot :-) did another test before work. I replaced my pirActive script with a script that just prints to console "I'm active" every second. As soon as I kill that process the console gets unresponsive again (so not using a GPIO import). Seems like I'm killing it incorrectly. To avoid the issue I'll integrate both scripts into one this weekend and refactor the entire thing. Many thanks for the help so far. If you do have more suggestions, I'm always open to learn new things! – MrBramme Feb 12 '16 at 6:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.