· 7 years ago · Dec 09, 2018, 08:24 AM
1import sqlite3
2import smtplib
3import Adafruit_BBIO.GPIO as GPIO
4import Adafruit_DHT
5import time
6
7PIN = "P8_10"
8
9sensor = Adafruit_DHT.DHT11
10
11
12
13def drop_table(cursor):
14 sql = 'drop table pomiary'
15 cursor.execute(sql)
16 print('usunieto tabele pomiary')
17
18
19def create_table(cursor):
20 sql = 'create table if not exists pomiary(temperatura number, wilgotnosc number)'
21 cursor.execute(sql)
22 print('utworzono tabele pomiary')
23
24
25def insert_into_table(cursor, temperatura, wilgotnosc):
26 cursor.execute('''insert into pomiary(temperatura, wilgotnosc) values(?, ?)''', (temperatura, wilgotnosc))
27
28def select_from_table(cursor):
29 sql = 'select * from pomiary order by _rowid_ desc'
30 cursor.execute(sql)
31 temp = cursor.fetchone()
32 return temp
33
34def main():
35 conn = sqlite3.connect("odl2.db")
36 cursor = conn.cursor()
37 create_table(cursor)
38 temp=[]
39 humi=[]
40 sredniatemp = 0
41 sredniahumi = 0
42
43 while True:
44 for x in range(0, 17):
45 hum, temperature = Adafruit_DHT.read_retry(sensor, PIN)
46 if temperature is not None and hum is not None:
47 temp.append(temperature)
48 humi.append(hum)
49
50 sumatemp = sum(temp)
51 sumahumi = sum(humi)
52 sredniatemp = sumatemp/18
53 sredniahumi = sumahumi/18
54 insert_into_table(cursor, sredniatemp, sredniahumi)
55 temp = select_from_table(cursor)
56 print(str(temp[0]) + ' ' + str(temp[1]))
57 #print(sredniahumi,sredniatemp)
58
59
60 #drop_table(cursor)
61 #conn.close()
62
63
64if __name__ == '__main__':
65 main()