W. H. Heydt wrote: ↑Fri Mar 06, 2020 5:55 am
Old TVs had a variety of ways to turn the power on or off. One very common one was a knob that pushed in or pulled out. That could be combined with something that turned. I'd suggest finding a good, old fashioned, electronics store to see if they stock what you want.
The way your power off on the Pi3 works is probably a momentary contact switch. It probably isn't an actuall power *off*, but just a system shutdown (and, if so, pressing it again--if you used the right pins--will reboot the system). A signal waking up a Pi that is truly off won't work. Dropping power and restoring it will, but for the sake of the well being of your SD card, there should be a proper system shut down first.
You can get control switches with any number of independent controls. Each separate (and separated) control is a "pole". So, for instance, you could have a single throw, double pole switch.
You're correct. The script doesn't completely kill power to the Pi, just runs a safe shutdown script. The momentary switch I have now connects to the GPIO pins and the script is just monitoring for a change from HIGH to LOW and vice versa to either turn the pi on, or initiate a shutdown command.
It sounds like both you, and rpdom, are recommending a multi-pole rotary switch (thanks for getting me to the right name guys!). With any multipole switch, I'm assuming I'm connecting two poles, in my case, to two different pins on the pi, and then creating a script similar to what I indicated above. I'm going to look for HIGH vs LOW on two different sets of pins. Does that sound about right? I'll probably have to do some more digging here, but want to confirm I'm on the right path.
IF I happened to find a pull/push type of switch, would I be able to set it up similar to the way that the momentary switch is setup, essentially using the same script, below? I got this script a while back from someone else and it worked well
Code: Select all
#!/usr/bin/python
import RPi.GPIO as GPIO
import time
import subprocess
''''
GPIO.setmode(GPIO.BOARD)
#using pin numbering on the Pi, instead of the
#GPIO pin outs
#use the same pin that is used for the reset button
GPIO.setup(5, GPIO.IN, pull_up_down = GPIO.PUD_UP)
loopCounter = 0
while True:
#get current button state
buttonState1 = GPIOinput(5)
#check if button has been pushed
if buttonState1 == False: #Button is down.
loopCounter = loopCounter + 1 #Add another counter to the loop counter
#shutdown instruction
subprocess.call("shutdown -h now", shell=True, stdout = subprocess.PIPE, stderr=subprocess.PIPE)
else:
loopCounter = 0 #button hasn't been pressed to trigger the loop counter
time.sleep(.5)
#/usr/bin/python
# shutdown/reboot(/power on) Raspberry Pi with pushbutton
import RPi.GPIO as GPIO
from subprocess import call
from datetime import datetime
import time
# pushbutton connected to this GPIO pin, using pin 5 also has the benefit of
# waking / powering up Raspberry Pi when button is pressed
shutdownPin = 5
# if button pressed for at least this long then shut down. if less then reboot.
shutdownMinSeconds = 3
# button debounce time in seconds
debounceSeconds = 0.01
GPIO.setmode(GPIO.BOARD)
GPIO.setup(shutdownPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
buttonPressedTime = None
def buttonStateChanged(pin):
global buttonPressedTime
if not (GPIO.input(pin)):
# button is down
if buttonPressedTime is None:
buttonPressedTime = datetime.now()
else:
# button is up
if buttonPressedTime is not None:
elapsed = (datetime.now() - buttonPressedTime).total_seconds()
buttonPressedTime = None
if elapsed >= shutdownMinSeconds:
# button pressed for more than specified time, shutdown
call(['shutdown', '-h', 'now'], shell=False)
elif elapsed >= debounceSeconds:
# button pressed for a shorter time, reboot
call(['shutdown', '-r', 'now'], shell=False)
# subscribe to button presses
GPIO.add_event_detect(shutdownPin, GPIO.BOTH, callback=buttonStateChanged)
while True:
# sleep to reduce unnecessary CPU usage
time.sleep(5)
'''
Original
Shutdown
Script
import RPi.GPIO as GPIO
import time
import subprocess
GPIO.setmode(GPIO.BOARD)
GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_UP)
loopCounter = 0
while True:
buttonState1 = GPIO.input(5)
if buttonState1 == False:
loopCounter = loopCounter + 1
subprocess.call("shutdown -h now", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
else:
loopCounter = 0
time.sleep(.5)