My Technology Trail avatar

raspi02 temperature logging Python code

mytechtrail

Published: 04 Jul 2019 › Updated: 04 Jul 2019raspi02 temperature logging Python code

raspi02 temperature logging Python code

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()

Yes, this code still 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.

Leave raspi02 temperature logging Python code to:

Written by

Projects I am working on, Raspberry Pi, Python and Ham Radio.

Read more #ulog posts


Best Posts From My Technology Trail

We have not curated any of mytechtrail's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From My Technology Trail