-
Notifications
You must be signed in to change notification settings - Fork 0
/
temp.py
executable file
·36 lines (31 loc) · 920 Bytes
/
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
#!/usr/bin/env python
import os
def sensor():
for i in os.listdir('/sys/bus/w1/devices'):
if i != 'w1_bus_master1':
ds18b20 = i
return ds18b20
def read(ds18b20):
location = '/sys/bus/w1/devices/' + ds18b20 + '/w1_slave'
tfile = open(location)
text = tfile.read()
tfile.close()
secondline = text.split("\n")[1]
temperaturedata = secondline.split(" ")[9]
temperature = float(temperaturedata[2:])
celsius = temperature / 1000
farenheit = (celsius * 1.8) + 32
return celsius, farenheit
def loop(ds18b20):
while True:
if read(ds18b20) != None:
# print "Current temperature : %0.3f C" % read(ds18b20)[0]
print "Current temperature : %0.3f F" % read(ds18b20)[1]
def kill():
quit()
if __name__ == '__main__':
try:
serialNum = sensor()
loop(serialNum)
except KeyboardInterrupt:
kill()