This project creates a startup script as a Daemon, hence it can be started and stopped at command line.
It uses a Python script to run on startup which is called by an init script which goes in the /etc/init.d folder. Reference to Debian documentaton.
Creating the Python scriptThe Python script (in this example below) calls for a shutdown command when the GPIO pin is pulled to low on an interrupt. Test the script before doing going further. Make the script executable (e.g. chmod 755 TestScript.py).
Creating the init scriptUse example below as a template. At line #13-16, Change the path to where your Python script is (in example /home/pi). Change the name of the script to whatever the.py file is. Give the service a name.Copy script to /etc/init.d
Make script executable (chmod 755 TestService.sh).
Run the command sudo update-rc.d TestService.sh defaults
Start the Python script using: sudo /etc/init.d/TestService.sh start
Check its status with: sudo /etc/init.d/TestService.sh status
Stop it with: sudo /etc/init.d/TestService.sh stop
Note if running non raspbian distro:GPIO commands need to work so they need to be enabled.On OSMC the following allows GPIO:
sudo apt-get update
sudo apt-get install python-pip python-dev gcc
sudo pip install rpi.gpio
Further reading and troubleshooting.
Note: re the code below, copy and paste in a new file and then save the file on your PI. Something to do with character formatting which changes things during download.
Edit 01/2022 the RPi-Gpio needs to be downloaded separately as a Zip then extract and run natively.
Get the package from - http://pypi.python.org/pypi/RPi.GPIO
wget https://pypi.python.org/packages/source/R/RPi.GPIO/RPi.GPIO-0.X.XX.tar.gz
Unzip it with: tar -xvf RPi.GPIO-0.X.XX.tar.gz
Goto the Directory: cd RPi.GPIO-0.X.XX
Run: sudo python setup.py install
Optionally remove the Directory
cd ~
sudo rm -rf RPi.GPIO-0.*
Comments
Please log in or sign up to comment.