Using System LEDs for Recording Status

From MythTV Official Wiki
Revision as of 02:45, 22 April 2010 by Wagnerrp (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

I don't personally care to have the Power LED or the HDD LED reflect system power or disc activity -- I figured it would be nice if these two LEDs showed me if my dual tuner MythTV box was recording (and specifically which tuner(s) were recording).

To do this I used

  • Python 2.3.3 (came with Redhat Fedora Core 2, but, any 2.2 or later Python should work)
  • py Serial 2.1
  • A female db9 serial connector, preferably with wires connected to the pins
  • Some extra wire

On my system I ended up using COM1, my script reflects this.

The first thing you have to do is connect your serial port to the LEDs. To do this, cut the little back connector from the HDD LED wires and Power LED wires which normally you would connect to your motherboard. Strip the ends of the wires. Each wire pair will be some color and white. If you want this to be reversible don't cut the black thing off, just figure out a better way to connect the wires.

Next, focusing on the female DB9. If you face the connector toward you there are 9 holes, the pinout is

  5 4 3 2 1
   9 8 7 6

The pins we want to work with are 4 (DTR), 5 (Ground), and 7 (RTS).

You will now use your extra wire to connect 5 (Ground) to the two white cables coming from the case LEDs.

Next, connect 4 (DTR) to one of the colored cables. Optionally you could use a resistor in this connection, something like a 1000 ohm resistor from what I hear. I didn't do it and it didn't seem necessary, but, this maybe a good idea. These resistors are probably really cheap at Radio Shack.

Finally, connect 7 (RTS) to the other colored cable. Again, maybe a resistor.

I would recommend soldering and/or electrical taping all of the connections. Personally I have just twisted the wires together and electirical taped them, but, do what you like.

Note: I just ran the wires from the DB9 plugged into the serial port at back of the case through one of the PCI slot openings. You may have a better way to do it, I couldn't think of one.

Plug the DB9 into a serial port (preferably COM1).

Now for the Python script to monitor if you are recording (OR watching LiveTV, there is no real difference)...

My script was created as "/root/checkrecording.py" and I made sure it had execute permissions.


PythonIcon.png /root/checkrecording.py

#!/usr/bin/python

import commands
import serial,time

#Open COM1
s = serial.Serial(0)
#Turn of all lights
s.setDTR(0)
s.setRTS(0)

while True:
   recording = commands.getoutput('/sbin/fuser /dev/video0 | grep -cv grep')
   if recording == "1":
      s.setDTR(1)
   else:
      s.setDTR(0)
   recording = commands.getoutput('/sbin/fuser /dev/video1 | grep -cv grep')
   if recording == "1":
      s.setRTS(1)
   else:
      s.setRTS(0)
   #Sleep for a minute
   time.sleep(60)

Update...

When I originally wrote the above script, this worked great for me. I have since moved to KnoppMyth and fuser doesn't seem to respond the same way, so, I have changed the script a bit. It probably isn't quite as efficient, but, somewhat easier to extend probably (and certainly efficient enough since it only runs through once a minute).


PythonIcon.png /root/checkrecording.py

#!/usr/bin/python

import httplib
import serial,time

mythStatus = "localhost:6544"
encoder1 = "Encoder 1 "
encoder2 = "Encoder 2 "
recording = "is local on mythtv and is recording"

#Open COM1
s = serial.Serial(0)
#Turn of all lights
s.setDTR(0)
s.setRTS(0)

while(True):
   conn = httplib.HTTPConnection(mythStatus)
   conn.request("GET", "/")
   r1 = conn.getresponse()
   results = r1.read()
   if encoder1+recording in results:
      s.setDTR(1)
   else:
      s.setDTR(0)
   if encoder2+recording in results:
      s.setRTS(1)
   else:
      s.setRTS(0)
   time.sleep(60)

As this script only checks for recording status of the two tuners once a minute, I believe it should add very low overhead. If you want the check to occur more or less often just adjust the time.sleep(60) statement to meet your needs.

Assuming you wired the cable to the LEDs correctly, you are plugged into COM1, and do have two tuners on /dev/video0 and /dev/video1 this script should run fine for you. The last bit to do is to make it being execution on system startup. To do this (using Redhat) I added a line to my "/etc/rc.d/rc.local"

# /root/checkrecording.py &

Enjoy! Kevin C. Dorff

Special thanks to Travis Kelley for posting the `fuser ...` stuff to the MythTV mailing list so I knew how to see if /dev/video0 or /dev/video1 was recording.


    • This nice script has the one slight problem, it is unable to differentiate between a 'live tv' display and a 'recording' display.

You should be able to do this. Try this instead: ls -l /proc/`fuser /dev/video0 | awk '{print $2}'`/fd/ 2>&1 | grep nuv$

This tells you if the process that is using /dev/video0, also has a file handle open to a file that ends in nuv. Give this a try and integrate into your script.

Scott Carlson


As someone pointed out on the mailing list -- another extremely low-tech way to do this, at least on a combined frontend/backend box, is to hook up a hard disk activity LED ;) -- Justin Mason