-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path1wire.py
More file actions
32 lines (29 loc) · 878 Bytes
/
Copy path1wire.py
File metadata and controls
32 lines (29 loc) · 878 Bytes
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
#!/usr/bin/python2.7
import re
import time
# define path to 1-wire sensor
pathes = (
"/sys/bus/w1/devices/10-000801b5a7a6/w1_slave",
"/sys/bus/w1/devices/10-000801b5959d/w1_slave"
)
def read_sensor(path):
value=None
try:
with open(path, 'r') as fd:
line = fd.readline()
if re.match(r"([0-9a-f]{2} ){9}: crc=[0-9a-f]{2} YES", line):
line = fd.readline()
m = re.match(r"([0-9a-f]{2} ){9}t=([+-]?[0-9]+)", line)
if m:
value = str(float(m.group(2)) / 1000.0)
except (IOError), e:
print time.strftime("%x %X"), "Error reading", path, ": ", e
return value
# read sensor data
for path in pathes:
data = read_sensor(path)
if data:
print '{} -> {}'.format(path, data)
else:
print '{} -> None'.format(path)
time.sleep(1)