Page 1 of 1

Status LED over GPIO tied to process

Posted: Sun Jan 11, 2015 2:33 pm
by RubiksCube
Hi

In connection to a little project I'm making based on the RuneAudio distro (runs on a stripped version of Arch). I want to have a LED turn on when Media Player Daemon is running.

I'm a total beginner with programming in general so any help here will be aprciated.

I started poking around the net for solutions and I found this script made for a wardriving set-up https://docs.google.com/file/d/0B1i26Iu ... JJSjg/edit and I thought I could use this as a basis for my own project.

I made some changes that I thought was the correct way of doing it.

Code: Select all

import RPi.GPIO as GPIO
import os
import subprocess
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)#Sets the GPIO pin to be an output pin
GPIO.output(17, False) #Makes sure all LED's are at the same state (off)

while 1 < 2:
    mpd = subprocess.Popen(['ps -ef | grep /usr/bin/mpd | grep -v grep '], stdout=subprocess.PIPE, shell=True) #Assigns the output from the grep to the kismet variable
    (output, error) = mpd.communicate()
    if '/usr/bin/mpd' in output:
        GPIO.output(17, True) #Turn on LED
    else:
        GPIO.output(17, False) #Turn off LED
But I get this error when I run the script:

Code: Select all

Traceback (most recent call last):
  File "led-test.py", line 12, in <module>
    if '/usr/bin/mpd' in output:
TypeError: Type str doesn't support the buffer API
Any ideas on what is wrong?

Re: Status LED over GPIO tied to process

Posted: Sun Jan 11, 2015 6:14 pm
by elParaguayo
If it were me, I'd probably do it like this:

Code: Select all

import subprocess

MPD_FILE = "/usr/bin/mpd"

output = subprocess.check_output(["ps", "-ef"])

if MPD_FILE in output:
     print "MPD running"
else:
    print "MPD not running"

Re: Status LED over GPIO tied to process

Posted: Mon Jan 12, 2015 2:28 pm
by RubiksCube
Thank you for the answer elParaguayo.

that worked for me with a little bit of tweaking

I ended up doing it like this:

Code: Select all

#!/usr/bin/python
import RPi.GPIO as GPIO
import os
import subprocess
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)#Sets the GPIO pin to be an output pin
GPIO.output(17, False) #Makes sure all LED's are at the same state (off)
GPIO.output(27, False)

MPD_FILE = b"/usr/bin/mpd"

output = subprocess.check_output(["ps", "-ef"])

if MPD_FILE in output:
     print ("MPD running")
     GPIO.output(17, True) #Turn on GREEN LED
else:
     print ("MPD not running")
     GPIO.output(17, False) #Turn off GREEN LED

REDIS_FILE = b"/usr/bin/redis-server"

output = subprocess.check_output(["ps", "-ef"])

if REDIS_FILE in output:
     print ("Redis Server running")
	 GPIO.output(27, True) #Turn on LED
else:
     print ("Redis Server not running")
	 GPIO.output(27, False) #Turn off LED
It seems to work for me like this.

I also figured you if I added a "b" before "/usr/bin/mpd" eg. MPD_FILE = b"/usr/bin/mpd" the "TypeError: Type str doesn't support the buffer API" went away. something about byte strings and unicode strings. I guess the "b" makes it a byte string.

Anyway thank you for your help.

Re: Status LED over GPIO tied to process

Posted: Mon Jan 12, 2015 3:53 pm
by elParaguayo
Glad I could help.

One little comment, do you need to run "ps -ef" for the second time? You could just check whether REDIS_FILE is in the existing output variable.

Re: Status LED over GPIO tied to process

Posted: Tue Jan 13, 2015 6:58 pm
by RubiksCube
You are right. Just goes to show that I have lot to learn.
Cleaned the code up a little and added a 10 second sleep.

Code: Select all

#!/usr/bin/python
import RPi.GPIO as GPIO
import os
import subprocess
import time
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)#Sets the GPIO pin to be an output pin
GPIO.setup(27, GPIO.OUT)
GPIO.output(17, False) #Makes sure all LED's are at the same state (off)
GPIO.output(27, False)

while True:
	MPD_FILE = b"/usr/bin/mpd"
	REDIS_FILE = b"/usr/bin/redis-server"
	
	output = subprocess.check_output(["ps", "-ef"])

	if MPD_FILE in output:
		GPIO.output(17, True) #Turn on GREEN LED
	else:
		GPIO.output(17, False) #Turn off GREEN LED

	if REDIS_FILE in output:
		GPIO.output(27, True) #Turn on RED LED
	else:
		GPIO.output(27, False) #Turn off RED LED
	time.sleep(10)  # Delay for 10 seconds
Is it possible to have 2 different sleep cycles? One if both outputs are true and one if one of the outputs are false?

Thanks again elParaguayo

Re: Status LED over GPIO tied to process

Posted: Tue Feb 17, 2015 7:03 pm
by Jfx
Hello,

I'm interesting by your script, i'm a really rookie with python (with english too !) so you can imagine my distress...

I just want to light a Led when my volumio is playing.

When i try your script the led stays light on. Playing or not.

What do i have to change in your script ?

Please help me.

Thank guys

Re: Status LED over GPIO tied to process

Posted: Fri Feb 20, 2015 4:11 pm
by -rst-
Jfx wrote:I just want to light a Led when my volumio is playing.

When i try your script the led stays light on. Playing or not.

What do i have to change in your script ?
You should just change the mpd spcefics to match your volumio:
- start volumio
- run the ps command manually to find out what volumio looks like in the output
- change the row MPD_FILE = b"/usr/bin/mpd" to MPD_FILE = b"/usr/bin/volumio" (or what ever the path to volumio is in the ps output)

Re: Status LED over GPIO tied to process

Posted: Fri Feb 20, 2015 4:29 pm
by -rst-
RubiksCube wrote:Is it possible to have 2 different sleep cycles? One if both outputs are true and one if one of the outputs are false?
If by 'different sleep cycles' you mean different sleep lengths, maybe something like:

Code: Select all

mpd_on = False
redis_on = False

...

   if MPD_FILE in output:
      mpd_on = True
   else:
      mpd_on = False
   GPIO.output(17, mpd_on) #Turn on/off GREEN LED

   if REDIS_FILE in output:
      redis_on = True
   else:
      redis_on = False
   GPIO.output(27, redis_on) #Turn on/off RED LED

   sleeptime = 1
   if mpd_on and redis_on:
      sleeptime = 10
   else:
      sleeptime = 5

   time.sleep(sleeptime)  # Delay for x seconds