Uninterruptible power supply for the Raspberry Pi
Serial Still Works
Typically, corresponding GPIO pins are used to gain access to the serial interface of the Raspberry Pi. I use a so-called serial USB-to-TTL cable, which can be purchased online. Because of the presence of the UPS board, the pins can no longer be reached; however, this is not a problem. You can redirect the serial interface of the Raspberry Pi to the UPiS with a firmware upgrade. Similarly, it is possible to redirect the interface to the micro-USB connection of the UPS. Then, the USB port of the PC should be connected with that of the UPiS board. This solution usually works.
In the example described here, communication took place over the serial USB-to-TTL cable previously mentioned using minicom
on the connected Linux PC. It was not necessary to adjust the configuration at all to gain access over the micro-USB connection of the UPiS. This held true for both the Rasp Pi and PC operating systems.
What Happens During a Power Outage?
The first task of a UPS is to protect connected devices from loss of power. The UPS should also be able to deal with situations in which the loss of power lasts longer than the time it takes for the battery to discharge. Typically, the UPS will direct all applications to close automatically and initiate an orderly shutdown of the connected device. The UPiS is equipped to deal with these tasks as well.
Two methods can be used for reading the status of the power supply and initiating suitable actions. Additionally, a hardware button, along with the proper configuration, can start an orderly shutdown of the Raspberry Pi. This switch has appeared as SDWN (Shutdown) during the firmware upgrade. A software implementation available via the UPiS terminal command @sdwn
.
Information regarding a desired shutdown is always readable on GPIO pin number 27 for both software- and hardware-initiated shutdowns. Therefore, when the Raspberry Pi is supposed to respond to the press of the hardware button, a program needs to run on the Rasp Pi to read out the status of this pin. The user handbook for the UPS board provides a simple Python script free of charge (Listing 2), which gently shuts down the ARM computer with shutdown -h now
.
Listing 2
Shutdown Script
01 #!/usr/bin/env python 02 # Import the access libraries for GPIOs, OS, and time delay 03 import RPi.GPIO as GPIO 04 import time 05 import subprocess 06 07 # Adapt the pin numbering 08 GPIO.setmode(GPIO.BCM) 09 10 # We need to pay attention to pin 27 11 GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_UP) 12 13 # A loop for waiting until the button is pressed, which is signaled via pin 27 14 15 while True: 16 # Check if something came in on pin 27 17 if(GPIO.input(27)==0): 18 # We want to execute the following command 19 cmd="sudo shutdown -h now" 20 # Send this command to the OS 21 pid=subprocess.call(cmd, shell=True) 22 break 23 # Wait for one second 24 time.sleep(1) 25 # That's all
You still need to deal with automatic shutdown. Apparently, there must be a change in the status of the power supply for this to be triggered. As described above, various steps must be undertaken before the Raspberry Pi will notice that it is running on battery power. Another UPiS terminal command comes to the rescue. By issuing @pwrinfo on
, the user communicates to the UPS board that it should signal a status change in the power supply to the serial interface (Figure 3). A listener process can then react in the desired fashion.
You also can query via @pm
regularly for the desired information and then initiate suitable actions. Users who find dealing with the serial interface unwieldy can use the I2C bus for reading out the necessary data. The register address needed to do this is 0x6A 0x00.
« Previous 1 2 3 Next »
Buy this article as PDF
Pages: 6
(incl. VAT)