Skip to content

Commit 546b967

Browse files
author
sbrinkerhoff
committed
Cleanup from Pylink
1 parent 92c2b76 commit 546b967

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

temp.py

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/usr/bin/python
22

3+
""" Simple Python datalogger for the Raspberry PI """
4+
35
import datetime
46
import sys
57

@@ -9,40 +11,52 @@
911

1012

1113
def get_dbhandle():
14+
""" Return a database handle to the SQLite file """
1215
con = None
1316
con = sqlite3.connect('/var/lib/rpi-datalogger/database.db')
1417
return con
1518

1619
def get_temp(file_link):
17-
file = open(file_link)
18-
text = file.read()
19-
file.close()
20-
firstline = text.split("\n")[0]
21-
if not 'YES' in firstline:
22-
sys.exit(0)
23-
secondline = text.split("\n")[1]
24-
temp = secondline.split(" ")[9]
25-
temp = round((int(temp[2:])/1000.0)*1.8+32,2)
26-
return temp
20+
""" Given a file_link to a dallas one-wire device return the
21+
temperature in fahrenheit """
22+
23+
with open(file_link) as sensor:
24+
text = sensor.read()
25+
26+
firstline = text.split("\n")[0]
27+
secondline = text.split("\n")[1]
28+
if not 'YES' in firstline:
29+
sys.exit(0)
30+
temp = secondline.split(" ")[9]
31+
32+
# Sensor reads in celsius, C * 1.8 + 32 = F
33+
temp = round((int(temp[2:])/1000.0)*1.8+32, 2)
34+
return temp
2735

2836
def main():
37+
""" Where the magic happens. """
38+
2939
inside = get_temp("/sys/bus/w1/devices/28-000003d37411/w1_slave")
3040
outside = get_temp("/sys/bus/w1/devices/28-000003ea8a14/w1_slave")
3141

42+
curdate = str(datetime.datetime.now())[0:19]
43+
3244
con = get_dbhandle()
3345
cur = con.cursor()
34-
sql = 'insert into trends (timestamp, insidetemp_f, outsidetemp_f) values ("%s", %s, %s)' % ( str(datetime.datetime.now())[0:19], inside, outside)
46+
sql = 'insert into trends (timestamp, insidetemp_f, outsidetemp_f) \
47+
values ("%s", %s, %s)' % (curdate, inside, outside)
3548

36-
37-
print sql
3849
cur.execute(sql)
3950
con.commit()
40-
print str(datetime.datetime.now())[0:19],",",inside,",",outside
51+
52+
curdate = str(datetime.datetime.now())[0:19]
53+
54+
print curdate, ",", inside, ",", outside
55+
4156
lcd = lcddriver.lcd(init=False)
42-
lcd.lcd_display_string("Inside : " +str(inside)+' F ',2)
57+
lcd.lcd_display_string("Inside : " +str(inside)+' F ', 2)
4358
lcd.lcd_display_string("Outside: " + str(outside) + " F ", 3)
44-
lcd.lcd_display_string(str(datetime.datetime.now())[0:19],4)
45-
59+
lcd.lcd_display_string(curdate, 4)
4660

4761
if __name__ == "__main__":
4862
main()

0 commit comments

Comments
 (0)