howchoo / pi-power-button Goto Github PK
View Code? Open in Web Editor NEWScripts useful for the Pi power button
License: MIT License
Scripts useful for the Pi power button
License: MIT License
Cloning the repo:
git clone https://github.com/Howchoo/pi-power-button.git
Running the following gets an error saying that the file or folder does not exist:
./pi-power-button/script/install
(unfortunately the text is in swedish, so ive translated it in the title of the issue)
looking in the folder with "ls" i can see the install file:
And this is on Raspberry Pi OS Lite (32-bit) running on a Raspberry Pi 4 Model B.
Several users have asked how to uninstall the power button because they need to use GPIO3 for another project; right now, uninstall must be done manually.
We should write a script to automatically uninstall things.
Hi,
I'm generally new to the Pi and it's features.
What I try to do is to insert a subprocess before the shutdown, to turn the tv off.
What I tried:
Create a monitor_off.sh-file with command: echo 'standby 0' | cec-client -s -d 1
Edit listen-for-shutdown-file to:
subprocess.call("sh monitor_off.sh", shell=True)
time.sleep(5)
subprocess.call(['shutdown', '-h', 'now'], shell=False)
Yet even if I delete the shutdown entirely, the Pi just reboots somehow.
Do you have a better Idea or a fix for my problem?
I've been using your script for a while now and I really like how clean it is. I've used it on every Pi setup I've done so far (7 total for various around the house projects and for friends) and it has worked flawlessly!
Anyway I recently set up a Octoprint for my Maker Select Plus and installed a button on the side of my case. I have accidentally tapped the button while loading and unloading filament several times. I was looking to add a wait/time delay so that a long press would trigger the script. There are other tutorials out there but i really like what I've got with your script. Also many of these tutorials offer a reboot on a short press/shutdown on a long press. I am not a professional coder, changed majors from computer science way back in 1999 so it is all quite rusty.
So really I'd like your opinion on wether the modified code below would actually work or what you would do to change it. It's hacked together from other sources and modified to the best of my abilities. I imported time and sleep in the header. Only big issue based on my limited knowledge is the 'if' call at the beginning since there isn't an 'else' that follows. I learned C way back in the day so I thought it might need a 'do'.
Note: Formatting on inserting code is strange on Github. I'm pretty new here so I added quote indentions because the code did not format correctly when copying it here.
#!/usr/bin/env python
import time
from time import sleep
import RPi.GPIO as GPIO
import subprocess
GPIO.setmode(GPIO.BCM)
GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.wait_for_edge(3, GPIO.FALLING)
def Long_press_shutdown:
#count how long the button is pressed
#not sure - is if: the correct call???
if
counter = 0
while ( pressed == 1 ):
if ( GPIO.input(3) == False ):
# button is still pressed
counter = counter + 1
# break if we count beyond 5 (to stop accidental shutdowns)
if (counter >= 5):
pressed = 0
else:
sleep(0.2)
else:
# button has been released
pressed = 0
# button has been released, count cycles and determine action
# count how long the button was pressed
if (counter < 2):
# short press, do nothing
pressed = 0
int_active = 0
else:
# longer press
if (counter < 5):
# longer press, initiate shutdown
print("system is shutting down...")
# run the shutdown command
subprocess.call(['shutdown', '-h', 'now'], shell=False)
#Use the interrupt from pin 3 to call the routine Long_press_shutdown
GPIO.add_event_detect(3, GPIO.FALLING, callback = Int_shutdown, bouncetime = 1000)
Please take a look and let me know!
I am getting this error when trying to run the install script.
Traceback (most recent call last):
File "/usr/local/bin/listen-for-shutdown.py", line 4, in
import RPi.GPIO as GPIO
ImportError: No module named RPi.GPIO
Python 3.x
64bit Bullseye on a RPi 4 b
Let me know if you need my information.
I'm having a lot of trouble getting a shutdown button to work, whether it's with adding dtoverlay=gpio-shutdown to config.txt or with your script.
Upon running sudo /etc/init.d/listen-for-shutdown.sh start I get:
RuntimeWarning: A physical pull up resistor is fitted on this channel!
I'd really like to get this button working but the extent of my raspberry pi knowledge is following tutorials and I can't seem to figure out what's wrong so I don't corrupt another SD card.
I had this working. I shut it down, powered off the pi, screwed in the button and hooked it back up, now it doesn’t work.
I even reinstalled and the scripts says listening for shutdown just fine but not shutting down or waking.
installed exactly as in the video on GPIO 3 and ground.
This worked fine for me for several years, but now - after a fresh installation of BOOKWORM, it fail.
The Shutdown-Functionality does not work any longer. I made a fresh Bookworm installation from scratch on a Pi Zero W,
installed the howchoo-script (installation script finished with success message), powered the PI Zero of via SSH 'sudo poweroff'.
I can restart the Pi with the button, but I can't power it off...
Any idea for me noob?
Hi, Thanks for your great tutorial it really help,
Using this method the problem is Fan is still on after the shutdown. I tried the GUI method, sudo shutdown -h now
and sudo poweroff
but the fan is still on. Also I cannot disconnect the fan.
Is there is any way or setting to turnoff the fan.
Thanks
Hello,
I'm testing this project. I have a problem.
Your example works ok for me.
In this moment I need to use the GPIO3, for the touchscreen.
So, Is it possible to use another gpio, not the gpio3 in this case?
Thanks!
Hello,
I have followed the instructions on your Howchoo article to make this code function... Only problem is, it doesn't start up during the boot. I have to use the command sudo /etc/init.d/listen-for-shutdown.sh start
in the terminal every time to initialize shutdown if I want to use my button. I'm not sure why this is. I'm using a Raspberry Pi 4B 4gb.
I liked the article so thought I'd add a switch to OSMC running on my RPi3. Unfortunately, it doesn't seem to have the RPi.GPIO module installed by default.
I got it working by following this post, but I'm new to this so I can't verify that everything in the post is really necessary. If somebody knows more, perhaps it could be added to the installation script.
One of the commenters on the accompanying article wrote:
Very good Post.
I found in my pi3 that RPi.GPIO were not loaded.
To fix it I did this:sudo apt-get install gcc python-dev python-pip i2c-tools libi2c-dev python-smbus
sudo pip install RPi.GPIO
Hi,
I follow your tuto and when the service is started, i can't use the keyboard anymore and can't access to the pi with ssh to.
Any idea ?
Best regards
I'm only interested in using this to shutdown and I cant seem to figure out where to find the file to edit the GPIO pin used. I would like to use GPIO23 (pin 16 on Pi B+). Thanks in advance.
Adding your 'listen-for-shutdown' code kills my HifiBerry audio card, it was working before adding the code, not working afterwards :-(
I assume it's some clash of resources, any suggestions please ??
I get that error on mi Pi 3B:
RuntimeWarning: A physical pull up resistor is fitted on this channel! GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
Hi all,
I'm using an RPi4 with an Octopi/Octoprint instance on it for 3D printer. In Octoprint software, i'm using
OctoPrint-PSUControl plugin to control a 3v3 relay on GPIO21 and through it power on and down 3D printer with Raspberry. And without the physical power button, this working as it must.
I successfully installed the power-button on GPIO3 and powering on and shutdown of raspberry is working well through the momentary button.
BUT. Then I send a command to close the relay and power on the printer through octoprint. The relay closes and the printer power on and at the same time raspberry seems to "receive" also the command to power off like if I push the new power button but I didn't touch it !
There must be a kind of interference of signal between Pi Power Button and OctoPrint-PSUControl. But I don't know where to start in investigations...
Any help will be very kind !
Hey, I need both GPIO 2 and 3 for a GroveSensor module over I2C, is there any other pins the wakeup works with?
Kind regards,
Matthew
Is it possible to use another pin rather Pin5 ?
I am not sure what is involved with updating unfortunately.
~ $ python3 /usr/local/bin/listen-for-shutdown.py
Traceback (most recent call last):
File "/usr/local/bin/listen-for-shutdown.py", line 4, in <module>
import RPi.GPIO as GPIO
ModuleNotFoundError: No module named 'RPi'
Is it possible to use other GPIOs? I already have those pins busy with a HAT 0.96 OLED screen. Thanks for the project it is brilliant and it works perfect.
Hi,
When I launch your install script it says
=> Installing shutdown listener...
=> Starting shutdown listener...
Starting listen-for-shutdown.py
Shutdown listener installed.
/usr/local/bin/listen-for-shutdown.py:9: RuntimeWarning: A physical pull up resistor is fitted on this channel!
GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_UP)
Then it is not responding and I have to quit with Ctrl + C
How can I solve this problem?
Also the project is awesome 😄
Thanks.
I'm currently using this with a switch rather than a button. I have the switch connected between BCM 3 and ground; if the pi is in a low power state the switch shorting to ground will start the pi, when running (switch is on,) the gpio is pulled low, so when the switch is turned off the pin goes high due to internal pull-up. I have changed in listen-for-shutdown.py:
GPIO.wait_for_edge(3, GPIO.FALLING)
to
GPIO.wait_for_edge(3, GPIO.RISING)
which works great for turning pi on and off. The only issue I have is the pi will boot and stay on with the switch in the off position when power is applied. A quick boot and shutdown is acceptable with the switch in the off position. I was thinking to do a one time check on the pin after the internal pull-up is initialized and perform a shutdown if it is already hi, not 100% sure how to go about this in the cleanest way. Any suggestions.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.