Eteindre son Raspberry avec un Bouton sur GPIO

Dans cet article, je vous décris comment éteindre son Raspberry Pi avec un appui long sur le GPIO.
De quoi éteindre proprement son système, sans commande SSH.

Le câblage:

Montage en Pull up ou Pull Down

Dans mon expérience j’utilise le montage Pull up sur le GPIO 24 du Raspberry (μSvxCard)

Programme source en Python:
http://raspberrypi.stackexchange.com/a/42945

#!/usr/bin/env python2.7
#-------------------------------------------------------------------------------
# Name:         Shutdown Daemon
#
# Purpose:      This program gets activated at the end of the boot process by
#               cron. (@ reboot sudo python /home/pi/shutdown_daemon.py)
#               It monitors a button press. If the user presses the button, we
#               Halt the Pi, by executing the poweroff command.
#
#               The power to the Pi will then be cut when the Pi has reached the
#               poweroff state (Halt).
#               To activate a gpio pin with the poweroff state, the
#               /boot/config.txt file needs to have :
#               dtoverlay=gpio-poweroff,gpiopin=27
#
# Author:      Paul Versteeg
#
# Created:     15-06-2015, revised on 18-12-2015
# Copyright:   (c) Paul 2015
# https://www.raspberrypi.org/forums/viewtopic.php?p=864409#p864409
#-------------------------------------------------------------------------------
 
import RPi.GPIO as GPIO
import subprocess
import time
 
GPIO.setmode(GPIO.BCM) # use GPIO numbering
GPIO.setwarnings(False)
 
# I use the following two GPIO pins because they are next to each other,
# and I can use a two pin header to connect the switch logic to the Pi.
# INT = 17    # GPIO-17 button interrupt to shutdown procedure
# KILL = 27   # GPIO-27 /KILL : this pin is programmed in /boot/config.txt and cannot be used by any other program
INT = 24    # GPIO button interrupt to shutdown procedure
 
# use a weak pull_up to create a high
GPIO.setup(INT, GPIO.IN, pull_up_down=GPIO.PUD_UP)
 
def main():
 
    while True:
        # set an interrupt on a falling edge and wait for it to happen
        GPIO.wait_for_edge(INT, GPIO.FALLING)
#       print "button pressed"
        time.sleep(1)   # Wait 1 second to check for spurious input
        if( GPIO.input(INT) == 0 ) :
            subprocess.call(['poweroff'], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
 
if __name__ == '__main__':
    main()

Mise en place:

Se connecter en ssh sur votre Raspberry, aller dans le dossier Home et créer un répertoire shutdown

cd /home/
mkdir shutdown

Se rendre dans ce nouveau répertoire et éditer un fichier:

cd shutdown
nano shutdown.py

Copier et coller le code ci-dessus et faire CTRL+X pour quitter et Y ou O pour enregistrer.

Editer le fichier rc.local

nano /etc/rc.local

Tout en bas juste avant le exit 0 insérér la ligne suivante:

python /home/shutdown/shutdown.py

Faire CTRL+X pour quitter et enregistrer en validant pas Y ou O selon le langage de votre système.

Le redémarrage prendra en compte le programme, désormais il vous suffit de faire un appui long sur le bouton afin d’éteindre le systeme.
Cela peut vous éviter une connexion SSH si le système Raspberry Pi ne possède pas d’écran.

Vous pouvez recevoir un email dès la parution d’un article sur le Blog F8ASB.COM, entrez votre mail sur la fenêtre à droite et cliquer sur abonnement. Tous les articles en 1 clic ICI

Ce contenu a été publié dans Raspberry, μSvxCard. Vous pouvez le mettre en favoris avec ce permalien.

2 réponses à Eteindre son Raspberry avec un Bouton sur GPIO

  1. Georg Rensonet, DD8ZX dit :

    Hi,
    on my svxcard there is a 2 pin jumper next to the shutdown button. Can I connect just a momentary switch to this jumper and shutdown the Pi when mounted in a box? I did not dare to short the 2 pins for a test, not knowing if I break something, if it is not for the purpose of connecting a switch.
    73
    Georg, DD8ZX

    • f8asb dit :

      No, 2pin isn’t for reset button but to connect electret microphone directly ( no tested for futur project, hotspot without radio)
      To reboot you can directly connect on 2 pin on raspberry or on the button,

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Time limit is exhausted. Please reload CAPTCHA.

Ce site utilise Akismet pour réduire les indésirables. En savoir plus sur comment les données de vos commentaires sont utilisées.