-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathds18b20.py
executable file
·29 lines (25 loc) · 1004 Bytes
/
ds18b20.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
#!/usr/bin/python3
import sys
# Path to the Sensor systempath
# 28-01142f7ba71a has to be changed to you sensor path!
sensor = '/sys/bus/w1/devices/28-01142f7ba71a/w1_slave'
def readTempSensor(sensorName) :
f = open(sensorName, 'r')
lines = f.readlines()
f.close()
return lines
def readTempLines(sensorName) :
lines = readTempSensor(sensorName)
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = readTempSensor(sensorName)
temperaturStr = lines[1].find('t=')
if temperaturStr != -1 :
tempData = lines[1][temperaturStr+2:]
tempCelsius = float(tempData) / 1000.0
tempKelvin = 273 + float(tempData) / 1000
tempFahrenheit = float(tempData) / 1000 * 9.0 / 5.0 + 32.0
return [tempCelsius, tempKelvin, tempFahrenheit]
print("Temperature: " + str(readTempLines(sensor)[0]) + " °C")
print("Temperature: " + str(readTempLines(sensor)[1]) + " K")
print("Temperature: " + str(readTempLines(sensor)[2]) + " °F")