Control APC UPS through USB

Post new topic   Reply to topic    DD-WRT Forum Index -> Broadcom SoC based Hardware
Author Message
emildev
DD-WRT User


Joined: 12 Dec 2009
Posts: 70

PostPosted: Sat Jan 30, 2010 9:30    Post subject: Control APC UPS through USB Reply with quote
Hi there,

Has anybody tried controlling an APC UPS with a USB cable through DD-WRT? I have found the following software that might work:

http://www.apcupsd.com

This would allow the router to control the UPS, for example shutdown the router and then the UPS when low on power.

Most of the new Linksys Routers now comes with a USB port.

Any suggestions
Sponsor
robertut
DD-WRT User


Joined: 30 Mar 2009
Posts: 145

PostPosted: Sat Feb 06, 2010 21:16    Post subject: Reply with quote
Alright, here's how I did. It works fine on my Asus WL-500gP with DD-WRT v24-sp2 Big (SVN revision 13401M NEWD Eko).

1. installed Optware on a 512MB USB stick according to this guide (see this topic also)

2. enabled USB 1.1 Support (UHCI) in the web interface (Services > USB > USB 1.1 Support UHCI = enable). Router rebooted. (You may have to enable OHCI instead, if the next steps fail, it really depends on your router hardware which is the right option)

3. issued the following command via telnet/ssh:
Code:
cat /proc/bus/usb/devices

it should output many things, but what's interesting is:
Quote:
T: Bus=01 Lev=01 Prnt=01 Port=01 Cnt=01 Dev#= 6 Spd=1.5 MxCh= 0
D: Ver= 1.10 Cls=00(>ifc ) Sub=00 Prot=00 MxPS= 8 #Cfgs= 1
P: Vendor=051d ProdID=0002 Rev= 1.00
S: Manufacturer=American Power Conversion
S: Product=Back-UPS 500 FW: 6.5.I USB FW: c1
S: SerialNumber=BB0303017030
C:* #Ifs= 1 Cfg#= 1 Atr=a0 MxPwr= 30mA
I: If#= 0 Alt= 0 #EPs= 1 Cls=03(HID ) Sub=00 Prot=00 Driver=none
E: Ad=81(I) Atr=03(Int.) MxPS= 8 Ivl=10ms
The above confirms the situation described in apcupsd manual, no hid driver is loaded by the kernel:
Code:
cat /proc/bus/usb/drivers
gives
Quote:
usbdevfs
hub
usb-storage
only.
Get the HID driver from here, unpack hid-linux-2.4.35-mips.tar.gz to /opt/lib/modules/hid.o and install it with the following command:
Code:
insmod -f /opt/lib/modules/hid.o
(note the -f switch, the kernel on the router is 2.4.37, while the driver was compiled for 2.4.35, however with -f it installs and works fine, despite some warnings at installation)

After this cat /proc/bus/usb/devices will display this, among others:
Quote:
T: Bus=01 Lev=01 Prnt=01 Port=01 Cnt=01 Dev#= 8 Spd=1.5 MxCh= 0
D: Ver= 1.10 Cls=00(>ifc ) Sub=00 Prot=00 MxPS= 8 #Cfgs= 1
P: Vendor=051d ProdID=0002 Rev= 1.00
S: Manufacturer=American Power Conversion
S: Product=Back-UPS 500 FW: 6.5.I USB FW: c1
S: SerialNumber=BB0303017030
C:* #Ifs= 1 Cfg#= 1 Atr=a0 MxPwr= 30mA
I: If#= 0 Alt= 0 #EPs= 1 Cls=03(HID ) Sub=00 Prot=00 Driver=hid
E: Ad=81(I) Atr=03(Int.) MxPS= 8 Ivl=10ms
, and
Code:
cat /proc/bus/usb/drivers
gives
Quote:
usbdevfs
hub
usb-storage
96-111: hiddev
hid
which is great!

4. Installed the latest apcupsd from optware repository with the command:
Code:
ipkg install apcupsd


5. Configured these values inside /opt/etc/apcupsd/apcupsd.conf:
Quote:
UPSNAME Router
UPSCABLE usb
UPSTYPE usb
DEVICE

(note the DEVICE line should be empty, just DEVICE and a space character, other default values are fine)

7. Created a /lock folder in /opt/var (to have /opt/var/lock for the lockfile)

8. Tests according the manual:
- command apctest displays a test menu for the UPS
- command apcaccess status displays UPS parameters
All the tests described in the manual should work.

After this, all you have to do is customize apcupsd to your needs (startup/shutdown actions, notifications, monitoring graph by munin, etc). Follow apcupsd manual for further details.

Good luck!


Last edited by robertut on Sat Feb 06, 2010 22:18; edited 4 times in total
robertut
DD-WRT User


Joined: 30 Mar 2009
Posts: 145

PostPosted: Sat Feb 06, 2010 21:22    Post subject: Reply with quote
To enable apcupsd at startup, I made an Optware service called "apcupsd", for the application apcupsd.

I made a file called S96apcupsd in /opt/etc/init.d with the following content.

The script loads the hid driver before running apcupsd, and unloads it after stopping it. Not the most proffessional way, but it works. It seems enough.

Code:
#!/bin/sh

prefix="/opt"
PATH=${prefix}/bin:${prefix}/sbin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=apcupsd
DAEMON=${prefix}/sbin/${NAME}
DAEMON_OPTS="-f ${prefix}/etc/apcupsd/apcupsd.conf"
HIDLOAD="insmod -f /opt/lib/modules/hid.o"
HIDUNLOAD="rmmod hid"

test -x $DAEMON || exit 0

if [ -z "$1" ] ; then
    case `echo "$0" | sed 's:^.*/\(.*\):\1:g'` in
        S??*) rc="start" ;;
        K??*) rc="stop" ;;
        *) rc="usage" ;;
    esac
else
    rc="$1"
fi

case "$rc" in
    start)
   echo "Loading HID driver..."
   $HIDLOAD > /dev/null
   sleep 5
   echo "Starting: $NAME"
   $DAEMON $DAEMON_OPTS
        ;;
    stop)
        if [ -n "`pidof $NAME`" ]; then
            echo "Stopping: $NAME..."
            killall $NAME 2> /dev/null
            sleep 5
            echo "Unloading HID driver"
            $HIDUNLOAD
        fi
        ;;
    restart)
        "$0" stop
        sleep 1
        "$0" start
        ;;
    *) 
        echo "Usage: $0 (start|stop|restart|usage)"
        ;;
esac

exit 0

(see Optware, the right way WiKi entry on using services)
emildev
DD-WRT User


Joined: 12 Dec 2009
Posts: 70

PostPosted: Sun Feb 07, 2010 9:20    Post subject: Reply with quote
Thank you robertut, this is probably 1 of the best answers I have received on the forum!

My 610v2 can only use K26 builds and the driver is for K24, for now I can see the ups but the driver is showing none - as expected.

I will browse around and see if I can find a K26 driver and then post back on progress.

Thank you again for taking the time to write such a comprehensive guide!

KInd regards, Emil
robertut
DD-WRT User


Joined: 30 Mar 2009
Posts: 145

PostPosted: Sun Feb 07, 2010 10:53    Post subject: Reply with quote
I am always happy to help - I helped myself too Cool

Please keep us updated on possible HID driver modules for different kernel versions. I'd love to see a proper module compiled for 2.4.37 and 2.6. Maybe someone could help us in this matter, who has developer environment installed?
billyburly
DD-WRT Novice


Joined: 03 Nov 2010
Posts: 2

PostPosted: Wed Nov 03, 2010 14:50    Post subject: Reply with quote
After some time I was able to get 2.6 modules compiled. In 2.6 I needed a few additional modules to get the UPS to load a driver.

