Hello,
I am trying to install a power switch on a RP4 device and here is the list of the steps I took manually, if you wish to have a go. It is based on various instructions, mainly
here to setup the switch and
here to install the library. I have the switch detected and I can access it by the python console but I am still looking at an efficient way to start the script during the boot sequence, as systemd does not seem to work.
Anyway, to install the tool you need to access GPIO from python scripts you can do the following. Start by the packages:
Terminal
sudo apt-get install python-dev python3-dev
sudo apt-get install mercurial
sudo apt-get install python-pip python3-pip
sudo apt-get install python3-setuptools
sudo apt-get install gcc-arm-linux-gnueabihf
Then you should be able to install the GPIO library. The version in the repository does not work for the raspberry pi versions higher than 1 so you need to install from the source repo:
Terminal
python3 -m pip install wheel
python3 -m pip install hg+http://hg.code.sf.net/p/raspberry-gpio-python/code#egg=RPi.GPIO
Apparently there is a kernel bug that prevents accessing the GPIO library without being root (see
here and
here), so you need to create a rule.d config:
Quote:Create /etc/udev/rules.d/90-gpio.rules with:
KERNEL=="gpiomem", OWNER="root", GROUP="gpio"
Create the group itself and assign it to an existing user "pi":
Then add a new group to manage the GPIO port and add the user to it:
Terminal
sudo groupadd -f --system gpio
sudo usermod -a -G gpio $USER
Note that this worked to some extent for me but I still encountered some issues when not using root, so I still use sudo for all the rest.
Once you have these done you can write your python script to switch off the device, I got mine from
there with some modifications to make it operate on the correct GPIO pin (pin 5, or equivalently GPIO3):
Code:
#!/usr/bin/env python3
import signal
import sys
import os
import RPi.GPIO as GPIO
BUTTON_GPIO = 3
def signal_handler(sig, frame):
GPIO.cleanup()
sys.exit(0)
def button_pressed_callback(channel):
print("Button pressed!")
os.system('poweroff')
if __name__ == '__main__':
GPIO.setmode(GPIO.BCM)
GPIO.setup(BUTTON_GPIO, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(BUTTON_GPIO, GPIO.FALLING,
callback=button_pressed_callback, bouncetime=100)
signal.signal(signal.SIGINT, signal_handler)
signal.pause()
When running this code from the terminal, the power switch operated correctly and switched off the device. Using pin 5 also makes it possible to use the same switch to power up the RP4.
I only need to put this script in the boot sequence now, I will try init.r (there are some instructions
here).
I hope this helps.
Just to update, init.d seems to be working, the steps are
here (method 3).
Add a header to the file:
Code:
#!/usr/bin/env python3
# /etc/init.d/shutdown_interrupt.py
### BEGIN INIT INFO
# Provides: shutdown_interrupt.py
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start shutdown daemon at boot time
# Description: Enable to shutdown the RP4 using the switch on GPIO5-6.
### END INIT INFO
import signal
import sys
import os
import RPi.GPIO as GPIO
BUTTON_GPIO = 3
def signal_handler(sig, frame):
GPIO.cleanup()
sys.exit(0)
def button_pressed_callback(channel):
print("Button pressed!")
os.system('poweroff')
if __name__ == '__main__':
GPIO.setmode(GPIO.BCM)
GPIO.setup(BUTTON_GPIO, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(BUTTON_GPIO, GPIO.FALLING,
callback=button_pressed_callback, bouncetime=100)
signal.signal(signal.SIGINT, signal_handler)
signal.pause()
then save it at /etc/init.d/, make it accessible and update the system:
Terminal
sudo cp ./shutdown_interrupt.py /etc/init.d/
sudo chmod +x sample.py
sudo update-rc.d sample.py defaults
It is now working for me, I have a start/stop switch on my RP4
happy times....