티스토리 뷰

728x90
반응형

 

라즈베리파이4 GPIO 5번(yellow), 39번(Green) 스위치 연결

1.  sudo nano pishutbtn.py

----------------------------------------------------------------------

#!/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.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(shutdownPin, GPIO.IN)

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)

-----------------------------------------------------------------------

 

2.   sudo nano /etc/xdg/lxsession/LXDE-pi/autostart 

@/usr/bin/python3 /home/pi/pishutbtn.py

 

3. reboot now

 

 

출처: GitHub - gilyes/pi-shutdown: Shutdown/reboot(/power on) Raspberry Pi with pushbutton

반응형
댓글