Raspberry Pi Media Player – Part 2, Software

Continuing on from Part 1 of the Raspberry Pi Media Player setup.

Install LibreELEC

LibreELEC is a nice open source media player distribution that uses the Kodi (originally XBMC) media player with just enough Linux to make it work. The result is a lightweight system that boots very quickly and runs quite well on lower end embedded systems such as the Raspberry Pi.

Download LibreELEC and install onto a micro SD card following the directions on the LibreElec site, then put the card in the Pi. Hook the Pi up to the network via the panel mounted ethernet connector and plug it into a TV/monitor via the HDMI port. Plug the power supply into the barrel connector for the moment of truth – something like the following should be on the screen:

You will probably have to run through a setup wizard to customize its network name and connection – make sure to enable the service SSH. After you have done that you will need to install the Python GPIO library – click on the Settings icon, select “Add-ons” and then “Program add-ons”. In the list that appears, select “Raspberry Pi Tools” and click “Install”.

Enable IR Support

Assuming that you are using an infra red receiver component connected to a GPIO pin as described in Part 1 (for a USB receiver, consult the LibreELEC wiki), you will need to enable GPIO infra red support.

For this step you will need to use your computer and a program that supports SSH (secure shell access). On Windows you can use Putty, on MacOS or Linux just open a terminal window and type the following:

ssh root@libreelec.local

when it prompts you for the password, enter libreelec . Note that by default the hostname will be “libreelec” but your local network domain may not be “.local”, so you may need to try the following instead:

ssh root@libreelec

After gaining SSH access to the Pi, enable infra red remote support by editing the file at /flash/config.txt :

mount -o remount,rw /flash
nano /flash/config.txt

and replace the line:

dtoverlay=lirc-ir

with (if the above line isn’t present, just make sure the following is present):

dtoverlay=gpio-ir

Save the file, then remount the partition as read only:

mount -o remount,ro /flash

By default LibreElec expects the IR sensor to be Windows Media Center compatible on GPIO# 18 (which is pin #12 on the GPIO connector), so no other settings should be necessary. If you are using a different type of remote then consult the LibreElec wiki for setting the infra red protocol type.

Test the remote works by clicking the up down arrows and making sure you can control Kodi. If you have issues, then the LibreELEC wiki has some good information on how to get it working. For my particular setup using a Windows MCE style remote, I made sure not to enable “Lirc” in the LibreELEC > Services section of the Settings.

Power Button and Indicator

Rather than just turning off the power to the Pi I wanted to have an intuitive way to shut it down safely. I also wanted to be able to reboot the Pi without pulling the power connector. Since I didn’t want to add a second button, the goal was to have a single short press of the button reset the Pi and pressing the button for a longer duration to power the Pi down.

When the button on my case was pressed originally, it pressed on a momentary push switch mounted on a circuit board behind the button. In Part 1 of this guide I retained the circuit board but wired the switch to GPIO #24 (pin 18 on the GPIO header) on the Pi and the other side of the switch to ground, so that pushing the switch will take GPIO #24 low. In Part 1 I also connected an LED as a power/running indicator to GPIO #23 (pin 16 on the GPIO header) and the other side of the LED to ground.

Now we need some code to make the button and LED do what we want. First, using the remote control configured in the above step we install an add on to enable use of the Raspberry Pi GPIO – select “Add-ons” from the main menu, then “Install from repository” > “LibreELEC Add-ons” > “Program add-ons” then select “Raspberry Pi Tools” and install it.

Next, using SSH on your computer connect to the Pi and type the following to use nano to create a new Python file:

nano /storage/.config/shutdownirq.py

Now paste in the following Python code:

#!/usr/bin/python
# Shutdown/reboot script with power indicator
# NOTE: requires RPi Tools to be installed

import sys
sys.path.append("/storage/.kodi/addons/virtual.rpi-tools/lib")
import RPi.GPIO as GPIO
import os
import time

