Skip to content

Instantly share code, notes, and snippets.

@donotknowspoon
Created May 14, 2016 18:16
Show Gist options
  • Save donotknowspoon/bc6a3f1e9b425d0d29a9bacd271a690f to your computer and use it in GitHub Desktop.
Save donotknowspoon/bc6a3f1e9b425d0d29a9bacd271a690f to your computer and use it in GitHub Desktop.
Temperature measurement
#!/usr/bin/env python
import os
import time
import datetime
import glob
import MySQLdb
from time import strftime
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
temp_sensor = '/sys/bus/w1/devices/28-0004441542ff/w1_slave'
# Variables for MySQL
db = MySQLdb.connect(host="localhost", user="xxxx",passwd="xxxxxx", db="xxx")
cur = db.cursor()
while True:
n = 0
t0list = [];
t1list = [];
t2list = [];
t3list = [];
#ambient temp0
tfile = open("/sys/bus/w1/devices/28-0004420588ff/w1_slave")
while (n<50):
text = tfile.read()
temperature_data = text.split()[-1]
t0 = float(temperature_data[2:])
t0 = t0 / 1000
list.append(t0)
n = n + 1
# print t1
n = 0
#enclosure temp1
tfile = open("/sys/bus/w1/devices/28-0004431613ff/w1_slave")
while (n<50):
text = tfile.read()
temperature_data = text.split()[-1]
t1 = float(temperature_data[2:])
t1 = t1 / 1000
list.append(t1)
n = n + 1
# print t1
n = 0
#enclosure temp2
tfile = open("/sys/bus/w1/devices/28-0004431af9ff/w1_slave")
while (n<50):
text = tfile.read()
temperature_data = text.split()[-1]
t2 = float(temperature_data[2:])
t2 = t2 / 1000
list.append(t2)
n = n + 1
# print t2
n = 0
#enclosure temp3
tfile = open("/sys/bus/w1/devices/28-000444175dff/w1_slave")
while (n<50):
text = tfile.read()
temperature_data = text.split()[-1]
t3 = float(temperature_data[2:])
t3 = t3 / 1000
list.append(t2)
n = n + 1
# print t3
n = 0
t0avg = sum(t0list) / float(len(t0list))
t1avg = sum(t1list) / float(len(t1list))
t2avg = sum(t0list) / float(len(t2list))
t3avg = sum(t0list) / float(len(t3list))
datetime = (time.strftime("%Y-%m-%d ") + time.strftime("%H:%M:%S"))
sql = ("""INSERT INTO temp (time,amb_temp,enc_temp1,enc_temp2,enc_temp3) VALUES (%d,%d,%d,%d,%d)""",(datetime,t0,t1,t2,t3))
try:
#print "Writing to database..."
# Execute the SQL command
cur.execute(*sql)
# Commit your changes in the database
db.commit()
#print "Write Complete"
except:
# Rollback in case there is any error
db.rollback()
#print "Failed writing to database"
time.sleep(60)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment