Adding the reset/shutdown button and script to a Raspberry Pi LibreELEC installLooking 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-upFind 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 screenSelect Install from repository at the topSelect LibreELEC add-onsSelect Program add-onsSelect Raspberry Pi ToolsSelect InstallWait for the Add-on installed message in the top rightThat's the RPi.GPIO library installed.Now it's time to install the scriptYou 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 passwordThen navigate to Kodi's home directory with this command...
#!/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")
chmod +x button.sh
#!/bin/sh python /storage/.kodi/userdata/button.sh &
chmod +x /storage/.config/autostart.sh