0

I need to run some system commands on a button press. But after pressing the first button the script seams to stop working, it's not closing but doesn't react to any other button presses.

For test I wrote this script:

#!/usr/bin/python

import RPi.GPIO as GPIO
import time
import os

buttonShutdown = 6
button9x9 = 13
buttonNext = 19
buttonPrev = 26

GPIO.setmode(GPIO.BCM)
GPIO.setup(buttonShutdown, GPIO.IN)
GPIO.setup(button9x9, GPIO.IN)
GPIO.setup(buttonNext, GPIO.IN)
GPIO.setup(buttonPrev, GPIO.IN)

GPIO.add_event_detect(buttonShutdown, GPIO.FALLING)
GPIO.add_event_detect(button9x9, GPIO.FALLING)
GPIO.add_event_detect(buttonNext, GPIO.FALLING)
GPIO.add_event_detect(buttonPrev, GPIO.FALLING)

while True:
  if GPIO.event_detected(buttonShutdown):
    cmd = "echo shutdown"
    os.system(cmd)
  if GPIO.event_detected(button9x9):
    cmd = "echo 9x9"
    os.system(cmd)
  if GPIO.event_detected(buttonNext):
    cmd = "echo next"
    os.system(cmd)
  if GPIO.event_detected(buttonPrev):
    cmd = "echo prev"
    os.system(cmd)
  time.sleep(0.1)

Does anyone know why it seems to hang?

  • Is above script the same that fails to respond or is it different ? if not, please share the script that fails. If the original script was actually executing the commands instead of being echoed as in this script and you pressed the "shutdown" button, the system would have actually halted which would give an appearance of "hang". i also noticed the input pins' PULL UP/DOWN resistors are not enabled, i assume you have external resistors setup – Shreyas Murali Oct 18 '16 at 21:34
  • yes the build is some like this "gpio pin <- 10k resistor | ground -> button <- 3.3V", the script is exactly that postet one... first echo work then nothing... – Broatcast Oct 18 '16 at 21:43
2

It's a known issue with the RPi.GPIO Python module.

#127 os.system breaks event detection

https://sourceforge.net/p/raspberry-gpio-python/tickets/127/

  • ok the system call seams the problem ... u know any solutions or workarounds? i see some like that – Broatcast Oct 18 '16 at 21:45
  • sry i kant format the command correctly -..-* a mod delete this above?... i see this and he call the script again in it self... but this spawn a new sub thread and not close the first ...and again and again ... so this seams not to be a good solution – Broatcast Oct 18 '16 at 21:50
  • Try subprocess.call instead? It doesn't rely on os.system under the covers (uses os.fork on POSIX platforms instead); no idea if it'll work or not, but it's worth a shot. – Dave Jones Oct 19 '16 at 14:54
  • @Broatcast You should be able to delete comments yourself. If you hover there should be a small circular delete cross. The RPi.GPIO author has a fix and if testing is okay will publish it within a day or so. – joan Oct 19 '16 at 15:35
  • This bug is now fixed in the just-released RPi.GPIO 0.6.3. – mkimball Oct 30 '16 at 23:01

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.