raspi02 temperature test Python code

Words
277
Reading
2 min
8y
Listen to this post
~2 min

raspi02 temperature test Python code

Here is the Python code that I am testing on raspi02 for monitoring temperatures

#!/usr/bin/python

MIN_TEMP = -100
temp1 = "/sys/bus/w1/devices/28-00000379fea9/w1_slave"
temp2 = "/sys/bus/w1/devices/28-00000620f19e/w1_slave"
temp3 = "/sys/bus/w1/devices/28-000004475127/w1_slave"

def read_temp1():
# open/read/close the file with the temperature
temp1file = open(temp1)
text = temp1file.read()
temp1file.close()

# split the two lines
lines = text.split("\n")

# make sure the crc is valid
if lines[0].find("YES") > 0:
# get the 9th (10th) chunk of text and lose the t= bit
temp = float((lines[1].split(" ")[9])[2:])
# add a decimal point
temp /= 1000
temp = round(temp,2)
return temp
return MIN_TEMP-1

def read_temp2():
# open/read/close the file with the temperature
temp2file = open(temp2)
text = temp2file.read()
temp2file.close()

# split the two lines
lines = text.split("\n")

# make sure the crc is valid
if lines[0].find("YES") > 0:
# get the 9th (10th) chunk of text and lose the t= bit
temp = float((lines[1].split(" ")[9])[2:])
# add a decimal point
temp /= 1000
temp = round(temp,2)
return temp
return MIN_TEMP-1

def read_temp3():
# open/read/close the file with the temperature
temp3file = open(temp3)
text = temp3file.read()
temp3file.close()

# split the two lines
lines = text.split("\n")

# make sure the crc is valid
if lines[0].find("YES") > 0:
# get the 9th (10th) chunk of text and lose the t= bit
temp = float((lines[1].split(" ")[9])[2:])
# add a decimal point
temp /= 1000
temp = round(temp,2)
return temp
return MIN_TEMP-1

print("Battery", read_temp1())

print("outside", read_temp2())

print("Outside Deck", read_temp3())

Yes, this code needs a lot of work. That is part of my learning process, throw some test code together and then rework it until it looks and runs more efficiently.

Until next time.