-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemp.py
executable file
·80 lines (65 loc) · 1.89 KB
/
temp.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
#!/usr/bin/env python
# coding=utf-8
"""
Author: Christian Ashby
September 2016
@babelmonk
Thanks to: http://raspbrew.tumblr.com/post/44456213110/reading-temperatures-on-a-raspberry-pi-using
"""
from threading import Thread
import time
import sys
class Temp(Thread):
"""
A class for getting the current temp of a DS18B20
"""
def __init__(self, sensorid = None, sensordir='/sys/bus/w1/devices', group=None, target=None, name=None, verbose=None):
super(Temp, self).__init__(group=group, target=target, name=name, verbose=verbose)
self.sensorid = sensorid
self.sensordir = sensordir
self.currentTemp = -999
self.correctionFactor = 0
self.enabled = True
self.error = False
self.ready = False
self.daemon = True
def run(self):
while True:
if self.isEnabled():
filename = self.sensordir + "/" + self.sensorid + "/w1_slave"
try:
f = open(filename, 'r')
except IOError as e:
sys.stderr.write("Error: File " + filename + " does not exist.\n")
self.error = True
return
lines=f.readlines()
crcLine=lines[0]
tempLine=lines[1]
result_list = tempLine.split("=")
temp = float(result_list[-1])/1000 # temp in Celcius
temp = temp + self.correctionFactor # correction factor
#if you want to convert to fahrenheit, uncomment this line
#temp = (9.0/5.0)*temp + 32
if crcLine.find("NO") > -1:
temp = -999
else:
self.ready = True
self.currentTemp = temp
#print "Current: " + str(self.currentTemp) + " " + str(self.sensorid)
time.sleep(1)
#returns the current temp for the probe
def getCurrentTemp(self):
return self.currentTemp
#setter to enable this probe
def setEnabled(self, enabled):
self.enabled = enabled
#getter
def isEnabled(self):
return self.enabled
#getter for ready check
def isReady(self):
return self.ready
#getter for error check
def isError(self):
return self.error