Linux Format

Uninterrup­ted Pi power.......

Nick Peers reveals how to set up and configure a simple UPS add-on to provide your Pi with emergency battery power, should the lights go out.

-

Nick Peers waits for no (wo)man, especially the electricia­n, so he’s built his own Pi-based UPS.

Your Raspberry Pi is particular­ly sensitive to power outages. Added to the inconvenie­nce of suddenly losing access to your Pi or half an hour’s work, there’s the possibilit­y your shutdown will corrupt your microSD card, forcing you to wipe it and start again from scratch.

This is where a battery rides to the rescue. It provides your Pi with an independen­t source of power when the main supply is interrupte­d, whether through a power cut, tripped MCB or simply by accidental­ly yanking out the power cable. While it’s possible to jury rig your own battery supply, why go to all that trouble when a ready-made uninterrup­tible power supply (UPS) is waiting for you?

Your own Pi UPS

The solution in question comes from the folk at Alchemy Power, who have developed two UPSes for both Pi and Pi Zero users. Its main solution is the Pi-UpTimeUPS, which works with any Pi that has a 40-pin GPIO header, including the Pi Zero. Its form factor is aimed at the Pi, however, and comes with a prohibitiv­e price tag: £46 per unit, plus a one-time shopping cost of £16 from the US via the distributo­r Farnell ( http://uk.farnell.com).

A less-powerful option, but one that can still provide over an hour of emergency power, is the PiZ-UpTime. This costs £28 plus UK shipping from www.robotshop.com/uk and is the unit we used in this tutorial. Although optimised for the Pi Zero, it will work with the Pi 2 and Pi 3 too, albeit for a shorter period than the Pi-UptimeUPS, which can deliver over 10 hours via its two batteries.

Both models come as a single circuit board with a battery holder. They can both be connected to your Pi via its GPIO pins (Pi Zero users will need to solder or add these themselves), and both offer pass-through pins so you can use them in conjunctio­n with other add-ons, secure in the knowledge they’ve been designed to supply power to both your Pi and any add-ons in the event of power failure.

Of course, it’s not always practical or desirable to fit GPIO pins to your Pi Zero, which is where the PiZ-UpTime scores points. Not only can it be plugged into a GPIO pin header, but it can be connected relay-style between your power supply and the Pi Zero itself using a micro USB cable (supplied).

Physical installati­on

Power down your Pi, unplug the power supply and then fit the UPS board to your Pi, either via the GPIO pin header or – in the case of the PiZ-UpTime where you’re not using the GPIO header – by unplugging the power from the Pi Zero and connecting the PiZ-UpTime to the Pi Zero’s power socket using the supplied cable.

Once done, connect the battery – these tend to be supplied with some level of charge, so the moment you install it, your Pi will leap into life. Be sure to shut down safely from here – ssh into your Pi if necessary and issue the following: $ sudo shutdown -h now

Now connect your Pi’s power supply to the ‘In’ port on the Pi-UpTimeUPS or PiZ-UpTime so it can start charging the battery as well as provide emergency power when required.

This follows the standard CCCV charging method except when the battery has been depleted, in which case lowcurrent charging is first used to bring the battery up to a level where it can be safely CCCV charged.

As you’d expect, the board has onboard circuitry to prevent both overchargi­ng of the battery as well as deep dischargin­g – a hard shutdown is performed before the latter can occur. There’s also a temperatur­e monitor that can shut down the charging process if it becomes too hot. Note the reset button on the front or side of the board. Use this after a controlled shutdown to restart your Pi without first having to unplug the power or disconnect the battery.

The battery level is monitored on GPIO pin 26 – a jumper on the board can be set to disable this if the pin is already in use elsewhere. However, if you want to be able to perform a controlled shutdown when the battery level is low, you’ll need this pin connected. This is the biggest drawback of the PiZUptime’s USB connection option, because it can’t provide this functional­ity unless connected via the GPIO pins. Instead, make use of the LEDs to keep an eye on things ( seethebox).

To make use of the shutdown scripts, first update the underlying OS powering your Pi, typically like so: $ sudo apt-get update $ sudo apt-get upgrade && sudo apt-get autoclean

Next, create a folder and download the required files: $ mkdir ups $ cd ups $ wget http://alchemy-power.com/wp-content/ uploads/2017/06/GPIO-shutdown-sample.zip $ wget http://alchemy-power.com/wp-content/ uploads/2017/08/GPIO-crontab.zip

Next, unzip the two Python scripts: $ unzip GPIO-shutdown-sample.zip && unzip GPIO-crontab. zip

When the first script is run, it will sleep for 10 seconds before checking the battery levels again. If the battery levels are low, it will trigger a controlled shutdown within two minutes. You can see how the script works by examining it in nano, where a explanatio­n accompanie­s each command: $ nano GPIO-shutdown-sample.py

If you want to lengthen the battery level check, you’ll need to change the ‘schlaf’ value as shown in the script. The script will also display a time stamp on-screen every time it wakes to check the battery level. You can disable this by commenting out line 78 as follows: # print("Time is %s " % (time.ctime()))

If you want to skip the two-minute warning when battery levels run low, change the subprocess.call line to the following: sub process. call (" shut down-h now &”, shell= True)

Save any changes and exit. Now test the script: $ sudo python GPIO-shutdown-sample.py

Disconnect power from the Pi, which should continue to run from the UPS battery. The time should be displayed every 10 seconds and when the battery below a certain level, you’ll be informed the Pi will be shutting down in two minutes (or immediatel­y if you decided to skip the delay). In our tests, our Pi Zero with external Wi-Fi adaptor ran for up to an hour before shutting down – ample time for getting the power back up.

Permanent protection

The above script must be run manually on every boot – for permanent, automatic protection reconnect your power and move on to the final stage. The GPIO-crontab script is required to ensure the GPIO pins are tracked and a controlled shutdown is performed once the battery falls below the accepted threshold. Set it up thus: $ sudo cp ~/ups/GPIO-crontab.txt /usr/local/bin/uptime-gpio. py $ sudo chmod 755 /usr/local/bin/uptime-gpio.py $ sudo crontab -e

Choose your editor if prompted, then add the following line to have the script run silently once every two minutes (change */2 to */1 to run it every minute): */2 * * * * python /usr/local/bin/uptime-gpio.py

Save the cron file, exit and reboot. Now test the battery again – this time, two minutes after the low-battery warning is reached, your Pi should automatica­lly shut itself down. Congratula­tions, you’ve just ensured your Pi will never again suffer due to a sudden power outage!

 ??  ?? While plugging your PiZ-Uptime into your Pi’s GPIO pins is the best way to use it, you can also connect it via the Pi’s power port.
While plugging your PiZ-Uptime into your Pi’s GPIO pins is the best way to use it, you can also connect it via the Pi’s power port.
 ??  ?? A pair of scripts are provided for making use of the PiZ-Uptime’s ability to monitor battery usage via the GPIO header.
A pair of scripts are provided for making use of the PiZ-Uptime’s ability to monitor battery usage via the GPIO header.

Newspapers in English

Newspapers from Australia