Created
April 24, 2010 15:19
-
-
Save mattfoster/377704 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env ruby | |
# Read current cost info. Output in a form suitble for cacti. | |
# On ubuntu, place in the following sudoers to allow running from cacti: | |
# www-data ALL=NOPASSWD: /usr/share/cacti/site/scripts/currentcost.rb | |
# To create device specific names, create: | |
# /etc/udev/rules.d/70-persistent-currentcost.rules containing: | |
# KERNEL=="ttyUSB*", SUBSYSTEM=="tty", ATTRS{manufacturer}=="Prolific Technology Inc.", SYMLINK+="currentcost%n" | |
require 'rubygems' | |
require 'serialport' | |
require 'rexml/document' | |
# Extract and print specified data. | |
def parse_to_string(line) | |
xml = REXML::Document.new(line) | |
output = $data.each.map do |key, value| | |
"#{key}:#{xml.elements[value].text.to_f}" | |
end | |
output.join(' ') | |
end | |
def print_forever(port) | |
while line = port.readline | |
STDERR.puts line if $DEBUG | |
parse_and_print(line) | |
end | |
end | |
# Names and values to output | |
$data = { | |
# 'src' => '//src', | |
# 'dsb' => '//dsb', | |
# 'time' => '//time', | |
# 'sensor' => '//sensor', | |
# 'id' => '//id', | |
# 'type' => '//type', | |
'temperature' => '//tmpr', | |
'power' => '//ch1/watts', | |
} | |
port = ARGV.shift || '/dev/ttyUSB0' | |
# Open a serial port, and read a single line. | |
SerialPort::open(port , 57600, 8, 1, SerialPort::NONE) do |port| | |
print parse_to_string(port.readline) | |
end | |
__END__ | |
# Example output from the device. | |
<msg><src>CC128-v0.12</src><dsb>00003</dsb><time>09:11:56</time><tmpr>19.0</tmpr><sensor>0</sensor><id>00077</id><type>1</type><ch1><watts>00380</watts></ch1></msg> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you're using the udev rule above, you can easily find all current costs devices using:
Dir['/dev/currentcost*']
Note: all prolific usb-serial adapters will probably show up in this glob / udev rule, so if you have more, this might need editing.