1 |
#!/usr/bin/python |
2 |
|
3 |
### |
4 |
# Program to read the temperature from a DLP-TEMP-G from a Linux computer |
5 |
# http://www.dlpdesign.com/usb/temp.shtml |
6 |
# |
7 |
# Loosely based on Tim Jensen's program at |
8 |
# http://www.jensens.org/tim/read-temperature.txt |
9 |
# |
10 |
# Copyright (c) 2007 Andrew Pollock <me@andrew.net.au> |
11 |
# |
12 |
# This program is free software; you can redistribute it and/or modify |
13 |
# it under the terms of the GNU General Public License as published by |
14 |
# the Free Software Foundation; either version 2 of the License. |
15 |
# |
16 |
# This program is distributed in the hope that it will be useful, |
17 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 |
# GNU General Public License for more details. |
20 |
# |
21 |
# You should have received a copy of the GNU General Public License along |
22 |
# with this program; if not, write to the Free Software Foundation, Inc., |
23 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
24 |
# |
25 |
|
26 |
try: |
27 |
import serial |
28 |
except ImportError: |
29 |
print "This program requires pyserial,", \ |
30 |
"try checking the python-serial package is installed" |
31 |
|
32 |
from optparse import OptionParser |
33 |
|
34 |
SENSOR_QUERY_CMD = (None, 'S', 'T', 'U') |
35 |
USAGE = "usage: %prog [options] [action]" |
36 |
|
37 |
parser = OptionParser(USAGE) |
38 |
parser.add_option("-F", "--fahrenheit", action="store_false", |
39 |
dest="use_celsius", help="Output temperature in degrees Fahrenheit", |
40 |
default=False) |
41 |
parser.add_option("-C", "--celsius", action="store_true", |
42 |
dest="use_celsius", help="Output temperature in degrees Celsius", |
43 |
default=True) |
44 |
parser.add_option("-d", "--device", action="store", type="string", |
45 |
dest="device", help="device to communicate with", |
46 |
default="/dev/ttyUSB0") |
47 |
parser.add_option("-s", "--sensor", action="store", type="int", |
48 |
dest="sensor", help="sensor to query", |
49 |
default="1") |
50 |
parser.add_option("--mrtg", action="store_true", |
51 |
dest="output_mrtg", help="Output decimal value suitable for MRTG", |
52 |
default=False) |
53 |
(options, args) = parser.parse_args() |
54 |
|
55 |
if not(options.sensor > 0 and options.sensor <= len(SENSOR_QUERY_CMD) - 1): |
56 |
parser.error("Invalid sensor") |
57 |
|
58 |
ser = serial.Serial(options.device, 9600, timeout=1) |
59 |
|
60 |
ser.write(SENSOR_QUERY_CMD[options.sensor]) |
61 |
buf = ser.read(8) |
62 |
|
63 |
temp = ord(buf[0]) | (ord(buf[1]) << 8) |
64 |
|
65 |
degrees = temp / 16.0 |
66 |
|
67 |
if (options.use_celsius): |
68 |
if (options.output_mrtg): |
69 |
print "%.0f" % degrees |
70 |
else: |
71 |
print "%.4f degrees Celsius" % degrees |
72 |
else: |
73 |
degrees = (9.0/5.0) * degrees + 32.0 |
74 |
if (options.output_mrtg): |
75 |
print "%.0f" % degrees |
76 |
else: |
77 |
print "%.4f degrees Fahrenheit" % degrees |