Advertisement
Guest User

Untitled

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