Skip to content

Commit 4312327

Browse files
authored
Update smarthome.py
1 parent 21c3ff1 commit 4312327

File tree

1 file changed

+39
-16
lines changed

1 file changed

+39
-16
lines changed

python/smarthome.py

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
__author__ = 'Caibiy'
55

6-
import Adafruit_DHT,os,time,datetime,sqlite3
6+
import Adafruit_DHT,os,time,datetime,sqlite3,uuid
77
conn,cursor=(None,None)
88
#gpio
99
@unique
@@ -17,11 +17,31 @@ def initDb():
1717
global conn,cursor
1818
conn = sqlite3.connect("./db/smarthome.db")
1919
cursor = conn.cursor()
20-
#图片表如果不存在则创建
20+
#图片表
2121
cursor.execute('''CREATE TABLE IF NOT EXISTS pic (
22-
picid INT UNSIGNED AUTO_INCREMENT PRIMARY KEY ,
22+
picid INT UNSIGNED PRIMARY KEY ,
2323
time VARCHAR (100) NOT NULL
2424
)''')
25+
#温度表
26+
cursor.execute('''CREATE TABLE IF NOT EXISTS dth (
27+
dthid INT UNSIGNED PRIMARY KEY ,
28+
temp VARCHAR (100) NOT NULL,
29+
humidity VARCHAR(100) NOT NULL
30+
)''')
31+
#mq2表
32+
cursor.execute('''CREATE TABLE IF NOT EXISTS mq2 (
33+
mq2id INT UNSIGNED PRIMARY KEY ,
34+
lpg VARCHAR (100) NOT NULL,
35+
co VARCHAR(100) NOT NULL,
36+
smoke VARCHAR(100) NOT NULL
37+
)''')
38+
#操作记录表
39+
cursor.execute('''CREATE TABLE IF NOT EXISTS operalog (
40+
id INT UNSIGNED PRIMARY KEY ,
41+
time VARCHAR(100) NOT NULL,
42+
operaid VARCHAR(100) NOT NULL,
43+
type VARCHAR(100) NOT NULL
44+
)''')
2545
conn.commit()
2646
#执行sql
2747
def executeDb(sql):
@@ -34,40 +54,43 @@ class smartHome(object):
3454
@property
3555
def sensor(self):
3656
return self.sensor
37-
def __init__(self,pdht11,pmq2,pbuzzer):
38-
self.__pdht11 = pdht11
39-
self.__pmq2 = pmq2
40-
self.__pbuzzer = pbuzzer
57+
def __init__(self):
4158
#初始化mode
42-
os.system('gpio -g mode %s out' % pdht11)
43-
os.system('gpio -g mode %s out' % pmq2)
44-
os.system('gpio -g mode %s out' % pbuzzer)
59+
os.system('gpio -g mode %s out' % gpio.pdht11.value)
60+
os.system('gpio -g mode %s out' % gpio.pmq2.value)
61+
os.system('gpio -g mode %s out' % gpio.pbuzzer.value)
4562
#蜂鸣器
4663
def buzzer(self):
47-
os.system('gpio -g write %s 1' % self.__pbuzzer)
64+
os.system('gpio -g write %s 1' % gpio.pbuzzer.value)
4865
time.sleep(0.5)
49-
os.system('gpio -g write %s 0' % self.__pbuzzer)
66+
os.system('gpio -g write %s 0' % gpio.pbuzzer.value)
67+
#读取dth11温度
5068
def readDth(self):
51-
humidity,temperature = Adafruit_DHT.read_retry(self.sensor,self.__pdht11)
69+
humidity,temperature = Adafruit_DHT.read_retry(self.sensor,gpio.pdht11.value)
5270
if humidity is not None and temperature is not None:
5371
print('temp:%s,humidity%d' % (humidity,temperature))
72+
saveDth(temperature,humidity)
5473
return (humidity,temperature)
74+
def saveDth(self,temp,humidity):
75+
executeDb('insert into dth (time) VALUES (\'%s\',\'%s\') ' % (temp,humidity))
76+
5577
#USB摄像头
5678
class usbCamera(object):
5779
def __init__(self):
5880
pass
5981
def takePhoto(self):
6082
nowTime = datetime.datetime.now().strftime("%Y-%m-%d-%H:%M:%S")
6183
os.system('fswebcam -r 1280x720 --no-banner ../img/%s.jpg' % nowTime)
62-
executeDb('insert into pic (time) VALUES (\'%s\') ' % nowTime)
84+
saveTime(nowTime)
85+
def saveTime(self,time):
86+
executeDb('insert into pic (time) VALUES (\'%s\') ' % time)
6387
def init():
6488
os.system('bash ./check.sh')
6589
s = smartHome(23,11,21)
6690
initDb()
6791
#uc = usbCamera()
6892
#uc.takePhoto()
69-
cursor.execute('select * from pic')
70-
print(cursor.fetchall())
93+
7194

7295
if __name__ =='__main__':
7396
init()

0 commit comments

Comments
 (0)