-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor_test.py
66 lines (55 loc) · 2.01 KB
/
sensor_test.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
import random, time, grovepi
from statistics import mean
from terminaltables import AsciiTable
timeout = time.time() + 60*2 # Set to run for 2 minutes
sensorLoudness = 1
sensorMQ5 = 2
arrayTimestamp = []
arrayTemperature = []
arrayHumidity = []
arrayLoudness = []
arrayMQ5 = []
def readingsSensors():
#th = grove_i2c_temp_hum_mini.th02()
theTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
#theTemperature = th.getTemperature()
#theHumidity = th.getHumidity()
theLoudness = grovepi.analogRead(sensorLoudness)
theMQ5 = grovepi.analogRead(sensorMQ5)
arrayTimestamp.append(theTime)
#arrayTemperature.append(theTemperature)
#arrayHumidity.append(theHumidity)
arrayLoudness.append(theLoudness)
arrayMQ5.append(theMQ5)
timeRemaining = str(int(timeout - time.time() + .5))
timeMessage = "Reading taken. " + timeRemaining + " seconds remaining."
print timeMessage
time.sleep(5)
while True:
if time.time() > timeout:
break
readingsSensors()
#with open("output.csv", "w") as f:
# writer = csv.writer(f)
# writer.writerows(dataArray)
print "Readings complete."
matrixData = []
for i in range(len(arrayTimestamp)):
matrixData.append([arrayTimestamp[i], arrayLoudness[i], arrayMQ5[i]]) #, arrayTemperature[i], arrayHumidity[i], )
print " "
print "======="
print "REPORT:"
print "======="
print " "
#print "RAW DATA TABLE:"
#print " "
#matrixRaw = matrixData
#matrixRaw.insert(0,["Time", "Temp", "Humidity", "Loudness", "MQ5"])
#table = AsciiTable(matrixRaw)
#print table.table
#print " "
#print "STATISTICS TABLE:"
#print " "
tableStats = [["READING", "MIN", "MEAN", "MAX"], ["Loudness", min(arrayLoudness), round(mean(arrayLoudness),3), max(arrayLoudness)], ["MQ5", min(arrayMQ5), round(mean(arrayMQ5),3), max(arrayMQ5)]]
table = AsciiTable(tableStats) #["Temperature", min(arrayTemperature), round(mean(arrayTemperature),3), max(arrayTemperature)], ["Humidity", min(arrayHumidity), round(mean(arrayHumidity),3), max(arrayHumidity)],
print table.table