I just created this for my setup, perhaps it is useful for others:
required items:
- rpi3 with libreelec
- switch (normally open) between ports 5 and 6 of the GPIO pins
- led between ports 6 (or some other GND) and 8 of the GPIO pins (TX uart) (add 330 ohm resistor)
- install package adafruit-libraries
The uart is off by default, so you need to turn it on in the config.txt
mount -o rw,remount /flash
Edit /flash/config.txt`` and insert the following somewhere logical
dtoverlay=pi3-miniuart-bt
```
create the following files
- /storage/scripts/off-button.py
`
#!/usr/bin/python
#This script was authored by AndrewH7 and belongs to him (www.instructables.com/member/AndrewH7)
#You have permission to modify and use this script only for your own personal usage
#You do not have permission to redistribute this script as your own work#Use this script at your own risk
import sys
sys.path.append('/storage/.kodi/addons/virtual.rpi-tools/lib')
import RPi.GPIO as GPIO
import os
gpio_pin_number=3
#Replace YOUR_CHOSEN_GPIO_NUMBER_HERE with the GPIO pin number you wish to use
#Make sure you know which rapsberry pi revision you are using first
#The line should look something like this e.g. "gpio_pin_number=7"
GPIO.setmode(GPIO.BCM)
#Use BCM pin numbering (i.e. the GPIO number, not pin number)
#WARNING: this will change between Pi versions#Check yours first and adjust accordingly
GPIO.setup(gpio_pin_number, GPIO.IN, pull_up_down=GPIO.PUD_UP)
#It's very important the pin is an input to avoid short-circuits
#The pull-up resistor means the pin is high by default
try:
GPIO.wait_for_edge(gpio_pin_number, GPIO.FALLING)
#Use falling edge detection to see if pin is pulled
#low to avoid repeated polling
#os.system("shutdown -h now")
os.system("/storage/scripts/poweroff.sh")
#Send command to system to shutdown
except:
pass
GPIO.cleanup()
#Revert all GPIO pins to their normal states (i.e. input = safe)
Display More
- /storage/scripts/poweroff.sh
#!/bin/sh
echo "standby 0" | cec-client RPI -s -d 1 -p 0
shutdown -h now
Make both scripts executable:
chmod +x /storage/scripts/poweroff.sh /storage/scripts/off-button.py
Then make sure the off-button.py script is started when the rpi starts up:
- /storage/.config/autostart.sh
(
python /storage/scripts/off-button.py
) &
After rebooting the rpi3, it should work.
Thanks to AndrewH7 of instructables for the python script and everyone on the internet who wrote about adding a power button with or without LED to the rpi.
(The only thing I added/changed was the cec-client stuff via an external script)