-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogData.py
75 lines (64 loc) · 1.98 KB
/
logData.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
75
import os, glob, time, sys, datetime, pickle
import sqlite3 as lite
import RPi.GPIO as GPIO
## Get session and db name ##
sessionName = pickle.load(open('/home/pi/FermentorApp/sessionName.pkl', 'rb'))
dbName = 'sensorsData.db'
## Setting up probes - three of them on the RPi ##
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
folderList = glob.glob(base_dir + '28*')
deviceFileList = []
for i in folderList:
p = i + '/w1_slave'
deviceFileList.append(p)
## Set up GPIO for fan control ##
pin = 27
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin, GPIO.OUT)
GPIO.setwarnings(False)
## Reads the raw device file info ##
def read_temp_raw(file):
f = open(file, 'r')
lines = f.readlines()
f.close()
return lines
## Captures temp in deg F and returns list of number values ##
def getTemps():
tempData = []
for i in range(len(deviceFileList)):
lines = read_temp_raw(deviceFileList[i])
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
temp_f = round(temp_c * 9.0 / 5.0 + 32.0, 1)
tempData.append(temp_f)
return tempData
def logData():
temps = getTemps()
tempIn, tempOut, tempBeer = temps
tempSet = pickle.load(open("/home/pi/FermentorApp/tempSet.pkl", "rb"))
now = datetime.datetime.now()
date = now.strftime("%c")
con = lite.connect(dbName)
cur = con.cursor()
cur.execute("INSERT INTO " + sessionName + " VALUES((?), (?), (?), (?), (?))", (date, tempIn, tempOut, tempBeer, tempSet))
con.commit()
con.close()
## Turn fan on our off based on internal temp of fermentor ##
if float(tempIn) > float(tempSet):
GPIO.output(pin, True)
else:
GPIO.output(pin, False)
def displayData():
c = lite.connect(dbName)
cur = c.cursor()
for row in cur.execute("SELECT * FROM " + sessionName):
print(row)
c.close()
logData()