Configuring the GPIO pins for LEDs and switches

In this setup, there are two types of operating systems with different configuration rules. Assuming you have your power switches wired up and your LEDs ready to go with a resistor on the line, the information below will help you on your way to lighting up your own status LEDs.

Installing RPi.GPIO on the Raspbian devices

Getting the GPIO tools working in Raspian is straightforward. Assuming you already have python installed on your device, sudo pip install RPi.GPIO to install the module is pretty much all it takes.

Installing RPi.GPIO on the Rasplex device

With Rasplex running openELEC instead of Raspbian, just about everything is different and special, and this is no exception. To enable control over the GPIO pins, you’ll need to manually install the RPi.GPIO module. Start by downloading the module and moving the file to /storage/lib/ . Open an ssh session into the Pi and run tar -zxvf RPi.GPIO-0.6.3.tar.gz . Once this is set up, any scripts that need this module will work as long as the following text is included at the top:

import sys

sys.path.append('/storage/lib')

import RPi.GPIO as GPIO

Activity LEDs

The easiest LED to set up on a Pi is the blink-on-activity LED. This can be configured with a line in the config.txt file that can be found in /boot/ in Raspbian and /flash/ in Rasplex/OpenELEC. The text added to the file varies with which Pi you have due to some hardware differences between the Pi 3 and its predecessors.

If you’re running a Raspberry Pi 3, add dtoverlay=pi3-act-led,gpio=7 (with the gpio value equal to where the LED is connected) to the end of the config.txt file.. If you’re running a Pi 2 or below, use dtparam=act_led_gpio=7 instead.

The config.txt file can be edited directly on Raspbian machines with sudo nano /boot/config.txt , but to edit the Rasplex version (found in /flash/ ) you will need to plug the memory card into your PC and edit the file through a local text editor. This is due to the read-only nature of the OpenELEC environment.

Once the modifications have been saved, reboot the Pi and the connected LED will blink instead of the on-board LED. More options that can be set in config.txt can be explored via this documentation.

Temperature Monitor LEDs

Rasplex on the Pi 3 can run a little warm in some circumstances, so I wanted a way to keep an eye on the temperature, in case I needed to add a fan or other cooling. I decided to monitor the Plex server Pi as well, both in case it also needed cooling, but also as a comparison to the other Pi 3 in the case. I wrote this script which will repeatedly check the temperature of the Pi and blink two LEDs to indicate the temperature, each blink of the blue light is 10 degrees and each blink of the red light is 1 degree.

#! /usr/bin/python

import os

import time

import RPi.GPIO as GPIO



GPIO.setwarnings(False)

GPIO.setmode(GPIO.BCM)

PWR =15

HOT =5

NOT =13

GPIO.setup(PWR, GPIO.OUT)



GPIO.setup(HOT, GPIO.OUT)

HOT_LED = GPIO.PWM( HOT, 50)



GPIO.setup(NOT, GPIO.OUT)

NOT_LED = GPIO.PWM( NOT, 50)



GPIO.output(PWR,True)

temperature = os.popen("cat /sys/class/thermal/thermal_zone0/temp").readline()

i = int(temperature)

s = str(i)

blue = int(s[0])

red = int(s[1])



def blink_blue():

# print(blue)

time.sleep(.1)

NOT_LED.start(1)

time.sleep(.1)

NOT_LED.start(0)



def blink_red():

# print(red)

time.sleep(.1)

HOT_LED.start(1)

time.sleep(.1)

HOT_LED.start(0)



def led():

for _ in range(blue):

blink_blue()

for _ in range(red):

blink_red()

return



while True:

# print(f)

led();

time.sleep(2)

temperature = os.popen("cat /sys/class/thermal/thermal_zone0/temp").readline()

i = int(temperature)

s = str(i)

blue = int(s[0])

red = int(s[1])

This script also includes instructions to run a third LED as a power light. This can easily be commented out if it’s not needed. After creating the script file on your Pi, be sure to add a line executing it to /etc/rc.local to ensure it runs at boot (more on this below).

