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 riskimport RPi.GPIO as GPIO
import osgpio_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 accordinglyGPIO.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 defaulttry:
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:
passGPIO.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.