Trying to use GPIO pin to shutdown Pi

Hi all,

I’m trying to install a Paspberry Pi Zero in my car running OSMC. I need a way to safely shut it down when the ignition is turned off.

I’m using a usb phone charger 5V battery pack to provide a few minutes of power after the 12V car power is removed (this battery pack can happily charge and supply power at the same time). I’ll be connecting a GPIO pin to ground using a relay form the car’s 12V system to trigger the shutdown command while on usb battery pack power.

I followed this article, but struggled: http://www.instructables.com/id/Simple-Raspberry-Pi-Shutdown-Button/

I tried moving the python script from /home/osmc to /etc/init.d as some forms mentioned that the /home/osmc directory may not be mounted early enough during the boot sequence.

This is the contents of /etc/init.d/pi-power-off.py

(I had to change the location of python in the first line)

NOTE: Script here is now edited to show corrected syntax!

#!/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 RPi.GPIO as GPIO
import os

gpio_pin_number=21
#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(“sudo shutdown -h now”)
#Send command to system to shutdown
except:
pass

GPIO.cleanup()
#Revert all GPIO pins to their normal states (i.e. input = safe)

My /etc/rc.local file’s contents:

#!/bin/sh -e

#rc.local

#This script is executed at the end of each multiuser runlevel.
#Make sure that the script will “exit 0” on success or any other
#Value on error.

#In order to enable or disable this script just change the execution
#bits.

#By default this script does nothing.

python /etc/init.d/pi-power-off.py

exit 0

On raspberry pi startup, the screen displays:

[FAILED] Failed to start /etc/rc.local Compatibility.
See ‘systemctl status rc-local.service’ for details.

So, having a look:

osmc@osmc:~$ systemctl status rc-local.service

  • rc-local.service - /etc/rc.local Compatibility
    Loaded: loaded (/lib/systemd/system/rc-local.service; static)
    Active: failed (Result: exit-code) since Fri 2018-01-26 16:48:04 GMT; 2min 23s ago
    Process: 241 ExecStart=/etc/rc.local start (code=exited, status=1/FAILURE)

Hmmph…

When I try to manually run the python script to see if it will work, I get this:

osmc@osmc:/etc/init.d$ python pi-power-off.py
Traceback (most recent call last):
File “pi-power-off.py”, line 8, in
import RPi.GPIO as gpio
ImportError: No module named RPi.GPIO

I did an update and tried installing RPi.GPIO, but had no luck:

osmc@osmc:/etc/init.d$ sudo apt-get -y install python-rpi.gpio
Reading package lists… Done
Building dependency tree
Reading state information… Done
E: Unable to locate package python-rpi.gpio
E: Couldn’t find any package by regex ‘python-rpi.gpio’

Any ideas? or is there a better way to do this such as cron?

Thanks, Scott.

I’ll only address the install of rpi.gpio for now (short of time right now).

From memory, so it might not be 100% correct :wink:

sudo apt-get update
sudo apt-get install python-dev python-pip build-essential
sudo pip install setuptools
sudo pip install rpi.gpio

Thank you, that seemed to work nicely to install RPi.gpio

Now I tried running the script, and got a slightly different result:

osmc@osmc:/etc/init.d$ python pi-power-off.py
Traceback (most recent call last):
File “pi-power-off.py”, line 8, in
import RPi.gpio as GPIO
ImportError: No module named gpio

I guess there’s something wrong in the python script?

Turns out I had a lower case gpio where upper case GPIO was needed, but now a different issue:

osmc@osmc:/etc/init.d$ python pi-power-off.py
Traceback (most recent call last):
File “pi-power-off.py”, line 21, in
GPIO.setup(gpio_pin_number, GPIO.IN, pull_up_down=GPIO.PUD_UP)
RuntimeError: No access to /dev/mem. Try running as root!

I’ve edited the script in the opening post to show it correct now.

WOOHOOO!!!

I now have one switch to shut it down, and another to start it up!

ACE!!!

It works!!!

I’ll document all of this on my website!

Cheers, Scott.

He - PIctures!!! :slight_smile: We wanna see it :smiley:

Please remember to post the URL here!

Currently it’s a pile of bits… but being installed in a roof-mounted sunglasses storage flap!

Hoping to have it done by the end of the weekend!

  • RPi Zero
  • HifiBerry DAC
  • 12V to 5V DC-DC converter (chopped up car cigarette lighter USB phone charger)
  • USB battery powered phone charger / power pack
  • USB wireless dongle OSMC official remote
  • Relay to short a GPIO pin to ground when 12V switches off (ie: keys removed form ignition)
  • A 2-way toggle switch sprung to the centre (off) position (SPDT, on-off-on); one way for start up (shorting RST to 0V), the other for shut down (shorting GPIO pin to 0V), so that only one thing can be done at a time.

I’ve had to use a different GPIO pin, as the HifiBerry uses 21 (pin 40)! I’m now using GPIO 22 (pin 15).

Making good progress! Got the HifiBerry working too.

Hi,

Here’s a little video showing it powering on and off. It was late at night and my brain wasn’t quite firing on all cylinders, but it’s good to see it working!

3 Likes

Nice!