-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogTemp.py
More file actions
115 lines (98 loc) · 3.72 KB
/
logTemp.py
File metadata and controls
115 lines (98 loc) · 3.72 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env python
import time
import MySQLdb
time.sleep(10)
import urllib2
import datetime
import Adafruit_DHT
import RPi.GPIO as GPIO
import ConfigParser
config=ConfigParser.ConfigParser()
config.read('/home/pertneer/Desktop/config.ini')
dbHost = config.get('database','host')
dbUser = config.get('database','user')
dbPasswd = config.get('database','passwd')
dbDb = config.get('database','db')
db = MySQLdb.connect(host=dbHost,
user=dbUser,
passwd=dbPasswd,
db=dbDb)
cur = db.cursor()
outsidePin = int(config.get('DHT','outsidePin'))
insidePin = int(config.get('DHT','insidePin'))
doorOpenPin=int(config.get('Pins','doorOpenPin'))
doorClosePin=int(config.get('Pins','doorClosePin'))
sensor = Adafruit_DHT.DHT22
sleepTime = 15*60
dateString = '%Y/%m/%d %H:%M:%S'
sunRise = ""
sunSet = ""
def setup():
global sunRise
global sunSet
if (GPIO.getmode() == 10) or (GPIO.getmode() == None):
GPIO.setmode(GPIO.BCM)
GPIO.setup(doorOpenPin, GPIO.IN, GPIO.PUD_UP)
GPIO.setup(doorClosePin, GPIO.IN, GPIO.PUD_UP)
humidity, temperature = Adafruit_DHT.read_retry(sensor, insidePin)
humidityOut, temperatureOut = Adafruit_DHT.read_retry(sensor, outsidePin)
if humidity is None and temperature is None:
print 'Failed to get a reading. Please try again!'
with open('/home/pertneer/Desktop/sunrise.conf','r') as s:
sunTimes = s.readlines()
s.close()
sunRise = sunTimes[0].strip('\n').replace(':',".")
sunSet = sunTimes[1].strip('\n').replace(':',".")
def writelog(message,location):
with open("/home/pertneer/Desktop/%s" % location,'a') as f:
f.write("{0} {1}\n".format(time.asctime( time.localtime(time.time())), message))
def loop():
global sleepTime
global sunRise
global sunSet
global sensor
global pin
log=True
while log:
db = MySQLdb.connect(host=dbHost,
user=dbUser,
passwd=dbPasswd,
db=dbDb)
cur = db.cursor()
if(GPIO.input(doorOpenPin) == 0):
doorLoc = 1
doorPosition='Open'
elif(GPIO.input(doorClosePin) == 0):
doorLoc = 0
doorPosition='Close'
else:
doorLoc = .5
humidity, temperature = Adafruit_DHT.read_retry(sensor, insidePin)
humidityOut, temperatureOut = Adafruit_DHT.read_retry(sensor, outsidePin)
temperature = temperature * 9/5 + 32
temperature = "{:2.2f}".format(temperature)
humidity = "{:2.2f}".format(humidity)
temperatureOut = temperatureOut * 9/5 + 32
temperatureOut = "{:2.2f}".format(temperatureOut)
humidityOut = "{:2.2f}".format(humidityOut)
currentTime = time.strftime(dateString)
sqlQuery="INSERT INTO temperature(tempIn,tempOut,humidityIn,humidityOut,doorPosition,openTime,closeTime) VALUES (" + str(temperature) + ", " + str(temperatureOut) + ", " + str(humidity) + ", " + str(humidityOut) + ", '" + doorPosition + "', '" + sunRise + "', '" + sunSet + "')"
if(temperatureOut < 120 and humidityOut < 100):
cur.execute(sqlQuery)
db.commit()
db.close()
else:
logData = str(temperature) + "," + str(humidity) + "," + str(temperatureOut) + "," + str(humidityOut)
writelog(logData,'Logs/tempData.csv')
#time.sleep(sleepTime)
log = False
def destroy():
writelog('Destroy', 'log.csv')
GPIO.cleanup() # Release resource
db.close()
if __name__ == '__main__': # Program start from here
setup()
try:
loop()
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
destroy()