Connecting UPS Battery Backup to Router with OpenWrt
Recently I decided to connect an UPS (uninterruptible power supply) at work to one of the devices that runs 24 hours a day. The UPS that we currently have is the APC Back-UPS 1400 (Back-UPS XS 1400U), in addition to the ability to connect up to 6 devices, allows you to monitor the status of the device using the PowerChute software via USB port.
The problem is that this software is not compatible with the server we use, since it is available in a version for personal use (Personal Edition).
So I decided to use one free port (we use another one for additional internet connection using a 4G router) to connect a UPS to it to monitor its status (this device does not have a display apart from two diodes). If we can do something more, it would be even better, but it all depends on what can be achieved using OpenWrt.
Initial Device Detection
Let’s start by seeing how our UPS is detected when connected to our router.
We connect our UPS and from terminal we execute the command dmesg
In my case I got the following entry:
usb 1-1: new low-speed USB device number 3 using orion-ehci
As we can see, my device is detected as a slow USB device and the driver that supports it is orion-ehci
.
Proper Device Detection
First, we update the packages in the system and install the usbutils
package
opkg update
opkg install usbutils
We check if our UPS is correctly detected using lsusb
Bus 001 Device 003: ID 051d:0002 American Power Conversion Back-UPS XS 1400U FW:926.T2 .I USB FW:T2
UPS Status Monitoring
We install the apcusbd
package (and accompanying packages):
opkg install apcupsd collectd-mod-apcups kmod-usb-hid
After disconnecting and connecting the USB cable, my UPS is detected (lsusb
) as follows:
usb 1-1: USB disconnect, device number 5
usb 1-1: new low-speed USB device number 6 using orion-ehci
hid-generic 0003:051D:0002.0003: hiddev96,hidraw0: USB HID v1.10 Device [American Power Conversion Back-UPS XS 1400U FW:926.T2 .I USB FW:T2 ] on usb-f1058000.usb-1/input0
In the configuration file /etc/apcupsd/apcupsd.conf
I made the following changes:
UPSNAME ups1
UPSCABLE usb
#UPSTYPE apcsmart
UPSTYPE usb
#DEVICE /dev/ttyS0
Notice the commented (#) lines like DEVICE
We run it using:
/etc/init.d/apcupsd start
Then executing the apcaccess
command we should get the following response:
APC : 001,036,0873
DATE : 2022-02-04 12:28:51 +0000
HOSTNAME : WRT3200ACM
VERSION : 3.14.14 (31 May 2016) unknown
UPSNAME : ups1
CABLE : USB Cable
DRIVER : USB UPS Driver
UPSMODE : Stand Alone
STARTTIME: 2022-02-04 12:28:47 +0000
MODEL : Back-UPS XS 1400U
STATUS : ONLINE
LINEV : 228.0 Volts
LOADPCT : 22.0 Percent
BCHARGE : 100.0 Percent
TIMELEFT : 26.5 Minutes
MBATTCHG : 5 Percent
MINTIMEL : 3 Minutes
MAXTIME : 0 Seconds
SENSE : Medium
LOTRANS : 160.0 Volts
HITRANS : 280.0 Volts
ALARMDEL : No alarm
BATTV : 27.1 Volts
LASTXFER : Automatic or explicit self test
NUMXFERS : 0
TONBATT : 0 Seconds
CUMONBATT: 0 Seconds
XOFFBATT : N/A
SELFTEST : NO
STATFLAG : 0x05000008
SERIALNO : xxxxxxxxxxxx
BATTDATE : 2021-12-16
NOMINV : 230 Volts
NOMBATTV : 24.0 Volts
NOMPOWER : 700 Watts
FIRMWARE : 926.T2 .I USB FW:T2
END APC : 2022-02-04 12:28:52 +0000
As you can see from the STATUS: ONLINE
field the connection was successful.
If, however, we see STATUS: COMMLOST
, then our UPS was not detected or the connection is not working.
Status Monitoring from the OpenWrt Admin Panel
We install the statistics collection package:
opkg install luci-app-statistics
This command will additionally install the packages
librrd1
,rrdtool1
,collectd-mod-rrdtool
,collectd-mod-iwinfo
,collectd-mod-cpu
,collectd-mod-memory
,collectd-mod -interface
,collectd-mod-load
,collectd-mod-network
.
After going to the administration panel of our router, we will see a new Statistic
option.
When you run it for the first time and go to the Graphs
tab, you will be informed that the part responsible for collecting data has not been configured.
We will then be redirected to the Setup
part where we are actually interested in the General plugins
tab where we need to check APC
(confirm the default settings - Save), and then Save & Apply the changes.
Now, going back to the Graphs
section we should see our UPS which will fill our graphs over time.
Here it should be noted that the collected statistics are saved by default in the device memory and are lost after the device is restarted. If we want to change this, in the Setup
section, Output plugins
tab, we configure RRDTool
and change the location in the Storage directory
.
And this way we have UPS connected to our router, and we can monitor its status from a web browser.
Now it would be useful to do a little more, such as safely turning off the router or other devices connected to it if the battery level gets very low.
For this purpose, I became interested in the NUT (Network UPS Tools) package, which theoretically will allow you to monitor the status of the UPS (which we have connected via USB cable to our router) from the level of devices in the network. When the software detects a drop in power and at the appropriate battery level, it will react by executing a specific command.
It all sounds great, because you can install it on another computer (server) that is protected by our power supply, and even though the power supply is not directly connected to it, react appropriately depending on the situation based on data from the program.
But that’s fun for another time.
If you want to try your hand at NUT, I refer you to: Monitoring UPS status with NUT(Network UPS Tools) on OpenWrt – Linux Man (linux-man.org).
Best regards.
Comments & Reactions