Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
33 lines (28 sloc) 838 Bytes
"""
Module for reading temperature from the raspbery Pi 1-wire interface.
"""
import os
import glob
import time
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
BASE_DIR = '/sys/bus/w1/devices/'
DEVICE_FOLDER = glob.glob(BASE_DIR + '28*')[0]
DEVICE_FILE = DEVICE_FOLDER + '/w1_slave'
def read_temp_raw():
"""Read the raw data."""
file_name = open(DEVICE_FILE, 'r')
lines = file_name.readlines()
file_name.close()
return lines
def read_temp():
"""Convert the raw data into a temperature output."""
lines = read_temp_raw()
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:]
temp_c = float(temp_string) / 1000.0
return temp_c