Using apcupsd on the Raspberry Pi?

apcupsd is a software package that allows you to use an uninterruptible power supply (UPS) with a Raspberry Pi. It communicates with the UPS over USB or serial to monitor the status and allows for safe shutdown of the Pi in the event of a power outage.

Using apcupsd on the Raspberry Pi?Why Use apcupsd on a Raspberry Pi

There are several benefits to using apcupsd with a Raspberry Pi:

  • It protects against data loss or file system corruption due to sudden power loss. This is especially important if your Pi is running important services.
  • You can configure automated safe shutdown of the Pi when the UPS battery runs low. This prevents unexpected shutdowns.
  • Status and alerts from the UPS can be monitored to understand battery health and runtime during an outage.
  • Unattended Pi systems can continue running in the event of power failures. The UPS handles power conditioning and battery backup.

Hardware Required

To use apcupsd with a Raspberry Pi, you will need:

  • Raspberry Pi
  • USB or serially-connected uninterruptible power supply (UPS)
  • Communication cable between UPS and Pi (USB or serial)

Many consumer UPS devices have USB connections and work well. Older UPS systems may require a serial cable and adapter. The UPS must support communication for monitoring status and initiating shutdown.

Installing apcupsd

apcupsd is included in most Raspberry Pi OS distributions. To install on Raspberry Pi OS:

sudo apt update

sudo apt install apcupsd

During installation you need to configure device and outlet settings that match your UPS model.

To verify installation:

systemctl status apcupsd

This completes the basic OS installation. Further configuration is required to enable communications and shutdown handling.

Configuring UPS Communications

After installation, apcupsd’s main configuration file needs adjustment to match your UPS device:

sudo nano /etc/apcupsd/apcupsd.conf

Some key parameters to set:

  • UPSTYPE – Set to match the protocol used to communicate with your UPS
  • DEVICE – The device path to communicate with your UPS
  • ONBATTERYDELAY – Seconds to wait after power failure before initiating shutdown

Save changes to apcupsd.conf after updating parameters.

Once configured, start and enable the apcupsd service:

sudo systemctl start apcupsd

sudo systemctl enable apcupsd

Use systemctl status apcupsd to verify correct operation.

Shutting Down Safely on Power Loss

To gracefully shutdown when the UPS battery runs low, we need to configure the Pi to listen for power events from apcupsd:

Edit /etc/apcupsd/apccontrol and configure these shutdown parameters:

What to do at the “Main” KillPower Level¬†

KILLPOWER 0

¬†What to do at the “Progressive” KillPower level

KILLPOWER 1

This initiates a safe system halt if the UPS reports a low battery shutdown request to apcupsd.

Monitoring Status from the UPS

Once up and running with apcupsd, status reports and metrics from the UPS can be monitored:

apcaccess status

Gives output on:

  • Battery Charge %
  • Remaining Runtime
  • UPS Load
  • Input/Output Voltage
  • Events like battery replacements

This data can be used to understand UPS performance over time.

Conclusion

Configuring apcupsd allows safe shutdown of a Raspberry Pi based on UPS battery thresholds. This protects against data loss and corruption during power outages for home automation and other applications running on a Pi. Paired with a supported USB/serial UPS device, the Pi can gracefully halt and preserve system integrity for short or long duration power events.

Key Takeaways

  • apcupsd enables UPS monitoring and management for Raspberry Pis
  • Gracefully shutdown Pi before UPS batteries are depleted
  • Protects against data loss or corruption during outages
  • Enables status monitoring for battery health metrics
  • Supported by most common USB/serial connected UPS devices

Frequently Asked Questions

Q: What UPS devices can I use with apcupsd on a Pi?
A: Most consumer UPS devices with USB connectivity are compatible. Serial models can work with a serial to USB adapter. apcupsd supports a wide range of popular models – check documentation for full list.

Q: Can I connect multiple Pis to a single UPS?
A: Yes, most UPS devices can support multiple systems. Each Pi would run its own instance of apcupsd and safely shutdown when on battery.

Q: Will my Pi shutdown suddenly during a quick power blip?
A: No. By default apcupsd waits 30 seconds (configurable) to see if input power returns before initiating system shutdown. Quick blips won’t disrupt the Pi.

Q: Is apcupsd compatible with Raspberry Pi OS or Ubuntu?
A: Yes, apcupsd works well with most common Pi operating systems like Raspberry Pi OS and Ubuntu. Debian packages handle most of the installation process.

Q: Can I monitor UPS status remotely from another system?
A: Yes. Once networked, apcupsd data is accessible remotely via secured ssh or by installing its CGI scripts into a web server for a simple web-based interface.

Q: What communication modes does apcupsd support?
A: It supports both USB and various serial protocols to match popular UPS hardware models. This covers most common modern devices and older serial models.

Q: Does the UPS need to match Raspberry Pi voltage?
A: No. The UPS handles power conditioning and voltage transformation. As long as the UPS has capacity to run the Pi load, voltages can differ.

Q: Is a pure sine wave UPS required?
A: For most Pi uses, simulated and step approximated sine wave models provide clean enough power. Pure sine wave models provide best power quality.

Q: How do I determine the size UPS I need?
A: Add up power draw in watts of all equipment to be connected, multiply by runtime required in minutes. Use a UPS that meets or exceeds this Total VA/Watt capacity.

Q: Can I put a UPS into maintenance mode to avoid wear on batteries?
A: On most models yes. apcupsd will detect and alert that utility power is disconnected and provide opportunity for a graceful shutdown before batteries are needed.

Q: How long should my UPS runtime be for a Pi?
A: For a graceful shutdown of Pi OS, 2-5 minutes is ideal. For finishing extended tasks 10-15 minutes may be preferred. Actual runtime needed depends on use case.

Q: Does the UPS replace a surge protector?
A: UPS devices include surge protection, so an additional surge protector is not necessary for equipment connected to the UPS. However, a protector can be used at the input of the UPS to further isolate it.

Q: What determines UPS battery life?
A: Frequency and duration of discharges, and ambient temperature predominantly. Batteries may last 3-5 years under ideal conditions, less with frequent extended discharges.

Q: How can I tell when UPS batteries need replacement?
A: Most UPS systems self-test batteries periodically. apcupsd will alert if metrics like estimated runtime drop below expected values, indicating aging batteries.

Q: Is there a software option for simulating power failures to test shutdown process?
A: Yes, apcupsd package includes apccontrol tool that can send custom shutdown and killpower commands to validate graceful shutdown configuration.

Leave a Comment