/[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 22 - (show annotations)
Sun May 27 19:46:53 2007 UTC (14 years, 11 months ago) by apollock
File MIME type: text/x-python
File size: 2662 byte(s)
Gracefully handle failing to open the serial port

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 import sys
34
35 SENSOR_QUERY_CMD = (None, 'S', 'T', 'U')
36 USAGE = "usage: %prog [options] [action]"
37
38 parser = OptionParser(USAGE)
39 parser.add_option("-F", "--fahrenheit", action="store_false",
40 dest="use_celsius", help="Output temperature in degrees Fahrenheit",
41 default=False)
42 parser.add_option("-C", "--celsius", action="store_true",
43 dest="use_celsius", help="Output temperature in degrees Celsius",
44 default=True)
45 parser.add_option("-d", "--device", action="store", type="string",
46 dest="device", help="device to communicate with",
47 default="/dev/ttyUSB0")
48 parser.add_option("-s", "--sensor", action="store", type="int",
49 dest="sensor", help="sensor to query",
50 default="1")
51 parser.add_option("--mrtg", action="store_true",
52 dest="output_mrtg", help="Output decimal value suitable for MRTG",
53 default=False)
54 (options, args) = parser.parse_args()
55
56 if not(options.sensor > 0 and options.sensor <= len(SENSOR_QUERY_CMD) - 1):
57 parser.error("Invalid sensor")
58
59 try:
60 ser = serial.Serial(options.device, 9600, timeout=1)
61 except serial.SerialException, e:
62 print "%s" % (e)
63 sys.exit(1)
64
65 ser.write(SENSOR_QUERY_CMD[options.sensor])
66 buf = ser.read(8)
67 ser.close()
68
69 temp = ord(buf[0]) | (ord(buf[1]) << 8)
70
71 degrees = temp / 16.0
72
73 if (options.use_celsius):
74 if (options.output_mrtg):
75 print "%.0f" % degrees
76 else:
77 print "%.4f degrees Celsius" % degrees
78 else:
79 degrees = (9.0/5.0) * degrees + 32.0
80 if (options.output_mrtg):
81 print "%.0f" % degrees
82 else:
83 print "%.4f degrees Fahrenheit" % degrees

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.22