Skip to main content

Setting up NUT on a Raspberry Pi for Greencell USB UPS

 



Background


This article provides a guide for installing a USB UPS attached to a Raspberry Pi. In my case I had purchased a GreenCell 600 360W USB UPS. Integrating the USB UPS requires using NUT (Network UPS Tools). This UPS uses the blazer_usb driver.

Installation NUT

  1. Plug-In the UPS USB-Connection to the host. My device showed up as Device 006 below:


# lsusb 
Bus 001 Device 006: ID 0001:0000 Fry's Electronics MEC0003
Bus 001 Device 005: ID 0bda:0309 Realtek Semiconductor Corp. USB3.0-CRW
Bus 001 Device 004: ID 0658:0200 Sigma Designs, Inc. Aeotec Z-Stick Gen5 (ZW090) - UZB
Bus 001 Device 003: ID 0424:ec00 Microchip Technology, Inc. (formerly SMSC) SMSC9512/9514 Fast Ethernet Adapter
Bus 001 Device 002: ID 0424:9514 Microchip Technology, Inc. (formerly SMSC) SMC9514 Hub


  1. Install the NUT package with the apt package manager: 

sudo apt-get install nut

  1. Make some configurations and changes in the nut directory /etc/nut:

# cd /etc/nut
drwxr-xr-x 2  root nut  4096   Jan 11 00:49 .
drwxr-xr-x 94 root root 4096   Jan 10 23:54 ..
-rw-r----- 1  root nut  1543   Jan 11 00:01 nut.conf
-rw-r----- 1  root nut  5571   Jan 11 00:12 ups.conf
-rw-r----- 1  root nut  4595   Jan 11 00:01 upsd.conf
-rw-r----- 1  root nut  2177   Jan 11 00:03 upsd.users
-rw-r----- 1  root nut  15362  Jan 11 00:16 upsmon.conf
-rw-r----- 1  root nut  3887   Jun  1  2018 upssched.conf

  1. In the nut.conf file we change the MODE to netserver:

# nano /etc/nut/nut.conf
MODE=netserver

  1. In the ups.conf we need to configure the right drivers for the UPS. You can get a compatibility list from the NUT-Server on https://networkupstools.org/stable-hcl.html

We make a new section for the UPS like in this example. As my UPS is connected via usb the port configuration can be set to auto. If you connect the UPS via a serial cable you need to configure it differently:

[greencell]
driver = "blazer_usb"
port = "auto"
desc = "Green Cell UPS"

  1. After setting up the right drivers we can make sure that the NUT-Server detects the UPS:

upsdrvctl start

  1. The output should look like this:

Network UPS Tools - UPS driver controller 2.7.4
Network UPS Tools - Generic HID driver 0.41 (2.7.4)
USB communication driver 0.33

  1. The upsd.conf has a LISTEN section. We need to configure it with the server ip-address so we can connect to the NUT-Server from other clients:

# nano /etc/nut/upsd.conf
LISTEN 192.168.x.x // Openhab server ip address
LISTEN 127.0.0.1 3493
LISTEN ::1 3493

In my example I’ve added the first ip-address (192.168.178.79). There is no need to write the port behind the address (3493) unless you want to change the default port.

  1. After that we need to make a new user in the upsd.users file:

# /etc/nut/upsd.users
[admin] 
password = ****  
actions = SET 
instcmds = ALL 
[monmaster] 
password = xxxxx 
upsmon master 
[lamppi] 
password = xxxxx 
upsmon slave 
[nextcloudpi]
password = xxxxx 
upsmon = slave 

You should add multiple users for the server and clients. See the manual page for upsd.users for more information.

  1. Now we configure the upsmon.conf there we need to add a MONITOR master

# /etc/nut/upsmon.conf
MONITOR greencell@192.168.x.x 1 monmaster upsmon0 master
POWERDOWNFLAG /etc/killpower
SHUTDOWNCMD "/sbin/shutdown -h now"

  1. As the system name uses the value in the brackets you made in the ups.conf the user and password was configured in the upsd.user file.

  2. Since we don’t want that other users can read this secret files we fix the permissions for our needs.


