The X728 is an power supply expansion board for all current models of the Raspberry Pi using a 40 pin header. The software provided by the vendor is a shell script and couple of python scripts that rather showcases how to communicate with the board.
I instead wanted to build my own daemonized service running on Pi to does all the job, that was:
- Handling physical button on board to gracefully shutdown or reboot RPi
- Monitor battery capacity and voltage and report it via MQTT
- Send a warning via MQTT, if battery voltage is below certain value
- Send a critical warning via MQTT, if battery drops below certain level
- Gracefully shutdown RPi, if AC voltage is missing and critical warning about battery level were triggered
- Send an alert via MQTT, if AC power is off
- Finally, MQTT command topic to trigger gracefull reboot or shutdown of my RPi
Which is actually a features that are currently implemented.
All aspects of MQTT thing is configurable via config.ini as follows.
| Config param name | Default value if not set | Current value | Description |
|---|---|---|---|
| mqtt_host | localhost | localhost | MQTT hostname or IP |
| mqtt_port | 1883 | 1883 | MQTT Port |
| status_interval | 60 | 120 | Battery and AC Power status reporting interval in seconds |
And topics related configuration
| Config param name | Default value if not set | Current value | Description | Values |
|---|---|---|---|---|
| lwt_topic | x728/LWT | home/tele/oh-ups/LWT | Last Will Topic | ONLINE/OFFLINE |
| alert_battery_topic | x728/stat/ALARM | home/alert/oh-ups/BATTERY | Alerts battery reached certain level of voltage | WARNING (below 3.5V), CRITICAL (below3.0V) |
| battery_stat_topic | x728/stat/BATTERY | home/stat/oh-ups/BATTERY | Status about UPS battery condition - Voltage and Capacity (%) | {"Voltage": "0.0", "Capacity": "90"} |
| acpower_stat_topic | x728/stat/ACPOWER | home/stat/oh-ups/ACPOWER | Status of the AC Power. Reported every status interval, or when changes | ON/OFF |
| shutdown_cmnd_topic | x728/cmd/SHUTDOWN | home/cmnd/oh-ups/SHUTDOWN | Command topic to gracefully shutdown or reboot RPi. Action happens after 10s | REBOOT/SHUTDOWN |
| shutdown_stat_topic | x728/stat/SHUTDOWN | home/stat/oh-ups/SHUTDOWN | Status of the shutdown command - some form of confirmation | REBOOT/SHUTDOWN/UNKNOWN |
- Python 3.10 + PIP 3
X728 service is running on pi user by default (if you have installed on different user follow the same steps). The user running the service must be a password-less sudoers with minium required rights to execute /sbin/shutdown command only.
If your user has rights to sudo without password you're ready to go.
If not, simply create new files in /etc/sudoers.d/ folder to enable that and restart your machine.
echo "pi ALL=NOPASSWD: /sbin/shutdown" > /etc/sudoers.d/010_shutdownExcept changing the values that are currently in config.ini nothing special is required.
pip install -r requirements.txt
- Copy systemd.service file
cp systemd/x728-daemon.service /etc/systemd/system- Set appropriate permissions to file
sudo chown root:root /etc/systemd/system/x728-daemon.service
sudo chmod 644 /etc/systemd/system/x728-daemon.service- And reload systemd daemon
sudo systemctl daemon-reload- Enable and start your service
sudo systemctl enable x728-daemon.service
sudo systemctl start x728-daemon.service- You can check status of the service
sudo systemctl status x728-daemon.service- And monitor logs
sudo journalctl --unit x728-daemon.service -fIn case of any issued. You can run your service in a standalone mode, just stop the systemd services and run the service in debug mode
python3 daemon.py -c config.ini -d- MQTT over SSL
- MQTT username/password
- Configurable triggers of battery alerts
- Configurable delay between shutdown/reboot