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

Diff of /scripts/read-temperature.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 22 by apollock, Sun May 27 19:46:53 2007 UTC revision 23 by apollock, Fri Jun 1 00:22:36 2007 UTC
# Line 32  except ImportError: Line 32  except ImportError:
32  from optparse import OptionParser  from optparse import OptionParser
33  import sys  import sys
34    
35    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  SENSOR_QUERY_CMD = (None, 'S', 'T', 'U')  SENSOR_QUERY_CMD = (None, 'S', 'T', 'U')
47  USAGE = "usage: %prog [options] [action]"  USAGE = "usage: %prog [options] [action]"
48    
# Line 57  if not(options.sensor > 0 and options.se Line 68  if not(options.sensor > 0 and options.se
68    parser.error("Invalid sensor")    parser.error("Invalid sensor")
69    
70  try:  try:
71    ser = serial.Serial(options.device, 9600, timeout=1)    temp = read_temperature(options.device, options.sensor)
72  except serial.SerialException, e:  except serial.SerialException, e:
73    print "%s" % (e)    print "%s" % (e)
74    sys.exit(1)    sys.exit(1)
75    
76  ser.write(SENSOR_QUERY_CMD[options.sensor])  degrees = temp / 16.0
 buf = ser.read(8)  
 ser.close()  
77    
78  temp = ord(buf[0]) | (ord(buf[1]) << 8)  # 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    
81  degrees = temp / 16.0  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    
89  if (options.use_celsius):  if (options.use_celsius):
90    if (options.output_mrtg):    if (options.output_mrtg):

Legend:
Removed from v.22  
changed lines
  Added in v.23

  ViewVC Help
Powered by ViewVC 1.1.22