-
Notifications
You must be signed in to change notification settings - Fork 0
/
temperature.py
executable file
·77 lines (64 loc) · 1.75 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
75
76
77
#!/usr/bin/env python
import MySQLdb
from MySQLdb import Error
import urllib2
import json
import RPi.GPIO as GPIO
# read value
GPIO.setmode(GPIO.BCM)
GPIO.setup(21, GPIO.IN)
poweron = GPIO.input(21)
GPIO.cleanup(21)
def turnonheater():
print "turn on heater"
GPIO.setup(21,GPIO.OUT, initial=GPIO.HIGH)
GPIO.output(21, GPIO.HIGH)
# GPIO.cleanup(21)
def turnoffheater():
print "turnoffheater"
GPIO.setup(21,GPIO.OUT, initial=GPIO.LOW)
GPIO.output(21, GPIO.LOW)
# GPIO.cleanup(21)
# check online how to find your w1/device folder
# maybe here: https://learn.adafruit.com/adafruits-raspberry-pi-lesson-11-ds18b20-temperature-sensing/ds18b20
tfile = open("/sys/bus/w1/devices/28-000005889a93/w1_slave")
text = tfile.read()
tfile.close()
temperature_data = text.split()[-1]
temperature = float(temperature_data[2:])
temperature = temperature / 1000
print temperature
db = MySQLdb.connect(MYSQLSECRETS)
curs = db.cursor()
try:
print "database call"
curs.execute ("SELECT * FROM thermostaat")
row = curs.fetchone()
onoff = row[1]
#print "onoff:"+str(onoff)
thermostemp = row[2]
print "temp thres " + str(row[2])
if onoff == 1 and row[2] > temperature:
print "temp is lower than threshold"
turnonheater()
elif onoff == 1 and row[2] < temperature:
print "temp is higher"
turnoffheater()
elif onoff == 0:
print "turn off"
turnoffheater()
except Error as e:
print (e)
try:
print "query db..."
q = "INSERT INTO temperatures(temperature) VALUES("+str(temperature)+");"
print q
curs.execute(q)
db.commit()
print "Data committed"
except:
print "Error: the database is being rolled back"
db.rollback()
finally:
curs.close()
db.close()