Stop Raspberry Pi with a button

Warning : This article has been automatically translated by Google Translate


Danger : This article has been automatically translated by Google Translate


Caveat : This article has been automatically translated by Google Translate

If you have a Raspberry in a corner, difficult to access and even more to connect a screen, You should be able to stop it properly without damaging the file system. Here's a small solution simple and inexpensive.

Arrêter le Raspberry Pi avec un bouton

The button is a simple and inexpensive component. Suddenly it is really easy to implement and bind to your Raspberry.

Editing in place, need to get to the software part. I chose Python because it's simple, the GPIO are well managed and I have never done, so it's the right opportunity ! 🙂

With the button, I chose to add a LED to indicate when the script is ready. In the startup process, I chose to run the script last thanks LSB tags. The LED lights so when all scripts are started.

You can disable script running in the background by stopping the service in a traditional manner. The LED will be off then.

So here is editing with a button, a LED and a resistor 220Ω.

Arrêter le Raspberry Pi avec un bouton

  1. Update distribution
     apt-get update && apt-get upgrade -y
  2. Install the required packages
     apt - get install python python y - rpi.gpio
  3. Create the Python script that will be used during the boot sequence. I called it /etc/init.d/shutdownBtn.py
    #!/usr/bin/env python2.7
    # -*- coding: UTF - 8 -*-
    import RPi.GPIO as GPIO import time as time import os import signal
    # Define mode to access GPIO pins GPIO.setmode(GPIO.BCM)
    GPIO.cleanup()
    # PIN id for the shutdown button GPIO_shutdown = 23
    # pin id for the LED status GPIO_led=17
    
    # GPIO_shutdown will be an input pin GPIO.setup(GPIO_shutdown, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    # GPIO_led will be an output pin GPIO.setup(GPIO_led, GPIO.OUT)
    
    # Handler to stop the process def signal_handler(signal, frame):
        # Turn off the status LED     GPIO.output(GPIO_led, False)
        GPIO.cleanup()
        exit(0)
    
    # Define what to do when the shutdown button is pressed def funcShutdown(channel):
        start_time = time.time()
        intSeconds = 0
        # How long to wait for the button     maxWaitPushButton = 2
        while intSeconds < maxWaitPushButton and GPIO.input(channel) == GPIO.LOW :
             intSeconds = time.time() - start_time          time.sleep(0.1)
        if intSeconds >= maxWaitPushButton:
            # Turn off the status LED         GPIO.output(GPIO_led, False)
            GPIO.cleanup()
            os.system('sudo halt')
            #exit()  
    
    # Add a signal handler for the SIGTERM signal signal.signal(signal.SIGTERM, signal_handler)
    # Wait for the shutdown button to be pressed GPIO.add_event_detect(GPIO_shutdown, GPIO.FALLING, callback=funcShutdown, bouncetime=300) 
    # Light up the status LED GPIO.output(GPIO_led, True)
    # Loop while 1:
        time.sleep(0.2)
  4. Créer le script qui va être utilisé lors de la séquence de boot. I called it /etc/init.d/shutdownBtn. Attention to the LSB parameters (more info here). I chose to start this script last, Once the system is ready.
    #!/bin/bash
    
    ### BEGIN INIT INFO
    # Provides:          shutdownBtn
    # Required-Start:    $CL.
    # Required-Stop:    
    # Default-Start:     2 3 4 5
    # Default-Stop:      0 1 6
    # Short-Description: Script to add the capability to shutdown the Raspberry Pi with a button
    # Description:       Script to add the capability to shutdown the Raspberry Pi with a button
    ### END INIT INFO box "$1" in start)
    	/usr/bin/python /etc/init.d/shutdownBtn.py &
    	echo $! > /tmp/.shutdownBtn-pid
            ;;
        stop)
    	kill 'cat /tmp/.shutdownBtn-pid'
            ;;
        reload|restart)
            $0 stop
            $0 Start
            ;;
    
        *)
            echo "Use: $0 Start|stop|restart|reload"
            exit 1
    esac exit 0
  5. Change the rights of the script
    chmod 755 /etc/init.d/shutdownBtn
  6. Add the script to the startup sequence
     insserv shutdownBtn

You are ready. Either you reboot your machine or you start the service manually. Press at least 2 seconds to turn off your precious !

Like this article ? Bear with me on patreon!

You may also like...