To get this running under Rasplex’s openELEC operating system, you’ll need to have followed the steps above and, use the Rasplex branch version of the script. You’ll need to create a file called /storage/.config/autostart.sh to start the script on boot. This is also covered further below.

Once this script is running, you can monitor the LED lights for the current temperature of your Pi at 10 degrees for every blink_blue() and 1 degree for every blink_red() .

Network status LEDs

As I mentioned above, I delegated a fourth Pi to drive these LEDs as there were various issues running a network LED on each of the FakeTV Pi units. Since the activity can’t be monitored directly (as it’s happening on a different device) I wrote this script which pings the requested IP and uses the ping result to determine the blink speed of the LED. The slower the blink, the slower the ping response. From the linked script hierarchy, I run ping.py , which runs three versions of the script, each configured to a different IP and GPIO. I set it up this way because running all from one script would only blink one LED at a time, this method blinks them all simultaneously. A command to run the script is added to /etc/rc.local as sudo python /home/pi/ping.py & to launch the script on boot.

Setting up the reset switches and running the scripts on boot

Due to the differences between Raspbian and openELEC, there are different software methods for operating the power switches and running scripts on boot. Also in the case, the openELEC shutdown script also runs the power LED, but for Raspbian units with an LED, the temperature monitor script from above also lights an LED on boot (you may notice in the Rasplex branch, those lines are commented out).

Rasplex

To set up the power button and LED with Rasplex, I followed these instructions. That thread includes a script to download, which is uncompressed into /storage/.plexht/addons/packages/gpio . The instuctions also have you create autostart.sh in /storage/.config/ and paste the following text

#!/bin/sh

(PYTHONPATH="${PYTHONPATH}:/storage/.plexht/addons/packages/gpio/lib";

export PYTHONPATH;

sleep 10s;

python ~/.plexht/addons/packages/gpio/bin/shutdown.py;) &

However, this autostart.sh doesn’t account for the temperature monitoring script that also needs to run on boot on this system. My autostart.sh looks like this:

#!/bin/sh

(PYTHONPATH="${PYTHONPATH}:/storage/.plexht/addons/packages/gpio/lib";

export PYTHONPATH;

sleep 10s;

python /storage/.plexht/addons/packages/gpio/bin/shutdown.py &

python /storage/bin/temp.py &

export PATH=$PATH:/storage/bin

export LD_LIBRARY_PATH=/storage/lib:$LD_LIBRARY_PATH

(modprobe lirc_rpi;)&

)

This will launch everything to control the power switch, power LED and temperature monitor LEDs when the unit boots. Since we’re using the GPIO pins for the switch that the Pi uses to recognize a ‘power on’ signal, pressing the button while the pi is off will start it, and pressing and holding it will signal it to shut down.

Raspbian (server and controller)

The server and controller both run Raspbian, which makes this much simpler. I used the script provided with these instructions. I created shutdown.py in the /home/pi directory, but you can put it in a subdirectory if you prefer.

#!/usr/bin/python

import RPi.GPIO as GPIO

import time

import subprocess # we will use the pin numbering to match the pins on the Pi, instead of the

# GPIO pin outs (makes it easier to keep track of things) GPIO.setmode(GPIO.BOARD) # use the same pin that is used for the reset button (one button to rule them all!)

GPIO.setup(5, GPIO.IN, pull_up_down = GPIO.PUD_UP) oldButtonState1 = True while True:

#grab the current button state

buttonState1 = GPIO.input(5) # check to see if button has been pushed

if buttonState1 != oldButtonState1 and buttonState1 == False:

subprocess.call("shutdown -h now", shell=True,

stdout=subprocess.PIPE, stderr=subprocess.PIPE)

oldButtonState1 = buttonState1 time.sleep(.1)

Then, to start at boot, sudo nano /etc/rc.local and add sudo python /home/pi/shutdown.py & to start the power switch script on boot and add sudo python /home/pi/temp.py & to start the temperature monitoring script (from above) on boot.

And that’s it, FakeTV hardware in a VCR casemod operating as a simulated television broadcast experience. It’s a criss-crossing combination of hardware, software and scripts running together so I can watch TV how I want to, and not how they want me to.