iamsmudge
Posts: 6
Joined: Mon Dec 29, 2014 10:55 pm

Start and STOP script from button.

Mon Jan 12, 2015 5:30 pm

Hi guys I have managed to get the Pi to run a script on a button input.

The Button script has the PiCam script embedded. So Button script says: On detecting pin x run PiCam script. No problems all good.

What I'd like to add is some code that says on second button push stop script. So one button, push once runs script, push a second time stop script and then loop that I suppose so that each subsequent push starts or stops the script until shutdown of either Pi or script.

So far I run the following which although may not be perfect seems to work. Any help would be greatly appreciated.

Thanks
S

Code: Select all

import RPi.GPIO as GPIO
Import time
import subprocess

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

while True:
input_state = GPIO.input(23)
if input_state == False:
subprocess.call("/home/pi/securipi-rpicamtd.sh", shell=True)
time.sleep(0.2)

gordon77
Posts: 6885
Joined: Sun Aug 05, 2012 3:12 pm

Re: Start and STOP script from button.

Mon Jan 12, 2015 6:16 pm

I use the following to start and stop a subprocess

p=subprocess.Popen(rpistr,shell=True,preexec_fn=os.setsid)

os.killpg(p.pid, signal.SIGTERM)

Where rpistr is the command you want to run.
Last edited by gordon77 on Mon Jan 12, 2015 7:45 pm, edited 1 time in total.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Start and STOP script from button.

Mon Jan 12, 2015 7:13 pm

Any reason you don't then do

Code: Select all

p.terminate()
?
That should send the SIGTERM signal. I'm curious if it doesn't work as well as your method.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

gordon77
Posts: 6885
Joined: Sun Aug 05, 2012 3:12 pm

Re: Start and STOP script from button.

Mon Jan 12, 2015 7:49 pm

I'm not sure but this seems to imply p.terminate still keeps the process running in the background

http://stackoverflow.com/questions/4789 ... shell-true

User avatar
iinnovations
Posts: 621
Joined: Thu Jun 06, 2013 5:17 pm

Re: Start and STOP script from button.

Mon Jan 12, 2015 8:35 pm

It would be cleaner to put the shell script commands inside the python script and simply stop execution of the Python script when the button is pressed.
CuPID Controls :: Open Source browser-based sensor and device control
interfaceinnovations.org/cupidcontrols.html
cupidcontrols.com

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Start and STOP script from button.

Mon Jan 12, 2015 9:44 pm

gordon77 wrote:I'm not sure but this seems to imply p.terminate still keeps the process running in the background

http://stackoverflow.com/questions/4789 ... shell-true
That would explain it and the code actually used looks very like the solution too!
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

iamsmudge
Posts: 6
Joined: Mon Dec 29, 2014 10:55 pm

Re: Start and STOP script from button.

Tue Jan 13, 2015 1:32 pm

Sorry guys you'll have to excuse me, but you lost me with those terms. I don't recognise them or understand how they work.

In simple terms, I seem to have a loop that looks for button presses and if I replaced the "run script" element with "print Button Pressed" then each push of the button would print "Button Pressed"

In keeping with the style of the code I have, what could I add so that each subsequent press stopped then started the script, or in simple terms allows press 2 to supersede the actions on press 1. and so on.

So in plain english I suppose I want to say.

Look for a button press on pin 21
when true start script X
leave script x running until button press button 21
when true stop script x and look for button press on pin 21
when true start script x

and continue this in a loop.

I'm sorry I don't have the language or understanding to speak about this other than in plain english. Any help is greatly appreciated.

S

poing
Posts: 1132
Joined: Thu Mar 08, 2012 3:32 pm

Re: Start and STOP script from button.

Tue Jan 13, 2015 2:34 pm

Semi code:

buttonState=0

While true:
if buttonPressed and buttonState=0 then buttonState=1 and PiCam=go
else if buttonPressed and buttonState=1 then buttonState=0 and PiCam=stop

gordon77
Posts: 6885
Joined: Sun Aug 05, 2012 3:12 pm

Re: Start and STOP script from button.

Tue Jan 13, 2015 6:08 pm

Something like this, try to press the button less than the 1 second. I wrote it on a tablet so l hope the indentation works OK.

Code: Select all

import RPi.GPIO as GPIO
Import time
import subprocess

GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIOIN, pull_up_down=GPIO.PUD_UP)
set = 0
while True:
   Input_state = GPIO.input(23)
    if input_state == False and set == 0:
        p=subprocess.Popen( "/home/pi/securipi-picamtd.sh",shell=True,preexec_fn=os.setsid) 
        time.sleep(1)
        set =1
    Input_state = GPIO.input(23)
    if input_state == False and set == 1:
         os.killpg(p.pid, signal.SIGTERM)
         time.sleep(1)
         set =0
Last edited by gordon77 on Tue Jan 13, 2015 9:08 pm, edited 1 time in total.

iamsmudge
Posts: 6
Joined: Mon Dec 29, 2014 10:55 pm

Re: Start and STOP script from button.

Tue Jan 13, 2015 8:52 pm

gordon77 wrote:Something like this, try to press the button less than the 1 second. I wrote it on a tablet so l hope the indentation works OK.

Code: Select all

import RPi.GPIO as GPIO
Import time
import subprocess

GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIOIN, pull_up_down=GPIO.PUD_UP)
set = 0
while True:
   Input_state = GPIO.input(23)
    if input_state == False and set = 0:
        subprocess.("/home/pi/securipi-rpicamtd.sh", shell=True)
        p=subprocess.Popen( "/home/pi/securipi-picamtd.sh",shell=True,preexec_fn=os.setsid) 
        time.sleep(1)
        set =1
    Input_state = GPIO.input(23)
    if input_state == False and set = 1:
         os.killpg(p.pid, signal.SIGTERM)
         time.sleep(1)
         set =0
Thank you very much for trying to help, but unfortunately that returned an error initially:

Code: Select all

File "Button2.py", line 12
    if input_state == False and set = 0:
                                   ^
SyntaxError: invalid syntax
I tried to remove the

Code: Select all

and set = 0
I then received a second error

Code: Select all

 File "Button2.py", line 13
    subprocess.("/home/pi/securipi-rpicamtd.sh", shell=True)
               ^
SyntaxError: invalid syntax
Thanks again. I'm not getting very far with this so I think I'll try to re-write the whole thing within a single python script using PiCam rather than trying to launch a .sh from inside the py script I think its a bit advanced for me yet. Thanks for your help though

S

gordon77
Posts: 6885
Joined: Sun Aug 05, 2012 3:12 pm

Re: Start and STOP script from button.

Tue Jan 13, 2015 9:02 pm

Sorry it should be and set == 0, and set == 1 in the other line

iamsmudge
Posts: 6
Joined: Mon Dec 29, 2014 10:55 pm

Re: Start and STOP script from button.

Tue Jan 13, 2015 9:03 pm

OK thanks, I'll try that first before I give up

gordon77
Posts: 6885
Joined: Sun Aug 05, 2012 3:12 pm

Re: Start and STOP script from button.

Tue Jan 13, 2015 9:06 pm

And it should be p= subprocess.Popen(

It got corrupted somehow and l got an extra line.

I've edited the code above.

The line
p=subprocess.Popen( "/home/pi/securipi-picamtd.sh",shell=True,preexec_fn=os.setsid) 

Should be one continuous line, my tablet keeps putting a line return in the middle

Code: Select all

import RPi.GPIO as GPIO
Import time
import subprocess

GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIOIN, pull_up_down=GPIO.PUD_UP)
set = 0
while True:
    input_state = GPIO.input(23)
    if input_state == False and set == 0:
        p=subprocess.Popen( "/home/pi/securipi-picamtd.sh",shell=True,preexec_fn=os.setsid) 
        time.sleep(1)
        set =1
    input_state = GPIO.input(23)
    if input_state == False and set == 1:
        os.killpg(p.pid, signal.SIGTERM)
        time.sleep(1)
        set =0

gordon77
Posts: 6885
Joined: Sun Aug 05, 2012 3:12 pm

Re: Start and STOP script from button.

Thu Jan 15, 2015 1:50 pm

I'm now at my Pi so here's a program I have working

I've used GPIO24 but you can change that. The switch is wired to GPIO24 and earth, GPIO24 also connected to +5v with a 2.7k resistor.

I've used it to start and stop raspistill on alternate presses of the switch, just change rpistr to your program to run

Code: Select all

import RPi.GPIO as GPIO
import time
import subprocess, os
import signal
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO_switch = 24  # pin 18
GPIO.setup(GPIO_switch,GPIO.IN)

print "  Press Ctrl & C to Quit"

try:
    
   run = 0
   while True :
      if GPIO.input(GPIO_switch)==0 and run == 0:
         print "  Started"
         rpistr = "raspistill -o /run/shm/test.jpg -t 0 -tl 0  "
         p=subprocess.Popen(rpistr,shell=True, preexec_fn=os.setsid)
         run = 1
         while GPIO.input(GPIO_switch)==0:
             time.sleep(0.1)
      if GPIO.input(GPIO_switch)==0 and run == 1:
         print "  Stopped " 
         run = 0
         os.killpg(p.pid, signal.SIGTERM)
         while GPIO.input(GPIO_switch)==0:
             time.sleep(0.1)
       

except KeyboardInterrupt:
  print "  Quit"
  GPIO.cleanup() 

GuruBastade
Posts: 4
Joined: Tue Dec 22, 2015 12:13 pm

Re: Start and STOP script from button.

Wed Dec 23, 2015 6:34 am

Hello,
How can I write a code to start live streaming from USB camera on pressing button once and stop it on pressing second time.
The command I'm using for streaming is - mplayer tv ://
I'm able to start streaming on pressing button using the following code but unable to stop it.

import RPi.GPIO as GPIO
import time
import os

#adjust for where your switch is connected
buttonPin = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(buttonPin,GPIO.IN)

while 1:

if (GPIO.input(buttonPin)):
os.system("python /home/pi/Desktop/cam.py")

gordon77
Posts: 6885
Joined: Sun Aug 05, 2012 3:12 pm

Re: Start and STOP script from button.

Wed Dec 23, 2015 6:44 am

Have you tried subprocess?

Import subprocess
rpistr = " <required command string>"
p=subprocess.Popen(rpistr,shell=True, preexec_fn=os.setsid)

....
And to kill it...

os.killpg(p.pid, signal.SIGTERM)

Return to “Python”