0
votes

I have a GPIO button set on pin 23 of a Raspberry Pi 3 and I would like when the button is pressed for it to execute another python script. When I run the initial program, it will print "Button Pressed", but it will not execute the second program. (I did ensure the permissions are set in the program.) Thank you very much for your help!

    #!/usr/bin/env python
    import RPi.GPIO as GPIO
    import time
    import subprocess

    GPIO.setmode(GPIO.BCM)
    GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)

    while True:
        input_state = GPIO.input(23)
        if input_state == False:
            print('Button Pressed')
            subprocess.call('/home/pi/Downloads/PuttingItAllTogether.py', shell=True)
            time.sleep(0.2)
1
If it's printing "Button Pressed" then it ought to be running your script. Are you certain that your /home/pi/Downloads/PuttingItAllTogether.py script produces output? Have you checked the exit status of the script (this is the return value of subprocess.call)? - larsks
Thank you for your help @larsks. I am very new to programming. How do I check the exit status of the script? I can run the /home/pi/Downloads/PuttingItAllTogether.py directly from python and it works. I sincerely appreciate your help. - Laura Jacob
Like I said, it's the return value of the call to subprocess.call. Save that in a variable and print it out, as in returnval = subprocess.call(...) followed by a print statement. - larsks

1 Answers

0
votes

Could you not use the library "os" and use the "system"?

#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
import os

GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)

while True:
    input_state = GPIO.input(23)
    if input_state == False:
        print('Button Pressed')
        os.system('/home/pi/Downloads/PuttingItAllTogether.py')
        time.sleep(0.2)

Usage Syntax:

import os
os.system('shell command to execute')