$ sudo chown root:nut /etc/nut/*
$ sudo chmod 640 /etc/nut/*

  1. At the end whe should reload both services upsd and upsmon. This can be done like this:

sudo systemctl restart nut-server 

Use the admin user to do this, you will be requested to enter username and password as above.

Testing a local Client request

  1. Now we can test if the NUT-Server works locally. I’ll make client request to this the instance name greencell:

upsc greencell

If everything works fine we get an output similar to this:

Init SSL without certificate database
battery.charge: 100
battery.voltage: 14.00
battery.voltage.high: 13.00
battery.voltage.low: 10.40
battery.voltage.nominal: 12.0
device.type: ups
driver.name: blazer_usb
driver.parameter.pollinterval: 2
driver.parameter.port: auto
driver.parameter.synchronous: no
driver.version: 2.7.4
driver.version.internal: 0.12
input.current.nominal: 2.0
input.frequency: 50.1
input.frequency.nominal: 50
input.voltage: 238.3
input.voltage.fault: 238.3
input.voltage.nominal: 230
output.voltage: 238.3
ups.beeper.status: enabled
ups.delay.shutdown: 30
ups.delay.start: 180
ups.load: 3
ups.productid: 0000
ups.status: OL
ups.type: offline / line interactive
ups.vendorid: 0001

Set up clients/slaves

  1. First, the necessary packages must be installed on a client:

sudo apt-get install nut-client

  1. This time we only have to edit two files. In /etc/nut/nut.conf we set the mode:

MODE=netclient

  1. Then we need to tell upsmon which UPS to monitor (in /etc/nut/upsmon.conf):

MONITOR greencell@192.168.178.79 1 lamppi upsmon1 slave

The IP and password must be adjusted. Please also note the slave at the end!

  1. Finally, restart the corresponding service and check that everything is running:

$ sudo systemctl restart nut-client

Congratulations! Now everything should be set up. 

  1. Check monitoring is working by issuing the following command on the nut server (openhabpi):

$ upsc -c greencell

The following information should be returned:

Init SSL without certificate database
192.168.178.93
192.168.178.79 

but how can you test that it really works?

Testing shutdown

The following command triggers the shutdown signal (as it would be in the event of a power failure) and shutdown all servers:

$ sudo upsmon -c fsd

All servers should shutdown (so be careful about using this command)! 

 


Comments

Popular posts from this blog

Sending SMS Messages from openHAB

Background So I setup sending SMS messages from my Raspberry Pi (see article I wrote here ) and have a bash script to automate this. The next step is to integrate this into my openHAB server, so I can send SMS messages to my mobile phone when my UPS system (managed by openHAB) is on battery power (e.g. due to a mains network failure). Installing the EXEC Binding to openHAB See here . Note especially the requirement to add commands to the whitelist. Adding a Thing to openHAB Once the EXEC binding is installed the next step is to setup a thing file to use the binding for sending bash shell commands:  $ nano /etc/openhab/things/exec.things Add the following line: Thing exec:command:sendsms [ command="/usr/local/bin/sendsms.sh %2$s", autorun=true, interval=0 ] Save the file. As you can see this thing is used to pass the command to run the bash shell script to send an SMS and pass the text sent as "%2$s" to the script. Adding Items to openHAB Two items are required, one

SMS messaging on Raspberry Pi with Gammu

Background Ever wanted to be alerted when the power fails at home? I typically worry that food in the freezer will go off when I am on vacation! (Yep, that's how I tick). I have recently installed a UPS system which I use to protect various IT and network devices from instant power loss, enabling them to shutdown gracefully. This is particulary important for my Raspberry Pi (Pi) devices, which are sensitive to this issue, as SD-Cards can be corrupted and then not re-boot.  The UPS I have installed interfaces via USB with my Raspberry Pi server using the Linux Network UPS tools NUT so that I can monitor status changes (e.g. grid power loss) and this is further integrated into my openHAB -based home automation system. This setup will be covered in another post.  I wanted to additionally be alerted when grid power fails via SMS to my mobile phone. To do this I have purchased a pre-owned USB Internet Surf Stick and a pay-as-you-go SIM card (zero cost) which I can load with credit. I a