raspi02 temperature logging Python code
Here is the Python code that I am testing on raspi02 for monitoring temperatures, which I have updated to log the measurements to a MYSQL database
import MySQLdb
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"
db = MySQLdb.connect("192.168.1.200", "pidata", "XXXXXXXX", "home_data")
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
sqlcmd = "insert into temperatures(location, temp) values ('ManCave', " + str(read_temp1()) + ")"
cursor = db.cursor()
cursor.execute(sqlcmd)
db.commit()
def read_temp2():
#open/read/close the file with the temperature
temp1file = open(temp2)
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
sqlcmd = "insert into temperatures(location, temp) values ('RearDeck', " + str(read_temp2()) + ")"
cursor = db.cursor()
cursor.execute(sqlcmd)
db.commit()
def read_temp3():
#open/read/close the file with the temperature
temp1file = open(temp3)
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
sqlcmd = "insert into temperatures(location, temp) values ('MCfloor', " + str(read_temp3()) + ")"
#print (sqlcmd)
cursor = db.cursor()
cursor.execute(sqlcmd)
db.commit()
db.close()