-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTemperatureReader.py
More file actions
38 lines (31 loc) · 1.16 KB
/
TemperatureReader.py
File metadata and controls
38 lines (31 loc) · 1.16 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
__author__ = 'matt'
import threading
import time
# A static class that provides the current temperature to a requestor. Updates the
# temperature every 10 seconds
class TemperatureReader():
def __init__(self):
self.currentTemperature = self.get_current_temperature()
self.lastCheck = time.time()
self.currentLock = threading.Lock()
def get_current_temperature(self):
# check thermometer
try:
f = open("/sys/bus/w1/devices/28-0000041117ab/w1_slave")
f.readline()
line = f.readline()
split = line.split("=")
temperature = float(split[1]) / 1000
fahrenheit = round((temperature * 1.8) + 32, 1)
f.close()
return fahrenheit
except:
print("reading thermometer failed, returning -1000")
return -1000
def CurrentTemperature(self):
if 10 <= time.time() - self.lastCheck:
self.currentLock.acquire()
self.currentTemperature = self.get_current_temperature()
self.lastCheck = time.time()
self.currentLock.release()
return self.currentTemperature