Pradeep Singh | 1st May 2016
Most of the APC UPS have a Serial Port (RJ-45 Socket) which can be used to connect UPS with a host machine. Though the main idea behind this connection is to shutdown the Host gracefully in case of any power problem, it can do much more than that. Using this serial link, UPS posts information about all the power events to host machine and also allows the Host to run tests related to UPS, Power and Batteries.
As you might have noticed this connection between UPS and Host works well but most of the features remains limited within the host. Here in this article we will see how to extend some of these features to IoT Network using MQTT.
What are we trying to do here?
UPS will send all the power events (For Ex: powerout, onbattery, offbattery, mainsback etc ) to Raspberry Pi, if we install ‘apcupsd’ and connect it with the UPS using the Serial Cable (RJ45 <–> USB). All these UPS Events can then be forwarded to MQTT Broker, which will allow us to monitor it remotely.
You can see the connectivity and information flow in following picture –
What is APCUPSD?
APCUPD stands for APC UPS Daemon and used for controlling APC UPSes and some OEM APC power supplies. It runs on Linux, UNIX, Mac OS X and Windows. You can find more details about it at http://www.apcupsd.org/.
To make your UPS publish power events to MQTT Broker you can follow first three out of following four points –
- Install and Configure APCUPSD on Raspberry Pi
- Configure Raspberry Pi to publish UPS Events to MQTT Topic
- Test with MQTT Client
- Uninstall APCUPSD
A. Install and Configure APCUPSD on Raspberry Pi (Raspbian OS):
Install apcupsd –
sudo apt-get install apcupsd
Open apcupsd.conf file in vi editor –
sudo vi /etc/apcupsd/apcupsd.conf
Change UPSNAME, UPSCABLE, UPSTYPE and DEVICE variables based on your UPS type and save the file. Following settings seem to work well for APC Back-UPS –
UPSNAME APC_UPS UPSCABLE usb UPSTYPE usb DEVICE
Open apcupsd file in vi editor –
sudo vi /etc/default/apcupsd
Change ISCONFIGURED from ‘no’ to ‘yes’ and save the file –
Start apcupsd service –
sudo /etc/init.d/apcupsd start
Check the connection between Raspberry Pi and APC UPS –
service apcupsd status
If you see output similar to this you have successfully installed ‘apcupsd’ on your Raspberry Pi –
pi@raspberrypi ~ $ service apcupsd status APC : 001,037,0978 DATE : 2016-05-02 05:55:40 +0000 HOSTNAME : raspberrypi VERSION : 3.14.10 (13 September 2011) debian UPSNAME : APC_UPS CABLE : USB Cable DRIVER : USB UPS Driver UPSMODE : Stand Alone STARTTIME: 2016-05-02 05:03:57 +0000 MODEL : Back-UPS RS 1500G-IN STATUS : ONLINE LINEV : 225.0 Volts LOADPCT : 9.0 Percent Load Capacity BCHARGE : 100.0 Percent TIMELEFT : 53.1 Minutes MBATTCHG : 5 Percent MINTIMEL : 3 Minutes MAXTIME : 0 Seconds SENSE : Low LOTRANS : 170.0 Volts HITRANS : 294.0 Volts ALARMDEL : 30 seconds BATTV : 27.0 Volts LASTXFER : Unacceptable line voltage changes NUMXFERS : 2 XONBATT : 2016-05-02 05:08:54 +0000 TONBATT : 0 seconds CUMONBATT: 18 seconds XOFFBATT : 2016-05-02 05:09:04 +0000 SELFTEST : NO STATFLAG : 0x07000008 Status Flag SERIALNO : E21450008640 BATTDATE : 2014-12-15 NOMINV : 230 Volts NOMBATTV : 24.0 Volts NOMPOWER : 865 Watts FIRMWARE : 901.L4 .I USB FW:L4 END APC : 2016-05-02 05:56:39 +0000 pi@raspberrypi ~ $
B. Configure Raspberry Pi to publish UPS Events to MQTT Topic:
Install ‘paho-mqtt’ on Raspberry Pi –
pip install paho-mqtt
Download ‘mqtt_events_publisher.py’ Python script from Github into /etc/apcupsd directory –
sudo wget --directory-prefix=/etc/apcupsd https://raw.githubusercontent.com/pradeesi/apcupsd-on-Raspberry-Pi/master/mqtt_events_publisher.py
Change the MQTT Broker and MQTT Topic in ‘mqtt_events_publisher.py’ file (Broker value you leave as it is, but make sure you define your own unique MQTT Topic). This is how these two variables may look after you make the changes –
# Define Variables MQTT_BROKER = "iot.eclipse.org" MQTT_TOPIC = "testTopic/APC_UPS_EVENTS"
Assign execution permission to file –
sudo chmod +x /etc/apcupsd/mqtt_events_publisher.py
Open ‘apccontrol’ in vi editor –
sudo vi /etc/apcupsd/apccontrol
Add following line and save the file –
python /etc/apcupsd/mqtt_events_publisher.py $1
Restart apcupsd service –
sudo /etc/init.d/apcupsd restart
C. Test with MQTT Client:
For testing you may use any MQTT Client and subscribe on MQTT Broker with Topic you defined in ‘mqtt_events_publisher.py’ file.
- Easiest approach to test is would be with some MQTT Tool. For Ex – you can use ‘MQTTLens‘ which is a Google Chrome Application.
- If you have Python 2.7 installed on your machine you can install Paho MQTT module (pip install paho-mqtt) and use Subscriber Python Script to receive MQTT Messages.
D. Uninstall APCUPSD:
you can uninstall apcupsd from Raspberry Pi using apt-get command –
sudo apt-get --purge remove apcupsd