Raspberry Pi UPS

Protecting your Raspberry Pi from power failure

Raspberry Pi UPS

Introduction

Unexpected power failures can corrupt your SD card and prevent your Raspberry Pi from booting up the next time you turn it on. I learnt that personally when my toaster decided to trip my house’s circuit breakers in a random way. There are a lot of possibilities to solve this problem and ensure that the shutdown is done in an orderly fashion. For instance, in this post you can see that, with a couple of supercapacitors and diodes, you can build a circuit that buffers 5 V and sends a signal to a GPIO port when the power is cut to start shutting down the system. There are also specific solutions designed to be mounted in the Raspberry Pi like this one. As I will explain in the following lines, I decided to use an USB power bank as my Raspberry Pi UPS (Uninterruptible Power Supply).

Solution implemented

I adopted this solution inspired by this page. The USB power bank used will simply power the Raspberry, while it itself is powered by an standard power grid. The power bank will, as usual, automatically start charging after a power shortage. It should not need any user interaction to start powering the Raspberry Pi, so avoid using power banks with on/off buttons like this model.

Note that some power banks experiment a short power interruption when passing from charging to powering mode. This period can be long enough to turn off your Raspberry Pi. The power bank proposed here, for instance, offers a very good price/quality relationship and satisfies all the requirements needed to be used as an UPS. Remember to use a good quality power supply and micro USB cable as commented here.

The second piece of this solution is the daemon upsd. It will detect power cuts by monitoring Raspberry Pi LAN’s connection. Since both the Raspberry Pi and and the domestic router are powered by the same power source, when a power interruption happens, the network interface status will be turned down. Of course, it will only work if you use an ethernet interface (no WiFi). This program will also estimate the remaining battery based on the power bank’s capacity and the system power consumption. When the battery reaches a certain level, it will automatically shutdown the system. As we did with fail2ban, you can configure upsd to send an IFTTT notification when the power is reestablished. You can see an image of the needed connections in the following link.

Configuring UPSD

Once all the connections are ready, you can start configuring the piece of software used to monitor power cuts. All the credit for this sofware goes to Mathias Kunter.

To install upsd, simply run the two following commands in the terminal of your Raspberry Pi:

Note that new versions may be available in the future, so visit the creator’s page and update, if needed, the package downloaded.

Once the software is installed, you should perform a couple of configurations. Open the upsd.conf file and adapt the values to your needs, starting from the network interface being monitored. I decided to increase considerably the check and update intervals to 10 and 60 seconds respectively.
In order to calculate your battery run time just divide the Watts per hour offered by your power bank by the Watts consumed by your Raspberry. If you only know the miliAmpers per Hour (mAh) of your device, just multiply them by 5V. I measured the Watts consumed by the Raspberry during a day with a energy consumption monitor and it was near 2 Watt with an standard CPU load, with peaks of 2.4 Watt when reproducing video. I will use this last value, in order to ensure a certain safety margin.
In my case:

In order to calculate your battery charge time you will need to know the input current. The charger I used offered 2 A so:

UPSD Scripts

Now it is time to set the scripts that will be run when a power interruption is detected. Those scripts are located in /usr/lib/upsd and are:

  • power outage – will run after a power outage is detected. You can decide in this point to reduce the activity in the Raspberry in order to decrease its consumption.
  • low battery – it runs when the low battery threshold is reached. In this point you should prepare you system for an ordered shut down.
  • power back – this script is executed if the power comes back after an outage. In this situation can be a good idea to send a notification to your smartphone using IFTTT, assuming that with the power the internet connection has also been recovered.

As an example, this is the script used for power back in my case. It waits until the DSL connection has been reestablished by testing if there is ping with host www.google.com. Code can also be found in my Github account. You can find more information about how to create an IFTTT notification in this link.

Once everything is correctly configured, just start the upsd service by doing:

And check that everything is running correctly by typing:

You can now try that everything is in place by simulating a power outage. Remember that, in order to be detected by the upsd monitor, the network interface should be down.
Raspberry UPS IFTT Notification

1 thought on “Protecting your Raspberry Pi from power failure

Leave a Comment