Raspberry Pi Media Player – Part 2, LibreELEC/Kodi Software

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

Install LibreELEC (Kodi)

Updated for LibreELEC v12 / Kodi v21

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 shutdown the Pi and pressing the button for a longer duration to reboot the Pi.

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 the Settings icon from the main menu, then click “Add-ons”, 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 gpiozero as gpio
import os
import time

SHUTDOWNPULSEMINIMUM = 0.2  #shutdown pulse signal should be at least this long (seconds)
SHUTDOWNPULSEMAXIMUM = 1.0  #shutdown pulse signal should be at most this long (seconds) -
                            #longer pulse indicates reboot
SHUTDOWN = 24               #GPIO used to signal shutdown to the Pi
BOOTINDICATOR = 23          #GPIO used to indicate Pi has booted up

# gpiozero library sets up a pipe so we need to be in a writable directory
os.chdir("/tmp")

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

# 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)
button = gpio.Button(SHUTDOWN, bounce_time=0.05, hold_time=0.0)

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

try:

    while True:
        button.wait_for_press()
        pulseStart = time.time() #register time at which the button was pressed
        print("button pressed")
        while button.is_pressed: #loop while button pressed
            time.sleep(0.1)
            print(".")
            if(time.time() - pulseStart >= SHUTDOWNPULSEMAXIMUM):
                # button held longer than shutdown, so assume reset is intended 
                print("\n==================================================================")
                print(" REBOOT request from GPIO", SHUTDOWN, ", recycling Rpi ...")
                print("==================================================================")
                os.system("reboot")
                sys.exit()
        print("button released")
        # button released - see if button was held long enough for a shutdown
        if time.time() - pulseStart >= SHUTDOWNPULSEMINIMUM:
            print("\n==================================================================")
            print(" SHUTDOWN request from GPIO", SHUTDOWN, ", halting Rpi ...")
            print("==================================================================")
            os.system("poweroff")
            sys.exit()

except:
    pass

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 shutdown or hold for reboot
==========================================================================================

and the power indicator LED should light up. Press and hold 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. For more information on running commands when Kodi is starting up or shutting down see 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. If the LED fails to come on there may been an issue with the autostart.sh file. You can get some information to help debug the issue by reconnecting to the Pi using SSH and typing:

systemctl status kodi-autostart.service

Test that the button works to reboot the Pi by pressing and holding it – after a second, Kodi will indicate that it is shutting down, then the Pi will reboot and the Kodi UI should reappear. Test the power button by giving it a quick press – the Pi should cleanly close down Kodi and the 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/