-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsensors.py
55 lines (48 loc) · 1.53 KB
/
sensors.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import glob
import codecs
import time
import config
reader = codecs.getreader("utf-8")
class Sensors:
device_file = ''
def __init__(self):
# DS18B20 thermometer: initialize device file
base_dir = '/sys/bus/w1/devices/'
device_folders = glob.glob(base_dir + '28*')
if device_folders:
self.device_file = device_folders[0] + '/w1_slave'
else:
self.device_file = None
# temperature sensor
def _read_temp_raw(self):
if self.device_file:
try:
f = open(self.device_file, 'r')
lines = f.readlines()
f.close()
return lines
except Exception:
# in case of a temp sensor issue, return None
return None
else:
return None
def read_temp(self):
lines = self._read_temp_raw()
if lines:
retries = 0
while (not lines or lines[0].strip()[-3:] != 'YES') \
and retries < 5:
time.sleep(0.2)
lines = self._read_temp_raw()
retries += 1
if lines and len(lines) > 1:
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 * config.TEMP_CORRECTION
return None
def cleanup(self):
pass