-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemperature.py
executable file
·74 lines (58 loc) · 1.83 KB
/
temperature.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env bash
import os
import commands
import glob
import time
import sqlite3 as lite
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
thermometers = ['28-0000035ac85e','28-0000035acb40']
temperatures = []
base_dir = '/sys/bus/w1/devices/'
device_file = '/w1_slave'
path = commands.getoutput('pwd')
DB_NAME = path + '/temperature/t.db'
FILE_NAME = path + '/temperature/t.log'
def saveToDB(data):
con = lite.connect(DB_NAME)
with con:
cur = con.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS Temperatures(Date DATE, Inside TEXT, Outside TEXT)")
cur.execute("INSERT INTO Temperatures(Date, Inside, Outside) VALUES(datetime('now','localtime'), ?, ?)", data)
con.commit()
def saveToFile(data):
time_stamp = str(time.strftime('%Y-%m-%d %H:%M:%S ,', time.localtime()))
with open(FILE_NAME,'a') as f:
f.writelines(time_stamp + str('%s, %s' %(data[0], data[1])) + '\n')
f.close()
def readFromDB():
temp = []
con = lite.connect(DB_NAME)
with con:
cur = con.cursor()
week = cur.execute("SELECT * FROM Temperatures ORDER BY DATE LIMIT 25200")
day = cur.execute("SELECT * FROM Temperatures ORDER BY DATE LIMIT 3600")
print day
with open('t.log','w') as f:
for item in day:
f.writelines(item)
f.writelines('\n')
print item
def temperature():
#temperatures.append(str(time.strftime('%Y-%m-%d %H:%M:%S ,', time.localtime())))
for thermometer in thermometers:
with open(base_dir + thermometer + device_file,'r') as f:
lines = f.readlines()
temp = [line.split() for line in lines ]
#print 'Temp:', temp
if 'YES' in temp[0]:
temperature = float(temp[1][-1][2:]) / 1000
temperatures.append(temperature)
f.close()
#print 'File is closed = ', f.closed
return temperatures
if __name__ == '__main__':
temp = temperature()
saveToDB(temp)
saveToFile(temp)
#readFromDB()