Last mod: 2024.11.11

Raspberry Pi - UPS

A Raspberry Pi paired with a UPS HAT offers a reliable power backup and status monitoring, essential for continuous operation. Along with providing backup power, we need custom script allows you to monitor the UPS's battery level, charge status.

Hardware

Raspberry and UPS HAT

Update software an install packages

Install requires packages:

sudo apt update
sudo apt upgrade -y
sudo apt-get install -y p7zip python3-smbus

Turn on I2C interface. Select Interfacing Options -> I2C ->yes to start the i2C kernel driver:

sudo raspi-config
sudo reboot

And after reboot:

wget https://files.waveshare.com/upload/d/d9/UPS_HAT.7z
7zr x UPS_HAT.7z
cd UPS_HAT/
python3 INA219.py 

We should see informations:

INA219 info

Create web page with status UPS

We will create a site based on the framweork flask and bootstrap:

sudo apt install -y python3-flask python3-flask-bootstrap

Let's create directory for flask project on Raspberry:

mkdir UpsStatus

Add files:

And finally run:

cd UpsStatus
python3 app.py

Open browser with IP and port (default value 5000) application, in my example http://192.168.3.42:5000 Status in Web browser

The page refreshes at the frequency defined in the template UpsStatus/templates/UpsStatus.html, line:

setInterval(fetchBatteryStatus, 1000);

We can see how the script works in the video:

Sources

https://gitlab.com/dziak.tech/examples/-/tree/main/IoT/UpsForRaspberry

Links

https://www.waveshare.com/ups-hat.htm?sku=18306 https://www.waveshare.com/wiki/UPS_HAT https://files.waveshare.com/upload/d/d9/UPS_HAT.7z