zoola.jpg

header.png


Home
IWH Software
My projects
Retro stuff
Random stuff
About me
Contact me
Admin login




Adding the reset/shutdown button and script to a Raspberry Pi LibreELEC install

Looking for instructions to add a button to Raspbian or Retropie instead?

Whilst it's easy enough to add a reset/shutdown button to the Raspberry Pi when it's running a debian based operating system like Raspbian or Retropie, it becomes a little more difficult when attempting to install the button on LibreELEC (Kodi)

But it can be done...

The biggest problem comes from not having access to the usual Raspbian repositories and apt-get, so there's no way to access the RPi.GPIO library as you would normally do so.

However within the LibreELEC repositories you can install the Pi Tools repository that contains RPi.GPIO, gpiozero, and picamera. Also you will need a slightly different script to what you would normally use, and start it within a LibreELEC script rather than using rc.local.

For this guide I'm using LibreELEC 8.0.2 which is/was the latest version available. YMMV on later versions.



Hardware set-up

Find yourself a momentary switch that's normally open, it'll have two terminals on it, there are literally 1000's available to choose from, so you'll be able to find one that fits in nicely with your existing installation.

The two terminals will need to connect to ground and GPIO17, in any recent Pi these are the fifth and sixth pins down on the left header row. You will either need to solder wires to the switch, then to the board on the Zero (unpopulated header), or use normal jumper wires if the header already has pins soldered in (populated header)

Once the switch is connected it won't actually do anything at the moment because you need to create a script that listens for the GPIO17 pin to go to ground, then react to it, either by restarting, or shutting down depending on how long the button is held down for.

Software installation and set-up.

Start by going to the Add-ons section on the main Kodi screen
Select Install from repository at the top
Select LibreELEC add-ons
Select Program add-ons
Select Raspberry Pi Tools
Select Install
Wait for the Add-on installed message in the top right

That's the RPi.GPIO library installed.

Now it's time to install the script

You may notice that you will log into the Pi as root, so sudo isn't necessary for any of the commands.

Make sure the SSH server within LibreELEC is enabled (Settings, LibreELEC, Services, Enable SSH)
Then SSH into the Pi with the IP address you get from Kodi settings (Settings, System information)
Use root for the username, and libreelec for the password

Then navigate to Kodi's home directory with this command...

cd /storage/.kodi/userdata

Then run this command...

nano button.sh

Type this out, make sure the indentations are correct or it won't work.

#!/usr/bin/python

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)
GPIO.setup(17, GPIO.IN, pull_up_down = GPIO.PUD_UP)

print "Starting button.sh..."

pressed_time = 0
button = 0
while button == 0:
  time.sleep(0.50)
  while GPIO.input(17) == False:
     time.sleep(1)
     pressed_time=pressed_time + 1
     button = 1
     if pressed_time == 3:
        break

if pressed_time<2:
  os.system("reboot")
elif pressed_time>=2:
  os.system("shutdown now")

Save with CTRL+O, then exit with CTRL+X

Now run this command to make it executable

chmod +x button.sh

Now, to make it automatically start at boot run this...

nano /storage/.config/autostart.sh

This will open a blank screen, then type this out...

#!/bin/sh

python /storage/.kodi/userdata/button.sh &

Save it with CTRL+O, then exit with CTRL+X.

Then make it executable with this command...

chmod +x /storage/.config/autostart.sh

Now reboot to make all this magical tomfoolery work

 reboot

Now when you press the button momentarily the Pi will reboot, and if you hold the button down it'll shutdown.


© 2019 IWH Software (Ian Hill)