GPIO.setmode(GPIO.BCM)
pulseStart = 0.0
REBOOTPULSEMINIMUM = 0.2    #reboot pulse signal should be at least this long (seconds)
REBOOTPULSEMAXIMUM = 1.0    #reboot pulse signal should be at most this long (seconds)
SHUTDOWN = 24               #GPIO used to signal shutdown to the Pi
BOOTINDICATOR = 23          #GPIO used to indicate Pi has booted up

# Set up GPIO BOOTINDICATOR as an output and indicate that the Pi has
# booted up by setting it HIGH (LED on)
GPIO.setup(BOOTINDICATOR, GPIO.OUT, initial=GPIO.HIGH)

# Set up GPIO SHUTDOWN as input for the shutdown signal, but with
# internal pull up as normally HIGH (pulse LOW for reboot, hold LOW
# for shutdown)
GPIO.setup(SHUTDOWN, GPIO.IN, pull_up_down=GPIO.PUD_UP)

print "\n=========================================================================================="
print " Shutdown IRQ script started."
print " Waiting for GPIO", SHUTDOWN, " to pulse for reboot or hold for shutdown"
print "=========================================================================================="

try:

    while True:
        GPIO.wait_for_edge(SHUTDOWN, GPIO.FALLING)
        shutdownSignal = GPIO.input(SHUTDOWN)
        pulseStart = time.time() #register time at which the button was pressed
        while not shutdownSignal:
            time.sleep(0.2)
            if(time.time() - pulseStart >= REBOOTPULSEMAXIMUM):
                print "\n====================================================================================="
                print " SHUTDOWN request from GPIO", SHUTDOWN, ", halting Rpi ..."
                print "====================================================================================="
                os.system("poweroff")
                sys.exit()
            shutdownSignal = GPIO.input(SHUTDOWN)
        if time.time() - pulseStart >= REBOOTPULSEMINIMUM:
            print "\n====================================================================================="
            print " REBOOT request from GPIO", SHUTDOWN, ", recycling Rpi ..."
            print "====================================================================================="
            os.system("reboot")
            sys.exit()
        if not GPIO.input(SHUTDOWN): #before looping we must make sure the shutdown signal went low
            GPIO.wait_for_edge(SHUTDOWN, GPIO.RISING)

except:
    pass

finally:
    GPIO.cleanup()

Save it and exit nano, then make the script executable:

chmod 755 /storage/.config/shutdownirq.py

Test the script by running it:

/storage/.config/shutdownirq.py

You should not see any errors, just something like:

==========================================================================================
 Shutdown IRQ script started.
 Waiting for GPIO 24  to pulse for reboot or hold for shutdown
==========================================================================================

and the power indicator LED should light up. Press the power button and you should see something like:

=====================================================================================
 REBOOT request from GPIO 24 , recycling Rpi ...
=====================================================================================

Your SSH session should terminate because the Pi should be rebooting. Reconnect to the Pi using SSH.

Now we have to tell the Pi to load the script at boot up – create autostart.sh:

nano /storage/.config/autostart.sh

and enter the following shell script command:

#!/bin/bash
(
/storage/.config/shutdownirq.py
)&

Save it and exit nano, then set the script’s permissions so that it is executable:

chmod 755 /storage/.config/autostart.sh

The autostart.sh script allows commands to be run on boot up before Kodi starts – more information can be found on the LibreELEC wiki.

Use the remote control to make LibreELEC reboot. As the Pi starts back up the power indicator LED will be off for a second or two and then turn on before the Kodi UI appears on the TV.

Test the power button by giving it a quick press – the Pi should cleanly close down Kodi and then reboot. Test that the button works to power down the Pi by pressing and holding it – after a second, Kodi will indicate that it is shutting down and a few seconds later the power indicator LED should turn off. You can now safely unplug the Pi!

Finish Setting Up LibreElec

Now that we have our customizations working, finish setting up LibreELEC by setting the locale (Settings > Interface > Regional), etc using the LibreELEC wiki.

References

Infra Red Remotes on LibreELEC:
https://wiki.libreelec.tv/infrared_remotes#gpio_ir_receiver_on_rpi

Autostart programs on LibreELEC:
https://wiki.libreelec.tv/autostart.sh
https://lowpowerlab.com/forum/atxraspi/libreelec-python-script/