/[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 23 - (hide annotations)
Fri Jun 1 00:22:36 2007 UTC (15 years, 6 months ago) by apollock
File MIME type: text/x-python
File size: 3089 byte(s)
If the temperature is 85 degrees, it probably isn't, so read it a second time

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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.22