|
|
|
The APC UPS units have a serial cable connection on the rear of the unit. The unit includes some software that will shutdown your PC if the power fails. HomeSeer can also be used to detect a power failure and then run an event to take some action.
To do this, you simply connect a serial cable between one of your PC's serial ports and the UPS unit. When the power fails, the UPS sends a BREAK over the serial line. By using a script and opening the serial port, you can detect this condition. The following script should be run when HomeSeer starts up. It can be called from the HomeSeer startup script "startup.txt".
Create a new script and name it "comm_apc.txt". Copy the following text to the script. Save the script in your HomeSeer scripts folder.
|
sub main()
dim result
result = hs.OpenComPort(2,"2400,N,8,1",0,"comm_apc.txt","apc_event")
if result <> "" then
hs.WriteLog "Error","Error opening COM port for UPS: "&result
end if
end sub
sub apc_event(data)
if data = 1001 then
hs.speak "the power has failed",true
end if
end sub |
|
The script works as follows. At startup, the "main" function is called and the COM port is opened. If the COM port cannot be opened, it logs an error in the event log. The COM port will now remain open so HomeSeer can detect any changes.
When there is a change in the status of the COM port, the script is called again (named "comm_apc.txt" here), and the function "apc_event" is called. The function is called with an event code. If the change was due to the reception of data, the parameter would be a data buffer. Since the APC unit does not send data, the parameter will be a code number. For the APC unit, the code 1001 is a "Break" condition, and this is the code we need to check for. For reference purposes, here is a list of some other codes we could get:
|
| comEventBreak |
1001 |
A Break signal was received. |
| comEventFrame |
1004 |
Framing Error. The hardware detected a framing error. |
| comEventOverrun |
1006 |
Port Overrun. A character was not read from the hardware before the next character arrived and was lost. |
| comEventRxOver |
1008 |
Receive Buffer Overflow. There is no room in the receive buffer. |
| comEventRxParity |
1009 |
Parity Error. The hardware detected a parity error. |
| comEventTxFull |
1010 |
Transmit Buffer Full. The transmit buffer was full while trying to queue a character. |
| comEventDCB |
1011 |
Unexpected error retrieving Device Control Block (DCB) for the port. |
|
The event portion of the script checks for this code, and if we get it, it speaks a message. It could also trigger an event using: hs.TriggerEvent.
|
|