#!/usr/bin/env python import os import glob import time import subprocess # this needs to be automated later TODO base_dir = '/sys/bus/w1/devices/' # here is the prefix of the hardware in this case, 10 device_folder = glob.glob(base_dir + '00*')[0] device_file = device_folder + '/w1_bus_master1' def read_temp_raw(): # This function reads the raw sensor chip information catdata = subprocess.Popen( ['cat', device_file], stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = catdata.communicate() out_decode = out.decode('utf-8') lines = out_decode.split('\n') return lines def read_temp(): # This function takes the the raw sensor chip information lines = read_temp_raw() # strips it down and takes only the temperature out while lines[0].strip()[-3:] != 'YES': time.sleep(0.2) lines = read_temp_raw() equals_pos = lines[1].find('t=') if equals_pos != -1: temp_string = lines[1][equals_pos+2:] # Calculates temperature in celcius and stores it in variable temp_c = float(temp_string) / 1000.0 # Calculates temperature in fahreint and stores it in variable temp_f = temp_c * 9.0 / 5.0 + 32.0 return temp_c # here it returns only the temperature in celcius