-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemperature.py
More file actions
39 lines (33 loc) · 1.15 KB
/
Copy pathtemperature.py
File metadata and controls
39 lines (33 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import os
import glob
import time
class TemperatureProbe(object):
# set up the termperature probe
def __init__(self):
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
self.tp = device_folder + '/w1_slave'
# take a raw reading from the probe
def read_temp_raw(self):
f = open(self.tp, 'r')
lines = f.readlines()
f.close()
return lines
# if it is a good reading convert to celcius. return error if not valid.
def read_temp(self):
lines = self.read_temp_raw()
try:
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
temp_c = round(temp_c, 1)
string_temp = str(temp_c)
return string_temp + "°"
except:
return "error"