This tutorial is for owners of the Plusberry Pi case designed for the Raspberry Pi. The goal is to setup the shutdown button to work with LibreELEC. If you are running OpenELEC, you can follow the original tutorial provided by the Plusberry Pi team here. No copyright intended. I simply added a few steps that are crucial to setting this up in LibreELEC. In summary, we will be creating the shutdown.sh, adding the script from the original guide, and finally adding the script to the autostart.sh.
Connecting the wires
Basically, you can use any available input, but this example will use pin #24. There is a connector with 3 wires (blue/black/green) coming from the left of the Plusberry board (it’s labeled “UART”). Connect the BLUE wire to pin #24. (see image below)
When you press the power button (short press), the GPIO will go from high to low, signaling the RPi to shut down. Now, of course, you need to write a script to actually shut down the RPi when getting the signal.
SSH into LibreELEC
Open up your ssh/telnet client to begin. Putty is a great utility for Windows users. For Mac, the built-in Terminal app is sufficient.
ssh root@insert.ip.address.here
When asked if you are sure type the following
yes
When asked for a password type the following
libreelec
Creating shutdown.sh
Change directory by typing the following
cd /storage/.config/
Create the file by typing the following
nano shutdown.sh
Copy and Paste the following in the empty space
#!/bin/bash
# monitor GPIO pin 24 (wiringPi pin 1) for shutdown signal
# export GPIO pin 24 and set to input with pull-up
echo "24" > /sys/class/gpio/export
echo "in" > /sys/class/gpio/gpio24/direction
# wait for pin to go low
while [ true ]
do
if [ "$(cat /sys/class/gpio/gpio24/value)" == '0' ]
then
echo "Raspberry Pi Shutting Down!"
halt &
exit 0
fi
sleep 1
done
Save the file according to the program you’re using
On Mac, you simply press Control + X on your keyboard
Say yes by typing the following
y
Leave the filename default
Should be named “shutdown.sh”
Press enter
Add the script to autostart.sh
Type the following
nano /storage/.config/autostart.sh
Paste the following in the empty space
(
/storage/.config/shutdown.sh &
)&
Save the file according to the program you’re using
Say yes by typing the following
y
Leave the filename default
Should be named “autostart.sh”
Press enter
Reboot the system and test it out!
What does the wiring diagram look like for a Model B which has a smaller GPIO header? I could use GPIO #4 for the shutdown button and then GPIO 5, the 3.3V pin and the ground pin for the IR Sensor. What about the other 2 wires. What are those used for?
The diagram is the same. Just ignore the extra pins on the image above. Your model appears to have 26 pins so just stop there. If pin #4 works then by all means use that. Pin #24 is just what they suggested. The Plusberry team did not specify what those extra wires are for. I just left them alone to the side. Everything seems to work without them.
For some reason this stops my pi from rebooting correctly? It hangs on a black screen?
I am not sure, I just copied the steps from the OpenELEC tutorial and changed a few things. Everything else is the same. I didn’t create the script so I have no idea how to help.
Good afternoon. I followed all the steps according to the instructions, only replaced GPIO24 with GPIO3. But the switch does not work. RPI 3 Libreelec 8.0.2 On which version of LE does this script work?
Hi. Thanks for the post. I had to do a couple of modifications to adapt the code for my PI. Otherwise was not working properly.
1. The autostart.sh above was not working for me. Instead I used the following query for the autostart.sh
#!/bin/bash
(sh /storage/.config/shutdown.sh)&
exit
2. I needed to used GPIO03 (SLC1 or Pin#05) instead of GPIO24. Just replaced the 24’s for 5’s in the shutdown.sh (switch “24” to “5” and “/gpio24/” to “/gpio5/”).
I’m using Libreelec 8.0.2. Hope this info helps someone as the scripts above helped me.
BR
Thank you! But still does not work. Could you send me your files autostrt.sh and shutdown.sh. Maybe I’m typing the wrong code ..
> I needed to used GPIO03 (SLC1 or Pin#05) instead of GPIO24. Just replaced the 24’s for 5’s in the shutdown.sh (switch “24” to “5” and “/gpio24/” to “/gpio5/”).
Are you sure? It’s pin5 but gpio3. In the original example it was pin 18 and gpio24. But it was always “24” not “18”. So don’t you have to use “3” and not “5”?
This didn’t work for me, so I pieced together something that did. Here is my rudimentary guide.
As LibreELEC’s system is generally a read-only system by default, you will need to get
those write-permissions first:
mount -o remount,rw /flash
Nano is a very easy-to-use built-in text editor:
nano /flash/config.txt
Scroll down to the bottom using the ARROW keys and Enter the following at the bottom of
the script:
dtoverlay=gpio-shutdown
Save the file according to the program you’re using ie ctrl+o, ctrl+x
Now you have to change back the read/write permissions of the system partition to ‘read-only’ again:
mount -o remount,ro /flash
This will call the shutdown menu when Pin 5 & 6 (GPIO 3 and Ground) are connected/shorted. Now we change the keymapping so the shutdown menu automatically selects shutdown without any extra key presses/menu clicks.
Edit the keymap file:
nano /storage/.kodi/userdata/keymaps/keyboard.xml
Enter the following code into the empty space:
shutdown
Finally, you need to reboot for the changes in the config.txt to take effect:
reboot
Libreelec 9.2.1 will now shut off and go to sleep properly when pins 5 & 6 are
connected and start up again when those same pins are connected again.
“Edit the keymap file:
nano /storage/.kodi/userdata/keymaps/keyboard.xml
Enter the following code into the empty space:
shutdown”
Hey there, thanks for the help so far, but putting shutdown into keyboard.xml does not result in the shutdown menu being skipped when I push the button.
When I push the power button now, it just turns the shutdown menu on and off.
Is there something missing from the keyboard.xml command you put down?
I don’t have this Plusberry case thing but did it with a standard button.
I tried various options to run Python and Shell scripts on startup and, although it worked in some cases, the scripts seemed to interfere with other processes and made the system pretty unstable.
I found the best way was indeed to add the device tree overlay to the config file as described above.
In the keymap file, you’ll need to write valid XML code though – just writing “shutdown” on its own won’t work.
This ought to do it:
shutdown
I tried various Python and Shell scripts on startup but they seemed to interfere with other processes and made the system pretty unstable.
The device tree overlay option as described above worked for me.
In the keymap file, you need to write valid XML though – just writing “shutdown” won’t work.
This ought to do it:
shutdown