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

Annotation of /scripts/read-temperature.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 19 - (hide annotations)
Sun May 27 18:28:36 2007 UTC (15 years, 1 month ago) by apollock
File MIME type: text/x-python
File size: 2577 byte(s)
Close the serial port as soon as possible

1 apollock 18 #!/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 apollock 19 ser.close()
63 apollock 18
64     temp = ord(buf[0]) | (ord(buf[1]) << 8)
65    
66     degrees = temp / 16.0
67    
68     if (options.use_celsius):
69     if (options.output_mrtg):
70     print "%.0f" % degrees
71     else:
72     print "%.4f degrees Celsius" % degrees
73     else:
74     degrees = (9.0/5.0) * degrees + 32.0
75     if (options.output_mrtg):
76     print "%.0f" % degrees
77     else:
78     print "%.4f degrees Fahrenheit" % degrees

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.22