Pi Supply Switch v1.1 Code Examples

Raspberry Pi

The code on this page has been superseded, you can find the new scripts on our GitHub repository. Use the code below only if you are using Wheezy.

Python

There are two ways to implement this in Python…with a while loop, or using interrupts. Below can be found some code examples to use the soft shutdown switch functionality using a Python script.

Before you choose either of the code methods below, you first need to open the Python IDE. Once you have this ready, you then need to type in the code from either the interrupts or while loop section below. After you have completed that you then need to move on to the section about loading a Python file during the boot up process.

Interrupts

The best way to enable the soft shutdown feature of the Pi Supply Switch in terms of efficiency of the code and load on the CPU is using an interrupt based script, rather than the following one which uses a while loop. The code for the interrupt method is posted below.

# Import the modules to send commands to the system and access GPIO pins
from subprocess import call
import RPi.GPIO as gpio

# Define a function to keep script running
def loop():
    raw_input()

# Define a function to run when an interrupt is called
def shutdown(pin):
    call('halt', shell=False)

gpio.setmode(gpio.BOARD) # Set pin numbering to board numbering
gpio.setup(7, gpio.IN) # Set up pin 7 as an input
gpio.add_event_detect(7, gpio.RISING, callback=shutdown, bouncetime=200) # Set up an interrupt to look for button presses

loop() # Run the loop function to keep script running

 

While loop

A while loop is a basic fundemental of almost every programming language in existence, and is a very useful tool as well. The following code has the same end result as the interrupt based script above – it allows the safe shutdown of your Raspberry Pi – but it does so in a significantly more resource hungry way.

It is a more basic piece of code in terms of actually understanding the operation of the code and what is going on, but like with a lot of things this does not mean it is the best way to do it. Whilst we recommend using the interrupt based code above, we have added the while loop based code here for inclusion, as it works fine and some people might prefer to use this code (or at least have a play with it and try to get their head round how it works).

# Import the libraries to use time delays, send os commands and access GPIO pins
import RPi.GPIO as GPIO
import time
import os

GPIO.setmode(GPIO.BOARD) # Set pin numbering to board numbering
GPIO.setup(7, GPIO.IN) # Setup pin 7 as an input
while True: # Setup a while loop to wait for a button press
    if(GPIO.input(7)): # Setup an if loop to run a shutdown command when button press sensed
        os.system("sudo shutdown -h now") # Send shutdown command to os
        break
    time.sleep(1) # Allow a sleep time of 1 second to reduce CPU usage

 

Loading a Python file during boot process

Once you have entered either the interrupt based or while loop based code above into the Python IDE, you then need to create a folder called PiSupply in the /home/pi directory. Save the above code (whichever you choose) in the newly created folder at /home/pi/PiSupply – naming the file softshut.py or similar.

Then open an LXTerminal session, and use the Nano text editor to add some code to enable the Python script we just created to run when the Pi boots up. Type in:

sudo nano /etc/rc.local

and then add in the following code:

python /home/pi/PiSupply/softshut.py &

before the line that says:

exit 0

Press Ctrl+X to exit the Nano editor and when prompted press Y and then Enter in order to save the file you just edited.

Obviously the file name softshut.py that we used above can be whatever you want it to be, and the folder names and locations are all up to you as well…you just need to make sure that you maintain these throughout the above locations to make sure this code will function correctly on boot. In the above example we have used physical pin 7 on the GPIO header, which as you can see corresponds to the 4th pin from the right, on the top row (when looking down on the Pi from above, with the RCA video connector socket facing towards you). You can change this to use any different GPIO pin, we just chose pin 7 as it is next to physical pin 8 which is the required pin to use for the automatic power supply switching without any additional code.

It is additionally possible to add some code to the above to change to a pin other than physical pin 8 for the main automatic power off functionality…more on this soon.