-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo1.py
95 lines (72 loc) · 2.98 KB
/
demo1.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import time
import sys
import datetime
import Adafruit_TCS34725
import smbus
tcs = Adafruit_TCS34725.TCS34725()
import Adafruit_BMP.BMP085 as BMP085
sensor = BMP085.BMP085()
import SI1145.SI1145 as SI1145
sensor_uv = SI1145.SI1145()
import Adafruit_DHT
sensor_hum = Adafruit_DHT.DHT22
pin = 12 #This changes depending on what GPIO port you put it in
import MySQLdb
db = MySQLdb.connect (
host = '192.168.1.151',
user = 'pi', #This changes for every sensor suite
passwd = '10416232', #This also changes for every sensor suite
db = 'demo1', #Same as this
port = 3306)
curs=db.cursor()
try:
while True:
#RGB SENSOR
r, g, b, c = tcs.get_raw_data()
color_temp = Adafruit_TCS34725.calculate_color_temperature(r,g,b)
lux = Adafruit_TCS34725.calculate_lux(r,g,b)
print('Color: red={0}, green={1}, blue={2}, clear={3}'.format(r,g,b,c))
if color_temp is not None:
print('Color Temperature: {0} K'.format(color_temp))
print('Luminosity: {0} lux'.format(lux))
print('----------------------------------------------------------------')
add_rgb = ("INSERT INTO rgb VALUES(%s,%s,%s,%s,%s,%s,%s,%s)")
curs.execute(add_rgb, (time.strftime("%Y/%m/%d"), time.strftime("%H:%M:%S"), r, g, b, c, color_temp, lux))
db.commit()
#PRESSURE SENSOR
temp = sensor.read_temperature()
pressure = sensor.read_pressure()
altitude = sensor.read_altitude()
sealevelpressure = sensor.read_sealevel_pressure()
print('Temp = {0:0.2f} *C'.format(temp))
print('Pressure = {0:0.2f} Pa'.format(pressure))
print('Altitude = {0:0.2f} m'.format(altitude))
print('Sealevel Pressure = {0:0.2f} Pa'.format(sealevelpressure))
print('----------------------------------------------------------------')
add_pressure = ("INSERT INTO pressurerecord VALUES(%s,%s,%s,%s,%s,%s)")
curs.execute(add_pressure, (time.strftime("%Y/%m/%d"), time.strftime("%H:%M:%S"), temp, pressure, altitude, sealevelpressure))
db.commit()
#UV SENSOR
vis = sensor_uv.readVisible()
IR = sensor_uv.readIR()
UV = sensor_uv.readUV()
uvIndex = UV / 100.0
print 'Vis: ' + str(vis)
print 'IR: ' + str(IR)
print 'UV Index: ' + str(uvIndex)
print('----------------------------------------------------------------')
addLight = ("INSERT INTO uvlight VALUES(%s,%s,%s,%s,%s)")
curs.execute(addLight, (time.strftime("%Y/%m/%d"), time.strftime("%H:%M:%S"), vis, IR, uvIndex))
db.commit()
#HUMIDITY SENSOR
humidity, temp3 = Adafruit_DHT.read_retry(sensor_hum, pin)
print("Humidity = " + str(humidity))
print('################################################################')
add_hum = ("INSERT INTO humidity VALUES(%s,%s,%s)")
curs.execute(add_hum, (time.strftime("%Y/%m/%d"), time.strftime("%H:%M:%S"), humidity))
db.commit()
time.sleep(10)
except KeyboardInterrupt:
print "\nA keyboard interrupt has been noticed"
except:
print "An error or exception has occurred"