forked from martinohanlon/pelmetcam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtempSensorController.py
executable file
·108 lines (91 loc) · 3.04 KB
/
tempSensorController.py
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import threading
import time
DEVICESDIR = "/sys/bus/w1/devices/"
#class for holding temperature values
class Temperature():
def __init__(self, rawData):
self.rawData = rawData
@property
def C(self):
return float(self.rawData) / 1000
@property
def F(self):
return self.C * 9.0 / 5.0 + 32.0
#class for controlling the temperature sensor
class TempSensorController(threading.Thread):
def __init__(self, sensorId, timeToSleep):
threading.Thread.__init__(self)
#persist the file location
self.tempSensorFile = DEVICESDIR + sensorId + "/w1_slave"
#persist properties
self.sensorId = sensorId
self.timeToSleep = timeToSleep
#update the temperature
self.updateTemp()
#set to not running
self.running = False
def run(self):
#loop until its set to stopped
self.running = True
while(self.running):
#update temperature
self.updateTemp()
#sleep
time.sleep(self.timeToSleep)
self.running = False
def stopController(self):
self.running = False
def readFile(self):
sensorFile = open(self.tempSensorFile, "r")
lines = sensorFile.readlines()
sensorFile.close()
return lines
def updateTemp(self):
data = self.readFile()
#the output from the tempsensor looks like this
#f6 01 4b 46 7f ff 0a 10 eb : crc=eb YES
#f6 01 4b 46 7f ff 0a 10 eb t=31375
#has a YES been returned?
if data[0].strip()[-3:] == "YES":
#can I find a temperature (t=)
equals_pos = data[1].find("t=")
if equals_pos != -1:
tempData = data[1][equals_pos+2:]
#update temperature
self.temperature = Temperature(tempData)
#update success status
self.updateSuccess = True
else:
self.updateSuccess = False
else:
self.updateSuccess = False
if __name__ == "__main__":
#create temp sensor controller, put your controller Id here
# look in "/sys/bus/w1/devices/" after running
# sudo modprobe w1-gpio
# sudo modprobe w1-therm
tempcontrol = TempSensorController("28-000003aaea41", 1)
try:
print("Starting temp sensor controller")
#start up temp sensor controller
tempcontrol.start()
#loop forever, wait for Ctrl C
while(True):
print tempcontrol.temperature.C
print tempcontrol.temperature.F
time.sleep(5)
#Ctrl C
except KeyboardInterrupt:
print "Cancelled"
#Error
except:
print "Unexpected error:", sys.exc_info()[0]
raise
#if it finishes or Ctrl C, shut it down
finally:
print "Stopping temp sensor controller"
#stop the controller
tempcontrol.stopController()
#wait for the tread to finish if it hasn't already
tempcontrol.join()
print "Done"