Thursday, January 11, 2018

LIBREELEC RPI2/3 Physical Power Off Button


There are many tutorials about installing a Power Off button in the Raspberry Pi 2/3 with Raspbian but there aren’t many about installing one if you use LIBREELEC so I decided to make this small tutorial.

First you need to know we will be using gpiozero, to install it you need to go to Add-ons / [Box on top] / Install from repository / LibreELEC Add-ons / Program add-ons / Raspberry Pi Tools and activate the Raspberry Pi Tools application.

We will make a very basic gpiozero script, I will include a link in the bottom to a website that has more advanced scripts.

This is the gpiozero script you need, just paste the text into a text editor:

import sys
sys.path.append('/storage/.kodi/addons/virtual.rpi-tools/lib')

#!/usr/bin/env python3
from gpiozero import Button
import os
Button(21).wait_for_press()
os.system("shutdown now")

Save the previous script as shutdown-switch.py and save it into the /storage folder of LibreELEC.

If you have trouble finding the folder you can use any FTP client (like FileZilla) to find it, it's inside the root folder (/) on the top of the hierarchy.

Then you need to run the script at boot, you can do it any way you want, I'll show you one way of doing it with this script (just paste this into a text editor):

(
 python storage/shutdown-switch.py;
) &

Now save it as autostart.sh into the /storage/.config folder of LIBREELEC (\\LIBREELEC\Configfiles for samba users)

Reboot LibreELEC with the "reboot now" command.

Test if the script is running, you can test it with this command:

ps -eaf | grep shutdown-switch.py | grep -v grep

If you can see the “shutdown-switch.py” script on the list, it means that it is running.
Similar to this: (648 root       0:14 python storage/shutdown-switch.py)

Disclaimer: It is unsafe to short pins in the Raspberry Pi, you must configure them first in order to be able to short them safely. This method shorts two pins on the Raspberry Pi but there is another method that uses a pull-up resistor, that method is supposed to be the ideal and safest way of doing it. Here is a link if you want to check it out.

Pull-up resistor:
https://grantwinney.com/using-pullup-and-pulldown-resistors-on-the-raspberry-pi/

I use the shorting method because it's simple and gpiozero supports it, if you do it like this you may want to use a resistor (like a 1K) for extra security in case the switch is pushed while the script isn't loaded (like during boot or if you reinstall LIBREELEC or Raspbian and forget to run the script).

It should be safe to short the pins now. You need a momentary switch of course; you need to connect it to the pins 39 and 40 (or GPIO 21 and GND).

Here you have an image of the pinout:



Press the switch once and that should do it, your Raspberry Pi should shutdown with the press of a button. You may want to program a push delay in the script to avoid accidental pushes, there are other things you can program with gpiozero, if you are interested check the link below.

Switch tutorial (for Raspbian) and more commands:
https://github.com/TonyLHansen/raspberry-pi-safe-off-switch

Update: the newest version of Kodi "Leia", 8.95.1 beta doesn't seem to run gpiozero correctly. So if you update please have in mind that the switch script may not run (note that this may be dangerous, specially if you have not added a resistor or a pull-up resistor), apparently the developers need to update gpiozero, so for now don't expect it to work, once the final version of Leia is released gpiozero may work, if not you can contact the develpers through the libreelec forums.

If you decide to try other way of implementing a switch you could try using dtoverlay which is a more official way of doing it, more info here: https://www.stderr.nl/Blog/Hardware/RaspberryPi/PowerButton.html and here: https://raspberrypi.stackexchange.com/questions/77905/raspberry-pi-3-model-b-dtoverlay-gpio-shutdown

1 comment:

Anonymous said...

Nice solution, but if you change GPIO 21 to GPIO 3 than you can turn on the raspberry with the same button.