However I am still having trouble getting apctest and apcupsd to talk to it. Any ideas?

modules needed:
input-core.ko
hid.ko
hidusb.ko


cat /proc/bus/usb/devices
Quote:
T: Bus=02 Lev=01 Prnt=01 Port=01 Cnt=01 Dev#= 3 Spd=1.5 MxCh= 0
D: Ver= 1.10 Cls=00(>ifc ) Sub=00 Prot=00 MxPS= 8 #Cfgs= 1
P: Vendor=051d ProdID=0002 Rev= 1.06
S: Manufacturer=APC
S: Product=Back-UPS NS 600 FW:818.w1 .D USB FW:w1
S: SerialNumber=3B1033X29042
C:* #Ifs= 1 Cfg#= 1 Atr=e0 MxPwr= 0mA
I:* If#= 0 Alt= 0 #EPs= 1 Cls=03(HID ) Sub=00 Prot=00 Driver=usbhid
E: Ad=81(I) Atr=03(Int.) MxPS= 6 Ivl=10ms


EDIT: removed old modules (they were not configured correctly. see post below for working modules)


Last edited by billyburly on Sun Apr 10, 2011 2:02; edited 2 times in total
ManateeMatt
DD-WRT Novice


Joined: 28 Dec 2010
Posts: 2

PostPosted: Tue Dec 28, 2010 15:56    Post subject: Reply with quote
Has anyone had any luck getting APCUPSD running on k2.6?

when I run apctest i get the following error

apctest: line 1: syntax error: word unexpected (expecting ")")

My UPS is detected

T: Bus=02 Lev=01 Prnt=01 Port=00 Cnt=01 Dev#= 4 Spd=1.5 MxCh= 0
D: Ver= 1.10 Cls=00(>ifc ) Sub=00 Prot=00 MxPS= 8 #Cfgs= 1
P: Vendor=051d ProdID=0002 Rev= 1.06
S: Manufacturer=APC
S: Product=Back-UPS ES 550 FW:843.K2 .D USB FW:K2
S: SerialNumber=4B1040P28278
C:* #Ifs= 1 Cfg#= 1 Atr=e0 MxPwr= 2mA
I:* If#= 0 Alt= 0 #EPs= 1 Cls=03(HID ) Sub=00 Prot=00 Driver=usbhid
E: Ad=81(I) Atr=03(Int.) MxPS= 6 Ivl=10ms

I think everything is setup, do i need anything more then apcupsd and the modules installed?
emerzon
DD-WRT Novice


Joined: 23 Jan 2010
Posts: 10

PostPosted: Fri Mar 04, 2011 3:47    Post subject: Reply with quote
ManateeMatt wrote:
Has anyone had any luck getting APCUPSD running on k2.6?

when I run apctest i get the following error

apctest: line 1: syntax error: word unexpected (expecting ")")

My UPS is detected

T: Bus=02 Lev=01 Prnt=01 Port=00 Cnt=01 Dev#= 4 Spd=1.5 MxCh= 0
D: Ver= 1.10 Cls=00(>ifc ) Sub=00 Prot=00 MxPS= 8 #Cfgs= 1
P: Vendor=051d ProdID=0002 Rev= 1.06
S: Manufacturer=APC
S: Product=Back-UPS ES 550 FW:843.K2 .D USB FW:K2
S: SerialNumber=4B1040P28278
C:* #Ifs= 1 Cfg#= 1 Atr=e0 MxPwr= 2mA
I:* If#= 0 Alt= 0 #EPs= 1 Cls=03(HID ) Sub=00 Prot=00 Driver=usbhid
E: Ad=81(I) Atr=03(Int.) MxPS= 6 Ivl=10ms

I think everything is setup, do i need anything more then apcupsd and the modules installed?


Try using ipkg-opt's version of apcupsd, the binary worked for me.

