How to run a user script with the PiJuice power events
This is a quick guide to show you how you can write you own scripts using the Python API or general system scripts and have them run when certain power events occur on the PiJuice HAT such as low power, no power etc. In this example we will create a python script that will shutdown the Raspberry Pi and remove 5V GPIO power fro the PiJuice. In this instance the PiJuice will then enter a low powered state until it has wakeup. There are some important factors that need to occur when creating the script to be run such as, the user script must be run as a non-prviliaged user such as ‘pi’, the script must be executable by the system and include the shebang line of the programming language. This guide will show you how to do those things and get your user script working with the PiJuice software.
Getting Started
Step 1 – First you will need to make sure that you have the PiJuice software setup and installed on your Raspberry Pi with the latest Raspbian Desktop. You can get the latest version of Raspbian from https://www.raspberrypi.org/downloads/. To install the PiJuice software you need to open up a terminal window and type in the following commands:
sudo apt-get update sudo apt-get install pijuice-gui
Reboot your Raspberry Pi and you should see the PiJuice software running in the taskbar.
Step 2 – Now lets go ahead and create our script that we want run when the main power supply has been removed. Open up a terminal window and type in the following to create the file:
sudo nano shutdown.py
Then add the following to the python script:
#!/usr/bin/python3
from pijuice import PiJuice
import os
pijuice = PiJuice(1, 0x14)
# Remove power to PiJuice MCU IO pins
pijuice.power.SetSystemPowerSwitch(0)
# Set wakeup
pijuice.power.SetWakeUpOnCharge(0.0)
# Remove 5V power to RPi after 60 seconds
pijuice.power.SetPowerOff(60)
# Shut down the RPi
os.system("sudo halt")
Save the changes with CTRL+O & ENTER then exit with CTRL+X
Note: Very important that the first line of the code is the shebang line. This tells the system how to run the script. As we ar using the PiJuice Api in our script we need to use Python 3 programming language.
Step 3 – Make the script executable so we can run it from other programs. Open the terminal and enter the following command:
sudo chmod +x shutdown.py
You can check the user permissions of any file with command:
ls -l
Before
After
Step 4 – Check if user is non-privilaged user such as pi. You can check who owns the script file with the same command:
ls -l
You can see in the image above that our script ‘shutdown.py‘ us owned by user ‘root‘ under user group ‘root‘ also. This will occur when you login and create files using SSH. If you created the file on the Raspberry Pi itself then the users and group will be ‘pi‘. If you need to change it then you can do so with the following command:
sudo chown pi:pi shutdown.py
Step 5 – Configure the PiJuice. Open up the PiJuice software and head over to the “System Events” tab.
Check the box “No Power” and then in the drop down box select “USER_FUNC1“.
Click “Apply”
Head over to the “User Scripts” tab.
Click on the three dots to the right next to USER FUNC1 and select the script file to where you save it to.
Click “Apply” to save the changes
Step 6 – Test your setup. Remove the power supply from the PiJuice and PiJuice will then detect and trigger the “No Power” event and begin to shutdown the Raspberry Pi. After 30 seconds the PiJuice will completely remove power to the Pi and enter a low power state. To wakeup the Raspberry Pi again simply reconnect the power supply to the PiJuice.