Getting Safe Shutdown/Reset working in OSMC for the newest NESPi Case+ w/safe shutdown modules

Just a repost from my blog since my blog… well. it doesn’t get much traffic, hehe… I’ve been a huge fan of the NesPi cases on amazon w/the power and reset buttons however the v1.0 of the case did not allow for a safe shutdown out of the box (only way was to add a module, solder, create a kit). However, v2.0 in form of the NesPi Case+ (NesPi Case+ (no referrals to me at all) has the actual module added to allow for safe shutdown and reboot. i also prefer to run OSMC as main OS and retropie as an ‘add-on’ via a script to add it to OSMC since i use my pi for plex/osmc more than retropie but like having the option. :wink:

I bought the case and after receiving it found out that there were ZERO scripts for it for OSMC. Only scripts the company had posted were for RetroFlag and RetroPie distros for the Pi. Also, these scripts did not work at all on OSMC since OSMC is based on a diff distro of linux. Moving forward, I ran into a post about using GPIO on OSMC to get a basic button working and in turn have used said notes to get the NesPi Case+ buttons working properly in OSMC. This also will make the LED work too in OSMC, led is using the serial port gpio and in turn just enabling UART makes it work… i thought i somehow shorted my LED but realized in non-safe shutdown mode it worked fine and in other os’s too it did. :wink:

Here are the steps using win10, putty and ssh into pi:

#1. Install RPi.GPIO. Open up Putty and run these commands in putty:

sudo su
apt-get update
apt-get install python-pip python-dev gcc
apt-get install python-setuptools
pip install rpi.gpio

#1a. Enable root to make things easier, you can disable afterwards, type and then enter a root password:

sudo passwd

#2. In Putty, create a python script for push button shutdown:

sudo nano /home/osmc/shutdown.py

#2a. Add to the file the following (copy all up until #2b). Once done hold CTRL+X to save, Y and enter:

#!/usr/bin/python
import RPi.GPIO as GPIO
import os

#hide warning
GPIO.setwarnings(0)

#connect RPi numbered pin
button = 5

#hide warning
GPIO.setwarnings(0)

#pins setup
GPIO.setmode(GPIO.BOARD)
GPIO.setup(button, GPIO.IN, pull_up_down=GPIO.PUD_UP)

#wait for button press
print(‘Wait for button press…’)
GPIO.wait_for_edge(button, GPIO.FALLING)

activate system command
Updated to make true safe shutdown w/emustation (incase OSMC has been added)
os.system(‘sudo killall emulationstation & sleep 5s & sudo shutdown -h now’)

#2b. In putty, type this command to change permission:

chmod +x /home/osmc/shutdown.py

#3. In putty, type this command to create a systemd service file:

sudo nano /lib/systemd/system/shutdown.service

#3a. Now add to the service file the below. Once done hold CTRL+X to save, Y and enter:

[Unit]
Description = GPIO shutdown button
[Service]
Type = idle
ExecStart = /home/osmc/shutdown.py
[Install]
WantedBy = multi-user.target

#4. In putty, type this command to enable auto startup:

sudo systemctl enable shutdown

#Connect a push button to RPi GPIO pin #56
#Start the service without reboot:

sudo systemctl start shutdown

#5a FINALLY, to enable the power on LED, we need to add a line to the /boot/config.txt file to enable the serial port
#This case uses the UART port for the led.

sudo nano /boot/config.txt

#5b, add this line of text to the end of config.txt and then CTRL+X and Y and enter:

enable_uart=1

##Press the power button to shutdown OSMC safely!
##Be sure you have toggled the safe mode switch on the pcb to ON
##Once you start up your Pi the LED will also be enabled! updated on 5/6/18

#------------------------------------------------

#You can make the reset button reboot OSMC by doing the following (basically same steps as above but different names and button):

#1. Create a python script for push button shutdown:

sudo nano /home/osmc/reset.py

#2a. Add to the file. Once done hold CTRL+X to save, Y and enter:

#!/usr/bin/python
import RPi.GPIO as GPIO
import os

#hide warning
GPIO.setwarnings(0)

#connect RPi numbered pin
button = 3

#hide warning
GPIO.setwarnings(0)

#pins setup
GPIO.setmode(GPIO.BOARD)
GPIO.setup(button, GPIO.IN, pull_up_down=GPIO.PUD_UP)

#wait for button press
print(‘Wait for button press…’)
GPIO.wait_for_edge(button, GPIO.FALLING)

#activate system command
#reboots OSMC
os.system(‘sudo reboot’)

#2b. Change permission:

chmod +x /home/osmc/reset.py

#3. Create a systemd service file:

sudo nano /lib/systemd/system/resetbutton.service

#3a. Add to the service file. Once done hold CTRL+X to save, Y and enter:

[Unit]
Description = GPIO reset button
[Service]
Type = idle
ExecStart = /home/osmc/reset.py
[Install]
WantedBy = multi-user.target

#4. Enable auto startup, type in putty:

sudo systemctl enable resetbutton

##Connect a push button to RPi GPIO pin #56
Start the service without reboot, type in putty:

sudo systemctl start resetbutton


github gist:

original blog post:

1 Like

This works perfectly, however the fan keeps spinning and the standby light stays on. So it doesn’t completely shut down as it does in Retropie.

This script also works on Raspbian but has the same result -> No complete shutdown. Also the ‘sudo killall emulationstation’ can be removed since it’s useless in OSMC/Raspbian.

This is great. Thanks for sharing. I too would love to see an updated version that turns off the fan.

1 Like

Anyone figure out how to get the fan to stop on shutdown?
Figured it out, made a [HowTo] post with my way of getting Safe Shutdown working

1 Like