- #REPETIER SERVER POWER SUPPLY SCRIPT HOW TO#
- #REPETIER SERVER POWER SUPPLY SCRIPT FULL#
- #REPETIER SERVER POWER SUPPLY SCRIPT CODE#
It is hard to tell that anything is really happening while you have the button pressed down.
#REPETIER SERVER POWER SUPPLY SCRIPT CODE#
The major thing lacking with the above code is any sort of feedback. If you look at the examples that come with the GPIO Zero source, you’ll find a script very similar called button_shutdown.py. The call to pause() is needed to cause the script to wait for the button presses. We then assign that event to the shutdown() function we defined earlier. This time, we are also specifying that the button must be held down, and when the hold time (6 seconds) has passed, any function assigned to the when_held event will be executed. The button is also assigned to a variable for use in the next statement. We then define a function named shutdown() to execute the poweroff system command. Secondly, it allows the GPIO number and hold time to be overridden on the command line, so that you can change them later without modifying the program. When making changes later, you won’t have to hunt through the code to find these variables. For a program this small, declaring the values at the top is not necessary, but it is good practice to declare any configurable variables near the top of the code. First, it defines variables to hold these numbers at the top of the code. Instead of hard-coding the GPIO number 21 and the hold time, this code does a few things differently. It would be better if you needed to hold the button down for several seconds before everything powers down. One major drawback of the previous code is that any accidental push of the button will shut your Raspberry Pi down. Now, after rebooting, your script will be running and listening for a button (connected between GPIO 21 on pin 40 and ground) to be pushed. # echo "~pi/shutdown-press-simple.py &" > /etc/rc.local Then add a line to the end of /etc/rc.local to run it at boot time: $ sudo su On a 26-pin header, GPIO 7 is similarly situated at the bottom, on pin 26, next to pin 25’s ground connection.Ĭreate the script on your Raspberry Pi using your favorite text editor (e.g., nano, Vim or Emacs), as in: $ nano ~pi/shutdown-press-simple.py This combination makes it difficult for an off-switch to be plugged in incorrectly. GPIO 21 is nice because it’s on pin 40 of the 40-pin header and sits right next to a ground connection on pin 39. This code creates a button on GPIO 21, waits for it to be pressed, then executes the system command to power down the Raspberry Pi. You can download it from the Git repo as shutdown-press-simple.py. Assuming your button is connected between GPIO 21 and GND (ground), the code is nice and easy.
With the GPIO Zero library, the Python code to deal with a button press becomes extremely simple.
Momentary push button switches Using GPIO Zero
#REPETIER SERVER POWER SUPPLY SCRIPT HOW TO#
Issue 52 of The MagPi featured an excellent article on how to connect a reset button. The safe off-switch is complementary to a reset switch, which is the best method for starting the Raspberry Pi up again. This article focuses on doing something useful with those switches and LEDs. Lots of articles are available to tell you how to use a breadboard to connect a button or LED to a Raspberry Pi’s GPIO pins.
#REPETIER SERVER POWER SUPPLY SCRIPT FULL#
The full article can be found in The MagPi 57 and was written by Tony Hansen. So, what can a self-respecting DIYer do? The answer, of course, is ‘add your own switch’! All the instructions say you should run the shutdown command before pulling the plug, but this is not always possible, particularly when your Raspberry Pi is running headless without a connected keyboard and monitor, and possibly even without a network connection. Unfortunately, this can lead to corruption problems with the SD card.
That’s OK, you say, we’ll just pull the plug to turn it off. To keep prices down, the Raspberry Pi is missing something that most electronic devices come with: a switch to turn it on and off.