RubiksCube
Posts: 3
Joined: Sun Jan 11, 2015 2:11 pm

Status LED over GPIO tied to process

Sun Jan 11, 2015 2:33 pm

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?

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

Re: Status LED over GPIO tied to process

Sun Jan 11, 2015 6:14 pm

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"
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

RubiksCube
Posts: 3
Joined: Sun Jan 11, 2015 2:11 pm

Re: Status LED over GPIO tied to process

Mon Jan 12, 2015 2:28 pm

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.

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

Re: Status LED over GPIO tied to process

Mon Jan 12, 2015 3:53 pm

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.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

RubiksCube
Posts: 3
Joined: Sun Jan 11, 2015 2:11 pm

Re: Status LED over GPIO tied to process

Tue Jan 13, 2015 6:58 pm

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

Jfx
Posts: 1
Joined: Tue Feb 17, 2015 6:55 pm

Re: Status LED over GPIO tied to process

Tue Feb 17, 2015 7:03 pm

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

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: Status LED over GPIO tied to process

Fri Feb 20, 2015 4:11 pm

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)
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

-rst-
Posts: 1316
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland

Re: Status LED over GPIO tied to process

Fri Feb 20, 2015 4:29 pm

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
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'

Return to “Python”