From 3de223e8d1fae23a99b4825ff40562ce1dbbec85 Mon Sep 17 00:00:00 2001 From: N6RDV <97379462+N6RDV@users.noreply.github.com> Date: Mon, 9 May 2022 20:56:25 -0700 Subject: [PATCH 01/13] Add long press delay Add long_press delay to prevent inadvertent shutdowns. Variable long_press can be set to number of 0.1 s intervals (10 for 1 s, 30 for 3s, etc.), defining how long user have to keep button pressed to shutdown Raspberry Pi. If set to 0 - button press would execute shutdown command immediately --- listen-for-shutdown.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/listen-for-shutdown.py b/listen-for-shutdown.py index cecc42b..0a1f5ab 100755 --- a/listen-for-shutdown.py +++ b/listen-for-shutdown.py @@ -1,12 +1,27 @@ #!/usr/bin/env python - import RPi.GPIO as GPIO import subprocess +import time +from time import sleep +gpio_pin=3 +long_press=30 GPIO.setmode(GPIO.BCM) -GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_UP) -GPIO.wait_for_edge(3, GPIO.FALLING) +GPIO.setup(gpio_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) + +def main(): + while True: + GPIO.wait_for_edge(gpio_pin, GPIO.FALLING) + counter = 0 + while GPIO.input(gpio_pin) == 0 and counter < long_press: + time.sleep(0.1) + counter += 1 + print('Shutdown button is pressed ' + str(counter)) + if counter >= long_press: + print('Shutting down') + subprocess.call(['shutdown', '-h', 'now'], shell=False) -subprocess.call(['shutdown', '-h', 'now'], shell=False) +if __name__ == '__main__': + main() From 6846a9dab79e085a211059689dc0c62a958b859c Mon Sep 17 00:00:00 2001 From: N6RDV <97379462+N6RDV@users.noreply.github.com> Date: Mon, 9 May 2022 21:05:50 -0700 Subject: [PATCH 02/13] Update listen-for-shutdown.py --- listen-for-shutdown.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/listen-for-shutdown.py b/listen-for-shutdown.py index 0a1f5ab..7fb483b 100755 --- a/listen-for-shutdown.py +++ b/listen-for-shutdown.py @@ -6,7 +6,7 @@ from time import sleep gpio_pin=3 -long_press=30 +long_press=0 GPIO.setmode(GPIO.BCM) GPIO.setup(gpio_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) From 465bcafa350e5c44ee40c5f95a70c50d0cb248f4 Mon Sep 17 00:00:00 2001 From: N6RDV <97379462+N6RDV@users.noreply.github.com> Date: Mon, 9 May 2022 21:10:26 -0700 Subject: [PATCH 03/13] Update listen-for-shutdown.py --- listen-for-shutdown.py | 1 + 1 file changed, 1 insertion(+) diff --git a/listen-for-shutdown.py b/listen-for-shutdown.py index 7fb483b..e725544 100755 --- a/listen-for-shutdown.py +++ b/listen-for-shutdown.py @@ -1,5 +1,6 @@ #!/usr/bin/env python + import RPi.GPIO as GPIO import subprocess import time From b8507874aeae2bd68595ae43038aaded6a81569c Mon Sep 17 00:00:00 2001 From: N6RDV <97379462+N6RDV@users.noreply.github.com> Date: Tue, 27 Aug 2024 14:39:44 -0700 Subject: [PATCH 04/13] Update listen-for-shutdown.py --- listen-for-shutdown.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/listen-for-shutdown.py b/listen-for-shutdown.py index e725544..c588a2c 100755 --- a/listen-for-shutdown.py +++ b/listen-for-shutdown.py @@ -6,23 +6,33 @@ import time from time import sleep -gpio_pin=3 -long_press=0 +button_gpio=3 +relay_gpio=4 + +short_press=0.5 +long_press=10 GPIO.setmode(GPIO.BCM) -GPIO.setup(gpio_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) +GPIO.setup(button_gpio, GPIO.IN, pull_up_down=GPIO.PUD_UP) +GPIO.setup(relay_gpio, GPIO.OUT) def main(): + relay_state=False + GPIO.output(relay_gpio, relay_state) while True: - GPIO.wait_for_edge(gpio_pin, GPIO.FALLING) + GPIO.wait_for_edge(button_gpio, GPIO.FALLING) counter = 0 - while GPIO.input(gpio_pin) == 0 and counter < long_press: + while GPIO.input(button_gpio) == 0 and counter < long_press: time.sleep(0.1) - counter += 1 + counter = round(counter + 0.1, 1) print('Shutdown button is pressed ' + str(counter)) if counter >= long_press: print('Shutting down') subprocess.call(['shutdown', '-h', 'now'], shell=False) + elif counter >= short_press: + relay_state = not relay_state + print('Changing relay state to: ' + str(relay_state)) + GPIO.output(relay_gpio, relay_state) if __name__ == '__main__': main() From b244c3de87e72e5f77cb9e8f182ae7b5b37ed77d Mon Sep 17 00:00:00 2001 From: N6RDV <97379462+N6RDV@users.noreply.github.com> Date: Tue, 27 Aug 2024 14:43:28 -0700 Subject: [PATCH 05/13] Update listen-for-shutdown.py --- listen-for-shutdown.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/listen-for-shutdown.py b/listen-for-shutdown.py index c588a2c..172fa6c 100755 --- a/listen-for-shutdown.py +++ b/listen-for-shutdown.py @@ -9,8 +9,8 @@ button_gpio=3 relay_gpio=4 -short_press=0.5 -long_press=10 +short_press=0.1 +long_press=5 GPIO.setmode(GPIO.BCM) GPIO.setup(button_gpio, GPIO.IN, pull_up_down=GPIO.PUD_UP) @@ -33,6 +33,7 @@ def main(): relay_state = not relay_state print('Changing relay state to: ' + str(relay_state)) GPIO.output(relay_gpio, relay_state) + time.sleep(0.5) if __name__ == '__main__': main() From 76ebf78a0f0644a3c617801f1131484e391fcf0e Mon Sep 17 00:00:00 2001 From: N6RDV <97379462+N6RDV@users.noreply.github.com> Date: Tue, 27 Aug 2024 14:45:37 -0700 Subject: [PATCH 06/13] Update listen-for-shutdown.py --- listen-for-shutdown.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/listen-for-shutdown.py b/listen-for-shutdown.py index 172fa6c..c13406e 100755 --- a/listen-for-shutdown.py +++ b/listen-for-shutdown.py @@ -17,7 +17,7 @@ GPIO.setup(relay_gpio, GPIO.OUT) def main(): - relay_state=False + relay_state=True GPIO.output(relay_gpio, relay_state) while True: GPIO.wait_for_edge(button_gpio, GPIO.FALLING) From d96786f40a9d6595d2b0262160261e017e78d9b0 Mon Sep 17 00:00:00 2001 From: N6RDV <97379462+N6RDV@users.noreply.github.com> Date: Tue, 27 Aug 2024 14:57:58 -0700 Subject: [PATCH 07/13] Update listen-for-shutdown.py --- listen-for-shutdown.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/listen-for-shutdown.py b/listen-for-shutdown.py index c13406e..263cc86 100755 --- a/listen-for-shutdown.py +++ b/listen-for-shutdown.py @@ -17,8 +17,7 @@ GPIO.setup(relay_gpio, GPIO.OUT) def main(): - relay_state=True - GPIO.output(relay_gpio, relay_state) + relay_state=GPIO.input(relay_gpio) while True: GPIO.wait_for_edge(button_gpio, GPIO.FALLING) counter = 0 From 6c141f618afb227dfc85421e6b4ef74fd9ad8f2a Mon Sep 17 00:00:00 2001 From: N6RDV <97379462+N6RDV@users.noreply.github.com> Date: Wed, 19 Nov 2025 23:56:54 -0800 Subject: [PATCH 08/13] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e31d232..56341f6 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Scripts used in our official [Raspberry Pi power button guide](https://howchoo.c ## Installation 1. [Connect to your Raspberry Pi via SSH](https://howchoo.com/g/mgi3mdnlnjq/how-to-log-in-to-a-raspberry-pi-via-ssh) -1. Clone this repo: `git clone https://github.com/Howchoo/pi-power-button.git` +1. Clone this repo: `git clone https://github.com/N6RDV/pi-power-button.git` 1. Optional: Edit line 9/10 in listen-for-shutdown.py to your preferred pin (Please see "Is it possible to use another pin other than Pin 5 (GPIO 3/SCL)?" below!) 1. Run the setup script: `./pi-power-button/script/install` From 883953dcbb46c2d9b838d9cbffdc5c469f62c801 Mon Sep 17 00:00:00 2001 From: N6RDV <97379462+N6RDV@users.noreply.github.com> Date: Thu, 20 Nov 2025 00:04:38 -0800 Subject: [PATCH 09/13] Update listen-for-shutdown.py --- listen-for-shutdown.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/listen-for-shutdown.py b/listen-for-shutdown.py index 263cc86..4d61801 100755 --- a/listen-for-shutdown.py +++ b/listen-for-shutdown.py @@ -7,17 +7,19 @@ from time import sleep button_gpio=3 -relay_gpio=4 +light_gpio=27 +fan_gpio=27 short_press=0.1 -long_press=5 +long_press=10 GPIO.setmode(GPIO.BCM) GPIO.setup(button_gpio, GPIO.IN, pull_up_down=GPIO.PUD_UP) -GPIO.setup(relay_gpio, GPIO.OUT) +GPIO.setup(light_gpio, GPIO.OUT) +GPIO.setup(fan_gpio, GPIO.OUT) def main(): - relay_state=GPIO.input(relay_gpio) + relay_state=GPIO.input(light_gpio) or GPIO.input(fan_gpio) while True: GPIO.wait_for_edge(button_gpio, GPIO.FALLING) counter = 0 @@ -31,7 +33,8 @@ def main(): elif counter >= short_press: relay_state = not relay_state print('Changing relay state to: ' + str(relay_state)) - GPIO.output(relay_gpio, relay_state) + GPIO.output(light_gpio, relay_state) + GPIO.output(fan_gpio, relay_state) time.sleep(0.5) if __name__ == '__main__': From 7a107e5b46c64a7bd17cb954cb282adb3db3ca1e Mon Sep 17 00:00:00 2001 From: N6RDV <97379462+N6RDV@users.noreply.github.com> Date: Thu, 20 Nov 2025 00:14:12 -0800 Subject: [PATCH 10/13] Update listen-for-shutdown.py --- listen-for-shutdown.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/listen-for-shutdown.py b/listen-for-shutdown.py index 4d61801..d933901 100755 --- a/listen-for-shutdown.py +++ b/listen-for-shutdown.py @@ -8,7 +8,7 @@ button_gpio=3 light_gpio=27 -fan_gpio=27 +fan_gpio=22 short_press=0.1 long_press=10 From 257538d24458fa47e2ee385d8dc1cc54f92d62d4 Mon Sep 17 00:00:00 2001 From: N6RDV <97379462+N6RDV@users.noreply.github.com> Date: Sat, 22 Nov 2025 22:51:02 -0800 Subject: [PATCH 11/13] Update listen-for-shutdown.py --- listen-for-shutdown.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/listen-for-shutdown.py b/listen-for-shutdown.py index d933901..c3b3ef2 100755 --- a/listen-for-shutdown.py +++ b/listen-for-shutdown.py @@ -19,7 +19,6 @@ GPIO.setup(fan_gpio, GPIO.OUT) def main(): - relay_state=GPIO.input(light_gpio) or GPIO.input(fan_gpio) while True: GPIO.wait_for_edge(button_gpio, GPIO.FALLING) counter = 0 @@ -27,6 +26,9 @@ def main(): time.sleep(0.1) counter = round(counter + 0.1, 1) print('Shutdown button is pressed ' + str(counter)) + + relay_state=GPIO.input(light_gpio) or GPIO.input(fan_gpio) + if counter >= long_press: print('Shutting down') subprocess.call(['shutdown', '-h', 'now'], shell=False) From 6a903edc2688bd06f6c3da5fd018fa6a385b08a6 Mon Sep 17 00:00:00 2001 From: N6RDV <97379462+N6RDV@users.noreply.github.com> Date: Sat, 22 Nov 2025 22:54:55 -0800 Subject: [PATCH 12/13] Update listen-for-shutdown.py --- listen-for-shutdown.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/listen-for-shutdown.py b/listen-for-shutdown.py index c3b3ef2..18caaf9 100755 --- a/listen-for-shutdown.py +++ b/listen-for-shutdown.py @@ -26,14 +26,11 @@ def main(): time.sleep(0.1) counter = round(counter + 0.1, 1) print('Shutdown button is pressed ' + str(counter)) - - relay_state=GPIO.input(light_gpio) or GPIO.input(fan_gpio) - if counter >= long_press: print('Shutting down') subprocess.call(['shutdown', '-h', 'now'], shell=False) elif counter >= short_press: - relay_state = not relay_state + relay_state = not (GPIO.input(light_gpio) or GPIO.input(fan_gpio)) print('Changing relay state to: ' + str(relay_state)) GPIO.output(light_gpio, relay_state) GPIO.output(fan_gpio, relay_state) From 20d07293004a3a1c1e7ba0d2fb77a598f11dcbc9 Mon Sep 17 00:00:00 2001 From: N6RDV <97379462+N6RDV@users.noreply.github.com> Date: Sat, 22 Nov 2025 22:57:17 -0800 Subject: [PATCH 13/13] Update listen-for-shutdown.py --- listen-for-shutdown.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/listen-for-shutdown.py b/listen-for-shutdown.py index 18caaf9..02fd623 100755 --- a/listen-for-shutdown.py +++ b/listen-for-shutdown.py @@ -34,7 +34,7 @@ def main(): print('Changing relay state to: ' + str(relay_state)) GPIO.output(light_gpio, relay_state) GPIO.output(fan_gpio, relay_state) - time.sleep(0.5) + time.sleep(short_press) if __name__ == '__main__': main()