Code Monkey home page Code Monkey logo

bye-pi's Introduction

Raspberry Pi Shutdown Button

Add a physical shutdown button to a Raspberry Pi. This is particularly useful for a Raspberry Pi running in headless mode, since yanking out the power cord is not a safe option for shutdown. Choose between C, Python, or UNIX shell script implementations.

Requirements

Hardware Requirements

  • 1 Raspberry Pi
  • 1 normally-open momentary pushbutton switch
  • 2 jumper wires with female connector (only need if not already attached to switch)

If you need to connect the pushbutton switch with the jumper wires, you will also need a soldering iron and solder.

Software Requirements

  • For bye_pi.py:
    sudo apt install python3-gpiozero
  • For bye_pi.c and bye_pi.sh:
    sudo apt-get install wiringpi

NOTE: The project was tested on a Raspberry Pi 3 Model B running the Raspbian Buster Lite OS.

Hardware Setup

If the switch did not already come with jumper wires attached, solder two jumper wires to the switch. The two jumper wires should have female connectors on one end, so they can be attached to the GPIO pins on the Raspberry Pi.

alt text

GPIO Pins

There is more than one pin cominbation available for connecting the pushbutton switch. The code in this project uses physical pins 39 and 40, so unless you modify the code accordingly, stick with those pins. Keep in mind that the physical pin numbers are different from the BCM pin numbers, which are also different from the wiringPi pin numbers. The best way to visualize the relationship between the three different numbering systems is to use the gpio readall command.

alt text

Additionally, the pinout command shows each pins' physical orientation on the board as well as other useful information.

alt text

Software Setup (Choose One)

The setup instructions assume you have the script in your /home/pi directory, but if you have it elsewhere just provide the actual path to the file instead of /home/pi.

bye_pi.py

Make the Python script executable.
chmod +x bye_pi.py

Place the script in rc.local so it runs at boot. To do so, edit the rc.local file:
sudo nano /etc/rc.local
Add the following above exit 0:
home/pi/bye_pi.py &

Alternatively, you could use cron instead of rc.local to schedule the script to run at boot.
crontab -e
@reboot /home/pi/bye_pi.py &

bye_pi.c

Compile the C code to executable code.
gcc -Wall bye_pi.c -o bye_pi -l wiringPi

Place the program in rc.local so it runs as boot. To do so, edit the rc.local file:
sudo nano /etc/rc.local
Add the following above exit 0:
home/pi/bye_pi &

Alternatively, you could use cron instead of rc.local to schedule the script to run at boot.
crontab -e
@reboot /home/pi/bye_pi &

bye_pi.sh

Make the bash script executable.
chmod +x bye_pi.sh

Place the script in rc.local so it runs at boot. To do so, edit the rc.local file:
sudo nano /etc/rc.local
Add the following above exit 0:
home/pi/bye_pi.sh &

Alternatively, you could use cron instead of rc.local to schedule the script to run at boot.
crontab -e
@reboot /home/pi/bye_pi.sh &

Usage

Connect the pushbutton switch to the physical pins 39 and 40. It is best to connect the switch when the Raspberry Pi is powered off.

alt text

Power on the Raspberry Pi. The script should already be running via rc.local or cron. Simply push the button to power off the Pi.

bye-pi's People

Contributors

fierceoj avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

5l1v3r1

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.