-
Notifications
You must be signed in to change notification settings - Fork 0
/
temp.py
38 lines (31 loc) · 827 Bytes
/
temp.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
import sys
import Adafruit_DHT
from gpiozero import Button, LED, Button
import time
from time import sleep
import MySQLdb
pin = 4
yellowled = LED(26)
button = Button(5)
def ledToggle():
yellowled.toggle()
try:
db = MySQLdb.connect("localhost", "assignmentuser", "joshsmartroom", "assignment")
curs = db.cursor()
print("Successfully connected to database!")
except:
print("Error connecting to mySQL database")
def atmosphere(temperature, humidity):
try:
sql = "INSERT into atmosphere (temperature, humidity) VALUES ('%s', '%s')" % (temperature, humidity)
print(sql)
curs.execute(sql)
db.commit()
except Exception:
print(Exception)
initialtime = time.time()
while True:
button.when_pressed = ledToggle
humidity, temperature = Adafruit_DHT.read_retry(11, pin)
atmosphere(temperature, humidity)
sleep(5)