I wanted to have mobile autonomy on my RPi 3, and I found this awesome UPS HAT from waveshare. It is a UPS HAT For Raspberry Pi, 5V Uninterruptible Power Supply, Multi Battery Protection Circuits. You can use 2x 18650 batteries and you’re in business.

But as you may know, it is dangerous for the battery life to drain too low. You need to monitor the battery level and bring a power supply back, or shutdown the RPi, if you reach a certain threshold. The website provides a python script to monitor some interesting data. Let’s get our hands at it!

I. RPi installation

As usual, I am using my default installation of a RPi with Ubuntu Core. But we also need to add a few Python libraries not installed by default.

sudo apt install -y python3-smbus

II. Scripts

We’re going to need 4 scripts or configurations:
1. INA219 python script
2. Our python script to get the data in the format we want
3. A BASH script run by CRON to check values and eventually shutdown the RPi. Note this could have been done within the custom python script too 😀
4. A logrotate configuration

1. INA219 python script
This is a small adjusted script from the original. You can download it here.

2. Custom python script
This script makes use of the INA219 utility script and format the data depending on command line arguments. The one we’ll be using is:

sudo python3 ./powerlog.sh short

Which will format the output like this:

799,-190,1.508,83

This first value is the voltage in V multiplied by 100. This is to get a non decimal value for the BASH script)
The second value is the current value in A multiplied by 100, for the same reasons. If this value is negative, that means the batteries are discharging to power the RPi
The third value is the power in W
The fourth value is the battery level in %. To be honest I don’t understand yet how this is calculated. I’ll have to come back to the formula and try to figure it out.

3. Shell script

This shell script is going to call the python script, log some info then determine if the battery level is too low. In that case it will shutdown the RPi. The relevant part is:

LOW=30
MAXVOLT=834
MINVOLT=690

The threshold is either battery % is below 30, or the voltage is below 6.9 V (remember the data is x 100).

4. Logrotate configuration

To be placed in /etc/logrotate.d/ folder on the RPi, it will rotate the logs once per week:

/var/log/powerlog.log
{
rotate 1
weekly
missingok
notifempty
compress
delaycompress
}

The first line tells which file to rotate.
In the configuration section, the parameters are almost self-explanatory:
– rotate 1: 1 rotate per cycle
– weekly: the rotation cycle
– missingok: don’t throw an error if the file is not found
– notifempty: don’t rotate if the file is empty
– compress: compress the archived file
– delaycompress: perform the compression after the rotation is done

Now all is left to do is to put the files in a directory and configure CRON to run it every minutes. I selected to put the files in the /opt/powerlog directory. Therefore my /etc/crontab look like this (only relevant line shown):

* * * * root /opt/powerlog/powerlog.sh

That’s all! When the power is below the threshold your RPi will safely shutdown.

One Reply to “Monitoring UPS HAT from a RPi”

Comments are closed.