-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReporting.py
166 lines (129 loc) · 3.83 KB
/
Reporting.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/python
#Imports:
import datetime
import time
import os
import glob
import configparser
import picamera
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
DEBUG = 1
GPIO.setmode(GPIO.BCM)
#initialize the thermometer
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
#Camera Config:
CamLocation = '/home/chempi/workspace/chempi/ChemPi/html/Pictures'
camera = picamera.PiCamera()
#Read config file
config = configparser.ConfigParser()
config.read('html/config.txt')
ConfTemperature = config['Config']['Temperature']
ConfTempGPIO = config['Config']['TempGPIO']
ConfLight = config['Config']['Light']
ConfLightGPIO = config['Config']['LightGPIO']
ConfpH = config['Config']['pH']
ConfpHGPIO = config['Config']['pHGPIO']
ConfCarbonDioxide = config['Config']['CarbonDioxide']
ConfCarbonDioxideGPIO = config['Config']['CarbonDioxideGPIO']
ConfOxygen = config['Config']['Oxygen']
ConfOxygenGPIO = config['Config']['OxygenGPIO']
ConfPiCam = config['Config']['PiCam']
ConfInterval = config['Config']['Interval']
CamRepInterval = config['Config']['CamRepInterval']
print(ConfLightGPIO)
#HTML location:
HTMLLocation = "/var/www/html/"
#Temperature
#Temperature File name:
TempeFN="csv/Temperature.csv"
def read_temp_raw():
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
return temp_c
def TemperatureMeasure():
MeasureTime = time.time()
Temp_Return = str(MeasureTime) + ", " + str(read_temp()) + ",\n"
return Temp_Return
#pH
#pH File name:
pHFN="csv/pH.csv"
def pHMeasure():
MeasureTime = time.time()
pH_Return = str(MeasureTime) + ", Test - pH is measured, \n"
return pH_Return
#Oxygen
#Oxygen File name:
OxygenFN="csv/Oxygen.csv"
def OMeasure():
MeasureTime = time.time()
O_Return = str(MeasureTime) + ", Test - O is measured, \n"
return O_Return
#Carbon Dioxide
#CO2 File name:
COTwoFN="csv/COTwo.csv"
def COTwoMeasure():
MeasureTime = time.time()
COTwo_Return = str(MeasureTime) + ", Test - CO2 is measured, \n"
return COTwo_Return
#Light measure
#Light File name:
LightFN="csv/Light.csv"
def RCtime (RCpin):
reading = 0
GPIO.setup(RCpin, GPIO.OUT)
GPIO.output(RCpin, GPIO.LOW)
time.sleep(.1)
GPIO.setup(RCpin, GPIO.IN)
# This takes about 1 millisecond per loop cycle
while (GPIO.input(RCpin) == GPIO.LOW):
reading += 1
return reading
def LightMeasure():
MeasureTime = time.time()
Light_Return = str(MeasureTime) + ", " + str(RCtime(ConfLightGPIO)) + ",\n"
return Light_Return
def TakePic():
MeasureTime = time.time()
camera.capture("%s%s.jpg" % (CamLocation, MeasureTime))
return 0
#Define camera interval start
lastInterval = 0
#Infinite loop to keep monitoring
while True:
if lastInterval>CamRepInterval:
TakePic()
lastInterval=0
else:
lastInterval = lastInterval + ReportingInterval
if ConfTemperature == "on":
with open(HTMLLocation+TempeFN, "a") as myfile:
myfile.write(TemperatureMeasure())
if ConfpH == "on":
with open(HTMLLocation+pHFN, "a") as myfile:
myfile.write(pHMeasure())
if ConfOxygen == "on":
with open(HTMLLocation+OxygenFN, "a") as myfile:
myfile.write(OMeasure())
if ConfCarbonDioxide == "on":
with open(HTMLLocation+COTwoFN, "a") as myfile:
myfile.write(COTwoMeasure())
if ConfLight == "on":
with open(HTMLLocation+LightFN, "a") as myfile:
myfile.write(LightMeasure())
time.sleep(ConfInterval)