/[svn.andrew.net.au]/scripts/read-temperature.py
ViewVC logotype

Contents of /scripts/read-temperature.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 24 - (show annotations)
Thu Jun 14 04:16:40 2007 UTC (14 years, 3 months ago) by apollock
File MIME type: text/x-python
File size: 3104 byte(s)
Actually exit if the serial module is unavailable

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 import sys
27 try:
28 import serial
29 except ImportError:
30 print "This program requires pyserial,", \
31 "try checking the python-serial package is installed"
32 sys.exit(-1)
33
34 from optparse import OptionParser
35
36 def read_temperature(device, sensor):
37 ser = serial.Serial(device, 9600, timeout=1)
38
39 ser.write(SENSOR_QUERY_CMD[sensor])
40 buf = ser.read(8)
41 ser.close()
42
43 temp = ord(buf[0]) | (ord(buf[1]) << 8)
44 return temp
45
46
47 SENSOR_QUERY_CMD = (None, 'S', 'T', 'U')
48 USAGE = "usage: %prog [options] [action]"
49
50 parser = OptionParser(USAGE)
51 parser.add_option("-F", "--fahrenheit", action="store_false",
52 dest="use_celsius", help="Output temperature in degrees Fahrenheit",
53 default=False)
54 parser.add_option("-C", "--celsius", action="store_true",
55 dest="use_celsius", help="Output temperature in degrees Celsius",
56 default=True)
57 parser.add_option("-d", "--device", action="store", type="string",
58 dest="device", help="device to communicate with",
59 default="/dev/ttyUSB0")
60 parser.add_option("-s", "--sensor", action="store", type="int",
61 dest="sensor", help="sensor to query",
62 default="1")
63 parser.add_option("--mrtg", action="store_true",
64 dest="output_mrtg", help="Output decimal value suitable for MRTG",
65 default=False)
66 (options, args) = parser.parse_args()
67
68 if not(options.sensor > 0 and options.sensor <= len(SENSOR_QUERY_CMD) - 1):
69 parser.error("Invalid sensor")
70
71 try:
72 temp = read_temperature(options.device, options.sensor)
73 except serial.SerialException, e:
74 print "%s" % (e)
75 sys.exit(1)
76
77 degrees = temp / 16.0
78
79 # The power-up temperature of the sensor is 85 degrees, which is fairly unlikely
80 # So if we get that temperature, try reading it again
81
82 if degrees == 85.0:
83 try:
84 temp = read_temperature(options.device, options.sensor)
85 except serial.SerialException, e:
86 print "%s" % (e)
87 sys.exit(1)
88 degrees = temp / 16.0
89
90 if (options.use_celsius):
91 if (options.output_mrtg):
92 print "%.0f" % degrees
93 else:
94 print "%.4f degrees Celsius" % degrees
95 else:
96 degrees = (9.0/5.0) * degrees + 32.0
97 if (options.output_mrtg):
98 print "%.0f" % degrees
99 else:
100 print "%.4f degrees Fahrenheit" % degrees

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.22