import RPi.GPIO as GPIO from time import sleep import os GPIO.setmode(GPIO.BOARD) GPIO.setwarnings(False) # Turn off warnings output GPIO.setup(38, GPIO.OUT) # Set pin #38 (GPIO20) to output GPIO.setup(40, GPIO.IN) # Set pin #40 (GPIO21) to input while True: buttonIn = GPIO.input(40) if buttonIn == True: print 'System shuts down' GPIO.cleanup() os.system("sudo shutdown -h now") break sleep(1)
#! /bin/bash # Set up GPIO20 and set to output echo "20" > /sys/class/gpio/export echo "out" > /sys/class/gpio/gpio20/direction echo "1" > /sys/class/gpio/gpio20/value # Set up GPIO21 and set to input echo "21" > /sys/class/gpio/export echo "in" > /sys/class/gpio/gpio21/direction while ( true ) do # check if the pin is connected to GND and, if so, halt the system if [ $(</sys/class/gpio/gpio21/value) == 1 ] then echo "20" > /sys/class/gpio/unexport echo "21" > /sys/class/gpio/unexport shutdown -h now "System halted by a GPIO action" fi sleep 1 done
wget http://pypi.python.org/packages/source/R/RPi.GPIO/RPi.GPIO-0.5.11.tar.gz tar zxf RPi.GPIO-0.5.11.tar.gz cd RPi.GPIO-0.5.11 sudo python setup.py install
@reboot python /home/pi/lentyay/poweroff.py & @reboot sudo /home/pi/lentyay/shutdown.sh &
Source: https://habr.com/ru/post/254723/
All Articles