I noticed that the LCDproc package on pfSense doesn’t have a way to display UPS battery information. Well, what better way to dust off that PHP coding hat than to add it myself.
I had been using a 20x4 character LCD display from eBay for a while now and it hit me that I could also show UPS battery information at a glance rather than logging into pfSense. Handy too when the power goes out and I can visually see how much battery time is left.
So my needs were to; display current UPS status, runtime(hours, minutes, seconds) or battery voltage, battery and current load percentage.
Being rusty with PHP, i’m thankful for what was already there as I could base my code on that. A fair amount of time went into getting this up and running - a few months on and off every day. Looking back now at the amount of code I actually wrote is pretty small and I feel perplexed where my time went. But I have to remind myself, I was also learning at the same time and trying to make heads and tails of even simple code.
The logic is fairly straightforward, I’m using an existing function from the NUT UPS package(nut_ups_status()) that stores all the UPS data in an associative array. Then I pull out what data I need from that. Along with checks that the package is indeed installed.
As for displaying the data itself on the screen. I tried to cover some bases and assumptions on what type of LCD screen a user might use. Switch casing for widths and lengths. Dabbling with Ternary Operators for the if conditions here and there. Since the only width and height options within LCDproc itself are 1,2,4 and 20, 16, 12 respectively. I took the spacing of them into consideration and a safe default to cover future displays without an update to the code. I’m sure there’s parts that can be optimized but that’s where I’m currently at for PHP knowledge.
Recommended parts:
- 20x4 or 16x4 character LCD - 20x4 very strongly recommended
- Adafruit USB + Serial LCD Backpack Add-On - ADA781
- Code on my Github
Using the backpack is the easiest as it only requires soldering to the LCD panel and also provides brightness and contrast control through pfSense.
Putting together the hardware is pretty simple. The backpack is soldered on via pin headers and a mini usb is connected(or hardwired into the backpack).
In pfSense, under LCDProc. Select Matrix Orbital for Display Type and LKD with Has adjustable backlight checked. It’s worth trying different Contrast and Brightness levels. 10% Brightness and 80% Contrast works well for my application.
I have a 3D printed box made for the screen to sit in. I might upload the model files but they need adjusting before I do.
Code available on my GitHub