Guest User

temppi

a guest
May 9th, 2014
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.91 KB | None | 0 0
  1. import os
  2. import glob
  3. import time
  4.  
  5. os.system('modprobe w1-gpio')
  6. os.system('modprobe w1-therm')
  7.  
  8. base_dir = '/sys/bus/w1/devices/'
  9. device_folders = glob.glob(base_dir + '28*')
  10.  
  11. def read_temp_raw(device_file):
  12.     f = open(device_file, 'r')
  13.     lines = f.readlines()
  14.     f.close()
  15.     return lines
  16.  
  17. def read_temp(device_file):    
  18.     lines = read_temp_raw(device_file)
  19.     while lines[0].strip()[-3:] != 'YES':
  20.         time.sleep(0.2)
  21.         lines = read_temp_raw(device_file)
  22.     equals_pos = lines[1].find('t=')
  23.     if equals_pos != -1:
  24.         temp_string = lines[1][equals_pos+2:]
  25.         temp_c = float(temp_string) / 1000.0
  26.         temp_f = temp_c * 9.0 / 5.0 + 32.0
  27.         return temp_c, temp_f
  28.  
  29. while True:
  30.     for device_folder in device_folders:
  31.         device_file = os.path.join(device_folder, 'w1_slave')
  32.         print(read_temp(device_file), end='')
  33.     print()
  34.     time.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment