├── .github └── FUNDING.yml ├── .gitignore ├── LICENSE ├── README.md ├── gpio_layout.jpg ├── install.sh ├── power_button.py ├── power_button.service ├── raspberrypi-with-button.jpg └── uninstall.sh /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: fire1ce 4 | patreon: fire1ce 5 | # open_collective: # Replace with a single Open Collective username 6 | # ko_fi: # Replace with a single Ko-fi username 7 | # tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | # community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | # liberapay: # Replace with a single Liberapay username 10 | # issuehunt: # Replace with a single IssueHunt username 11 | # otechie: # Replace with a single Otechie username 12 | custom: ["https://www.paypal.com/donate/?hosted_button_id=Y5NSXQQ6VPSK6&Z3JncnB0", "https://www.buymeacoffee.com/fire1ce"] 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Node 2 | node_modules/ 3 | .package-lock.json 4 | 5 | # General 6 | .DS_Store 7 | .AppleDouble 8 | .LSOverride 9 | 10 | # Icon must end with two \r 11 | Icon 12 | 13 | # Thumbnails 14 | ._* 15 | 16 | # Files that might appear in the root of a volume 17 | .DocumentRevisions-V100 18 | .fseventsd 19 | .Spotlight-V100 20 | .TemporaryItems 21 | .Trashes 22 | .VolumeIcon.icns 23 | .com.apple.timemachine.donotpresent 24 | 25 | # Directories potentially created on remote AFP share 26 | .AppleDB 27 | .AppleDesktop 28 | Network Trash Folder 29 | Temporary Items 30 | .apdisk 31 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Stas Yakobov 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # Raspberry Pi Power Button - Wake/Power Off/Restart(Double Press) 3 | 4 | ## Information 5 | 6 | When Raspberry Pi is powered off, shortening GPIO3 (Pin 5) to ground will wake the Raspberry Pi. 7 | 8 | This script uses pin GPIO3(5), Ground(6) with momentary button. 9 | 10 | ![gpio layout](https://github.com/fire1ce/raspberry-pi-power-button/raw/main/gpio_layout.jpg) 11 | 12 | ![raspberrypi-with-button](https://github.com/fire1ce/raspberry-pi-power-button/raw/main/raspberrypi-with-button.jpg) 13 | 14 | ## Requirements 15 | 16 | * python3-gpiozero 17 | 18 | Can be install via apt 19 | 20 | ```bash 21 | sudo apt install python3-gpiozero 22 | ``` 23 | 24 | ## Install 25 | 26 | This will install the script as `service` and it will run at boot 27 | 28 | ```bash 29 | curl https://raw.githubusercontent.com/fire1ce/raspberry-pi-power-button/main/install.sh | bash 30 | ``` 31 | 32 | ## Uninstall 33 | 34 | ```bash 35 | curl https://raw.githubusercontent.com/fire1ce/raspberry-pi-power-button/main/uninstall.sh | bash 36 | ``` 37 | 38 | ## Default Behavior 39 | 40 | | __Button Press (Raspberry Pi is ON)__ | __Behavior__ | 41 | | ----------------------------------------- | ------------ | 42 | | Single | Nothing | 43 | | Double | Reboot | 44 | | Long press and releases (above 3 seconds) | Power off | 45 | 46 | | __Button Press (Raspberry Pi is OFF)__ | __Behavior__ | 47 | | -------------------------------------- | ------------ | 48 | | Single | Power On | 49 | 50 | ## Check if service is running 51 | 52 | ```bash 53 | sudo systemctl status power_button.service 54 | ``` 55 | -------------------------------------------------------------------------------- /gpio_layout.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fire1ce/raspberry-pi-power-button/c81417a38492867c9cf882e69b40d4acadd3c3c3/gpio_layout.jpg -------------------------------------------------------------------------------- /install.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | curl -O https://raw.githubusercontent.com/fire1ce/raspberry-pi-power-button/main/power_button.py 4 | curl -O https://raw.githubusercontent.com/fire1ce/raspberry-pi-power-button/main/power_button.service 5 | 6 | if [ ! -d "/usr/local/bin" ]; then 7 | sudo mkdir -p /usr/local/bin 8 | fi 9 | 10 | sudo chmod +x power_button.py 11 | sudo mv power_button.py /usr/local/bin 12 | sudo mv power_button.service /etc/systemd/system 13 | sudo systemctl start power_button.service 14 | sudo systemctl enable power_button.service 15 | 16 | echo "done." 17 | -------------------------------------------------------------------------------- /power_button.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | from datetime import datetime, timedelta 3 | from time import sleep 4 | from signal import pause 5 | from gpiozero import Button 6 | import os 7 | 8 | use_button = 3 # pin 5 9 | held_for = 0.0 10 | Button.pressed_time = None 11 | 12 | 13 | def released(): 14 | global held_for 15 | if (held_for > 3.0): 16 | print("Shuting down...") 17 | os.system("shutdown now -h") 18 | else: 19 | held_for = 0.0 20 | 21 | 22 | def pressed(button): 23 | if button.pressed_time: 24 | if button.pressed_time + timedelta(seconds=1) > datetime.now(): 25 | print("rebooting...") 26 | os.system("reboot") 27 | button.pressed_time = None 28 | else: 29 | # print("pressed once") # debug 30 | button.pressed_time = datetime.now() 31 | 32 | 33 | def held(): 34 | global held_for 35 | held_for = max(held_for, button.held_time + button.hold_time) 36 | 37 | 38 | button = Button(use_button, hold_time=1.0, hold_repeat=True) 39 | button.when_held = held 40 | button.when_released = released 41 | button.when_pressed = pressed 42 | 43 | pause() 44 | -------------------------------------------------------------------------------- /power_button.service: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description=GPIO power button 3 | After=network.target 4 | 5 | [Service] 6 | Type=simple 7 | Restart=always 8 | RestartSec=1 9 | User=root 10 | ExecStart=/usr/bin/python3 /usr/local/bin/power_button.py 11 | 12 | [Install] 13 | WantedBy=multi-user.target 14 | -------------------------------------------------------------------------------- /raspberrypi-with-button.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fire1ce/raspberry-pi-power-button/c81417a38492867c9cf882e69b40d4acadd3c3c3/raspberrypi-with-button.jpg -------------------------------------------------------------------------------- /uninstall.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | sudo systemctl disable power_button.service 4 | sudo systemctl stop power_button.service 5 | sudo rm -rf /etc/systemd/system/power_button.service 6 | sudo rm -rf /usr/local/bin/power_button.py 7 | 8 | echo "done." 9 | --------------------------------------------------------------------------------