I am also trying to setup it in K2.6 and apparently the HID module needs to be recompiled with the CONFIG_USB_HIDDEV option on, else you will step in some error like this:

Code:

root@snowball:/opt/etc/apcupsd# apctest

2011-03-04 01:46:48 apctest 3.14.8 (16 January 2010) unknown
Checking configuration ...
Attached to driver: usb
sharenet.type = DISABLE
cable.type = USB_CABLE

You are using a USB cable type, so I'm entering USB test mode
mode.type = USB_UPS
Setting up the port ...
apctest FATAL ERROR in linux-usb.c at line 609
Cannot find UPS device --
For a link to detailed USB trouble shooting information,
please see <http://www.apcupsd.com/support.html>.
apctest error termination completed


I am trying to rebuild it from the keychain using those instructions:

http://iomega.nas-central.org/wiki/Category:Gigabit_Ethernet%281000%29

I just changed the target to ddwrt. Let's see if it works. I'll let you know if I get it working.
billyburly
DD-WRT Novice


Joined: 03 Nov 2010
Posts: 2

PostPosted: Sun Apr 10, 2011 2:00    Post subject: Reply with quote
emerzon got it right. had the kernel options wrong. included the right options and recompiled and now apctest sees the ups.

thanks, emerzon.

ive attached the properly configured kernel modules



hid.zip
 Description:

Download
 Filename:  hid.zip
 Filesize:  48.17 KB
 Downloaded:  1257 Time(s)

emerzon
DD-WRT Novice


Joined: 23 Jan 2010
Posts: 10

PostPosted: Sun Apr 10, 2011 3:21    Post subject: Reply with quote
billyburly wrote:
emerzon got it right. had the kernel options wrong. included the right options and recompiled and now apctest sees the ups.

thanks, emerzon.

ive attached the properly configured kernel modules


Thanks a lot billyburly! I wasn't able to compile the modules on my own Embarassed

I used your modules and now I can see the device appearing in dmesg:

Code:
hiddev96: USB HID v1.10 Device [American Power Conversion Back-UPS RS 1500 LCD FW:839.H7 .D USB FW:H7 ] on usb-0000:00:02.0-1


However, apctest still doesn't see it:

Quote:
2011-04-10 00:19:01 apctest 3.14.8 (16 January 2010) unknown
Checking configuration ...
Attached to driver: usb
sharenet.type = DISABLE
cable.type = USB_CABLE

You are using a USB cable type, so I'm entering USB test mode
mode.type = USB_UPS
Setting up the port ...
apctest FATAL ERROR in linux-usb.c at line 609
Cannot find UPS device --
For a link to detailed USB trouble shooting information,
please see <http://www.apcupsd.com/support.html>.
apctest error termination completed


Any idea what can I be missing?
motocrossmann
DD-WRT Novice


Joined: 05 Jan 2009
Posts: 39

PostPosted: Wed Nov 27, 2013 5:34    Post subject: Reply with quote
I'm getting close on this as well, but am missing something. After following the suggestions here on getting this working on k26, I am realizing that "ls -l /sys/bus/usb/drivers/" does not show HID drivers. (per apcupsd.com) I installed the HID.zip mentioned previously, but am clearly missing something.

EDIT:
I got it going. I'm not exactly sure what was tripping me up. I did chmod 777 all the *.ko files, but I'm not sure that is related. Anyways, here are the commands I run to get an HID driver loaded for my UPC UPS:

nsmod -f /mnt/usb/input-core.ko
insmod -f /mnt/usb/hid.ko
insmod -f /mnt/usb/usbhid.ko

Afterwards, I can confirm the driver loads with 'cat /proc/bus/usb/devices'. apctest works too (after configuring it's conf file)!!! Happy camper here... Thanks guys!
Display posts from previous:    Page 1 of 1
Post new topic   Reply to topic    DD-WRT Forum Index -> Broadcom SoC based Hardware All times are GMT

Navigation

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum