├── ATXRaspi_R3_mm.dxf ├── README.md ├── rc.local ├── shutdowncheck-OLD ├── shutdownchecksetup.sh └── shutdownchecksetupOpenElec.sh /README.md: -------------------------------------------------------------------------------- 1 | ATXRaspi 2 | ========= 3 | 4 | ATXRaspi is a smart power controller for RaspberryPi that allows you to poweroff or reboot your Pi from [a momentary switch (with LED status)](https://lowpowerlab.com/shop/product/118). 5 | 6 | ## For the latest revision and details please see the [official ATXRaspi Guide](https://lowpowerlab.com/guide/atxraspi/). 7 | 8 | ### Overview and setup video (click to watch): 9 | [![ATXRaspi overview](https://farm8.staticflickr.com/7616/16572327060_3dd6c95d24.jpg)](http://www.youtube.com/watch?v=w4vSTq2WhN8) 10 | 11 | ### Wiring your Pi to [ATXRaspi](http://www.lowpowerlab.com/atxraspi) 12 | - GPIO7 (input to Pi) to outgoing arrow pin on ATXRaspi. 13 | - GPIO8 (output from Pi) goes to incoming arrow pin on ATXRaspi. 14 | - Connect power from output header (+/-) of ATXRaspi to GPIO power pins (5V/GND) on RaspberryPi (or straight through USB if you have that soldered). 15 | - Connect a momentary button and status LED to ATXRaspi (no resistor needed for LED). You can also get LED integrated momentary switches [from the LowPowerLab webshop](https://lowpowerlab.com/shop/LEDSwitch). 16 | 17 | ### Quick setup steps (raspbian): 18 | Log into your Pi an run the setup script, then remove it and reboot: 19 | - `sudo wget https://raw.githubusercontent.com/LowPowerLab/ATX-Raspi/master/shutdownchecksetup.sh` 20 | - `sudo bash shutdownchecksetup.sh` 21 | - `sudo rm shutdownchecksetup.sh` 22 | - `sudo reboot` 23 | 24 | ### For OpenElec and other details please see the [official guide](https://lowpowerlab.com/guide/atxraspi/). 25 | 26 | ### Enjoy shutting down and rebooting your Pi from the external button! 27 | -------------------------------------------------------------------------------- /rc.local: -------------------------------------------------------------------------------- 1 | #!/bin/sh -e 2 | # 3 | # rc.local 4 | # 5 | # This script is executed at the end of each multiuser runlevel. 6 | # Make sure that the script will "exit 0" on success or any other 7 | # value on error. 8 | # 9 | # In order to enable or disable this script just change the execution 10 | # bits. 11 | # 12 | # By default this script does nothing. 13 | 14 | # Print the IP address 15 | _IP=$(hostname -I) || true 16 | if [ "$_IP" ]; then 17 | printf "My IP address is %s\n" "$_IP" 18 | fi 19 | 20 | # The following line is required for ATXRaspi 21 | # to start the shutdowncheck script that will listen for a shutdown signal 22 | # and initiate a "sudo halt" command 23 | /etc/shutdowncheck & 24 | 25 | exit 0 26 | -------------------------------------------------------------------------------- /shutdowncheck-OLD: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | PATH=/usr/bin:/home/pi/wiringPi/gpio 4 | 5 | echo "ATXRaspi shutdown script starting..." 6 | echo "Asserting pins (7=in,low; 8=out,high)" 7 | 8 | gpio -g mode 7 in 9 | gpio -g write 7 0 10 | gpio -g mode 8 out 11 | gpio -g write 8 1 12 | 13 | while [ 1 ]; do 14 | if [ "$(/home/pi/wiringPi/gpio/gpio -g read 7)" = "1" ]; then 15 | echo "PIN7 requested a SYSTEM HALT !" 16 | sudo poweroff 17 | break 18 | fi 19 | /bin/sleep 0.5 20 | done 21 | 22 | exit 0 23 | -------------------------------------------------------------------------------- /shutdownchecksetup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | OPTION=$(whiptail --title "ATXRaspi/MightyHat shutdown/reboot script setup" --menu "\nChoose your script type option below:\n\n(Note: changes require reboot to take effect)" 15 78 4 \ 4 | "1" "Install INTERRUPT based script /etc/shutdownirq.py (recommended)" \ 5 | "2" "Install POLLING based script /etc/shutdowncheck.sh (classic)" \ 6 | "3" "Disable any existing shutdown script" 3>&1 1>&2 2>&3) 7 | 8 | exitstatus=$? 9 | if [ $exitstatus = 0 ]; then 10 | sudo sed -e '/shutdown/ s/^#*/#/' -i /etc/rc.local 11 | 12 | if [ $OPTION = 1 ]; then 13 | echo '#!/usr/bin/python 14 | # ATXRaspi/MightyHat interrupt based shutdown/reboot script 15 | # Script by Tony Pottier, Felix Rusu 16 | import RPi.GPIO as GPIO 17 | import os 18 | import sys 19 | import time 20 | 21 | GPIO.setmode(GPIO.BCM) 22 | 23 | pulseStart = 0.0 24 | REBOOTPULSEMINIMUM = 0.2 #reboot pulse signal should be at least this long (seconds) 25 | REBOOTPULSEMAXIMUM = 1.0 #reboot pulse signal should be at most this long (seconds) 26 | SHUTDOWN = 7 #GPIO used for shutdown signal 27 | BOOT = 8 #GPIO used for boot signal 28 | 29 | # Set up GPIO 8 and write that the PI has booted up 30 | GPIO.setup(BOOT, GPIO.OUT, initial=GPIO.HIGH) 31 | 32 | # Set up GPIO 7 as interrupt for the shutdown signal to go HIGH 33 | GPIO.setup(SHUTDOWN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) 34 | 35 | print("\n==========================================================================================") 36 | print(" ATXRaspi shutdown IRQ script started: asserted pins (",SHUTDOWN, "=input,LOW; ",BOOT,"=output,HIGH)") 37 | print(" Waiting for GPIO", SHUTDOWN, "to become HIGH (short HIGH pulse=REBOOT, long HIGH=SHUTDOWN)...") 38 | print("==========================================================================================") 39 | try: 40 | while True: 41 | GPIO.wait_for_edge(SHUTDOWN, GPIO.RISING) 42 | shutdownSignal = GPIO.input(SHUTDOWN) 43 | pulseStart = time.time() #register time at which the button was pressed 44 | while shutdownSignal: 45 | time.sleep(0.2) 46 | if(time.time() - pulseStart >= REBOOTPULSEMAXIMUM): 47 | print("\n=====================================================================================") 48 | print(" SHUTDOWN request from GPIO", SHUTDOWN, ", halting Rpi ...") 49 | print("=====================================================================================") 50 | os.system("sudo poweroff") 51 | sys.exit() 52 | shutdownSignal = GPIO.input(SHUTDOWN) 53 | if time.time() - pulseStart >= REBOOTPULSEMINIMUM: 54 | print("\n=====================================================================================") 55 | print(" REBOOT request from GPIO", SHUTDOWN, ", recycling Rpi ...") 56 | print("=====================================================================================") 57 | os.system("sudo reboot") 58 | sys.exit() 59 | if GPIO.input(SHUTDOWN): #before looping we must make sure the shutdown signal went low 60 | GPIO.wait_for_edge(SHUTDOWN, GPIO.FALLING) 61 | except: 62 | pass 63 | finally: 64 | GPIO.cleanup()' > /etc/shutdownirq.py 65 | sudo sed -i '$ i python /etc/shutdownirq.py &' /etc/rc.local 66 | elif [ $OPTION = 2 ]; then 67 | echo '#!/bin/bash 68 | # ATXRaspi/MightyHat interrupt based shutdown/reboot script 69 | # Script by Felix Rusu 70 | 71 | #This is GPIO 7 (pin 26 on the pinout diagram). 72 | #This is an input from ATXRaspi to the Pi. 73 | #When button is held for ~3 seconds, this pin will become HIGH signalling to this script to poweroff the Pi. 74 | SHUTDOWN=7 75 | REBOOTPULSEMINIMUM=200 #reboot pulse signal should be at least this long 76 | REBOOTPULSEMAXIMUM=600 #reboot pulse signal should be at most this long 77 | echo "$SHUTDOWN" > /sys/class/gpio/export 78 | echo "in" > /sys/class/gpio/gpio$SHUTDOWN/direction 79 | #Added reboot feature (with ATXRaspi R2.6 (or ATXRaspi 2.5 with blue dot on chip) 80 | #Hold ATXRaspi button for at least 500ms but no more than 2000ms and a reboot HIGH pulse of 500ms length will be issued 81 | #This is GPIO 8 (pin 24 on the pinout diagram). 82 | #This is an output from Pi to ATXRaspi and signals that the Pi has booted. 83 | #This pin is asserted HIGH as soon as this script runs (by writing "1" to /sys/class/gpio/gpio8/value) 84 | BOOT=8 85 | echo "$BOOT" > /sys/class/gpio/export 86 | echo "out" > /sys/class/gpio/gpio$BOOT/direction 87 | echo "1" > /sys/class/gpio/gpio$BOOT/value 88 | 89 | echo -e "\n==========================================================================================" 90 | echo " ATXRaspi shutdown POLLING script started: asserted pins ($SHUTDOWN=input,LOW; $BOOT=output,HIGH)" 91 | echo " Waiting for GPIO$SHUTDOWN to become HIGH (short HIGH pulse=REBOOT, long HIGH=SHUTDOWN)..." 92 | echo "==========================================================================================" 93 | 94 | #This loop continuously checks if the shutdown button was pressed on ATXRaspi (GPIO7 to become HIGH), and issues a shutdown when that happens. 95 | #It sleeps as long as that has not happened. 96 | while [ 1 ]; do 97 | shutdownSignal=$(cat /sys/class/gpio/gpio$SHUTDOWN/value) 98 | if [ $shutdownSignal = 0 ]; then 99 | /bin/sleep 0.2 100 | else 101 | pulseStart=$(date +%s%N | cut -b1-13) # mark the time when Shutoff signal went HIGH (milliseconds since epoch) 102 | while [ $shutdownSignal = 1 ]; do 103 | /bin/sleep 0.02 104 | if [ $(($(date +%s%N | cut -b1-13)-$pulseStart)) -gt $REBOOTPULSEMAXIMUM ]; then 105 | echo -e "\n=====================================================================================" 106 | echo " SHUTDOWN request from GPIO", SHUTDOWN, ", halting Rpi ..." 107 | echo "=====================================================================================" 108 | sudo poweroff 109 | exit 110 | fi 111 | shutdownSignal=$(cat /sys/class/gpio/gpio$SHUTDOWN/value) 112 | done 113 | #pulse went LOW, check if it was long enough, and trigger reboot 114 | if [ $(($(date +%s%N | cut -b1-13)-$pulseStart)) -gt $REBOOTPULSEMINIMUM ]; then 115 | echo -e "\n=====================================================================================" 116 | echo " REBOOT request from GPIO", SHUTDOWN, ", recycling Rpi ..." 117 | echo "=====================================================================================" 118 | sudo reboot 119 | exit 120 | fi 121 | fi 122 | done' > /etc/shutdowncheck.sh 123 | sudo chmod +x /etc/shutdowncheck.sh 124 | sudo sed -i '$ i /etc/shutdowncheck.sh &' /etc/rc.local 125 | fi 126 | 127 | echo "You chose option" $OPTION ": All done!" 128 | else 129 | echo "Shutdown/Reboot script setup was aborted." 130 | fi 131 | -------------------------------------------------------------------------------- /shutdownchecksetupOpenElec.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | echo '#!/bin/bash 3 | 4 | #This is GPIO 7 (pin 26 on the pinout diagram). 5 | #This is an input from ATXRaspi to the Pi. 6 | #When button is held for ~3 seconds, this pin will become HIGH signalling to this script to poweroff the Pi. 7 | SHUTDOWN=7 8 | REBOOTPULSEMINIMUM=200 #reboot pulse signal should be at least this long 9 | REBOOTPULSEMAXIMUM=600 #reboot pulse signal should be at most this long 10 | echo "$SHUTDOWN" > /sys/class/gpio/export 11 | echo "in" > /sys/class/gpio/gpio$SHUTDOWN/direction 12 | 13 | #Added reboot feature (with ATXRaspi R2.6 (or ATXRaspi 2.5 with blue dot on chip) 14 | #Hold ATXRaspi button for at least 500ms but no more than 2000ms and a reboot HIGH pulse of 500ms length will be issued 15 | 16 | #This is GPIO 8 (pin 24 on the pinout diagram). 17 | #This is an output from Pi to ATXRaspi and signals that the Pi has booted. 18 | #This pin is asserted HIGH as soon as this script runs (by writing "1" to /sys/class/gpio/gpio8/value) 19 | BOOT=8 20 | echo "$BOOT" > /sys/class/gpio/export 21 | echo "out" > /sys/class/gpio/gpio$BOOT/direction 22 | echo "1" > /sys/class/gpio/gpio$BOOT/value 23 | 24 | echo "ATXRaspi shutdown script started: asserted pins ($SHUTDOWN=input,LOW; $BOOT=output,HIGH). Waiting for GPIO$SHUTDOWN to become HIGH..." 25 | 26 | #This loop continuously checks if the shutdown button was pressed on ATXRaspi (GPIO7 to become HIGH), and issues a shutdown when that happens. 27 | #It sleeps as long as that has not happened. 28 | while [ 1 ]; do 29 | shutdownSignal=$(cat /sys/class/gpio/gpio$SHUTDOWN/value) 30 | if [ $shutdownSignal = 0 ]; then 31 | /bin/sleep 0.2 32 | else 33 | pulseStart=$(date +%s%N | cut -b1-13) # mark the time when Shutoff signal went HIGH (milliseconds since epoch) 34 | while [ $shutdownSignal = 1 ]; do 35 | /bin/sleep 0.02 36 | if [ $(($(date +%s%N | cut -b1-13)-$pulseStart)) -gt $REBOOTPULSEMAXIMUM ]; then 37 | echo "ATXRaspi triggered a shutdown signal, halting Rpi ... " 38 | poweroff 39 | exit 40 | fi 41 | shutdownSignal=$(cat /sys/class/gpio/gpio$SHUTDOWN/value) 42 | done 43 | #pulse went LOW, check if it was long enough, and trigger reboot 44 | if [ $(($(date +%s%N | cut -b1-13)-$pulseStart)) -gt $REBOOTPULSEMINIMUM ]; then 45 | echo "ATXRaspi triggered a reboot signal, recycling Rpi ... " 46 | reboot 47 | exit 48 | fi 49 | fi 50 | done' > /storage/.config/shutdowncheck.sh 51 | echo '#!/bin/bash 52 | ( 53 | /storage/.config/shutdowncheck.sh 54 | )&' > /storage/.config/autostart.sh 55 | chmod 777 /storage/.config/autostart.sh 56 | chmod 777 /storage/.config/shutdowncheck.sh --------------------------------------------------------------------------------