How to add a safe shutdown button to Stratux (or Raspberry Pi)?

By default a Raspberry Pi and therefore a Stratux Flarm does not have a physical switch to shut down the operating system. Just cutting the power of a Raspberry (or any computer) will result in RAM issues in the long term. Therefore, this section explains how we use a button which triggers the command to shut down a Raspberry Pi/ Stratux safely.

Since Python becomes more popular, we will use this programming language.

Stratux firmware by Dross:Aviation supports safe shutdown switch for Stratux FLARM
Enable Stratux firmware to support shutdown switch

Buy your ready 2 fly Stratux Firmware SD card instead.


Use PUTTY to enable SSH connection to your Stratux/ Raspberry Pi

  1. The operating system has already been flashed on a SD card.
  2. Download and install the latest version of PUTTY, a client to enable SSH connection
  3. Boot up your Stratux
  4. Stratux' Wifi should appear in your Laptop's network section.
  5. Execute PUTTY and use Stratux' default IP address as the Host Name:192.168.10.1
    PUTTY - enable SSH connection to Raspberry PI and Stratux (OGN) Flarm
    PUTTY - enable SSH connection to Raspberry PI and Stratux (OGN) Flarm
  6. Use the Raspberry PI's standard username "pi" and password "raspberry"
    the screen should look like the following:

    PUTTY SSH welcome page
    PUTTY SSH welcome page

    highlight the source code below, go to PUTTY and right click to paste the code


Creating the shut down button support

  1. We create a folder which we name "Scripts".
  2. Then a blank file named "shutdown_pi.py" gets created
  3. we edit the file and fill-in content into it.
    (mark the code, click right in PUTTY console to paste the code, then press Enter)

    mkdir Scripts  
    cd Scripts  
    touch shutdown_pi.py
    nano shutdown_pi.py
  4. Then fill-in the below code into the file:
    #!/bin/python  
    # Simple script for shutting down the raspberry Pi at the press of a button.  
    # by Inderpreet Singh, modified by Dross:Aviation 
     
    import RPi.GPIO as GPIO  
    import time  
    import os  
     
    # Use the Broadcom SOC Pin numbers  
    # Setup the Pin with Internal pullups enabled and PIN in reading mode.  
    GPIO.setmode(GPIO.BCM)  
    GPIO.setup(21, GPIO.IN, pull_up_down = GPIO.PUD_UP)  
     
    # Our function on what to do when the button is pressed  
    def Shutdown(channel):  
        os.system("sudo shutdown -h now")  
     
    # Add our function to execute when the button pressed event happens  
    GPIO.add_event_detect(21, GPIO.FALLING, callback = Shutdown, bouncetime = 2000)  
     
    # Now wait!  
    while 1:  
        time.sleep(1)
  5. Save the file by pressing CTRL+X then press y to confirm to overwrite and Enter
  6. The Python script now tries to access GPIO Pin  GPIO pin 39 (Ground) and 40 (GPIO 21 – PCM_DOUT)
  7. To test if the script works, please simply enter the following lines into PUTTY (again, mark the code, and do a right click in the console to paste)
    sudo python shutdown_pi.py
  8. If the script works, you must close and re-open PUTTY again, by following steps 4. and 5.
  9. Now we have to enable that the script runs automatically every time the Raspberry Pi/ Stratux starts. Therefore we execute the following command:
    sudo nano /etc/rc.local
  10. We need to add our python command before the last line which closes the if loop. Therefore, add the following line before the #fi at the end of the file:
    add the following line before the #fi at the end of the file
    add the following line before the #fi at the end of the file
    sudo python /home/pi/Scripts/shutdown_pi.py &
  11. Save the file by pressing CTRL+X and then y, Enter
  12. Connect the switch to GPIO pin 39 (Ground) and 40 (GPIO 21 – PCM_DOUT) and press it. The Stratux should now shutdown within 2 seconds.