This repository has been archived by the owner on Aug 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect.py
346 lines (308 loc) · 13.4 KB
/
detect.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import RPi.GPIO as GPIO
import time
import math
from tqdm import tqdm
from firebase import firebase
# change these as desired - they're the pins connected from the
# SPI port on the ADC to the Cobbler
SPICLK = 11
SPIMISO = 9
SPIMOSI = 10
SPICS = 8
mq2_dpin = 26
mq2_apin = 1
mq135_dpin = 20
mq135_apin = 0
mq4_dpin = 21
mq4_apin = 2
firebase = firebase.FirebaseApplication('https://air-quality-c164a-default-rtdb.firebaseio.com')
#port init
def init():
GPIO.setwarnings(False)
GPIO.cleanup() #clean up at the end of your script
GPIO.setmode(GPIO.BCM) #to specify whilch pin numbering system
# set up the SPI interface pins
GPIO.setup(SPIMOSI, GPIO.OUT)
GPIO.setup(SPIMISO, GPIO.IN)
GPIO.setup(SPICLK, GPIO.OUT)
GPIO.setup(SPICS, GPIO.OUT)
GPIO.setup(mq2_dpin,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(mq135_dpin,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(mq4_dpin,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
#read SPI data from MCP3008(or MCP3204) chip,8 possible adc's (0 thru 7)
def readadc(adcnum, clockpin, mosipin, misopin, cspin):
if ((adcnum > 7) or (adcnum < 0)):
return -1
GPIO.output(cspin, True)
GPIO.output(clockpin, False) # start clock low
GPIO.output(cspin, False) # bring CS low
commandout = adcnum
commandout |= 0x18 # start bit + single-ended bit
commandout <<= 3 # we only need to send 5 bits here
for i in range(5):
if (commandout & 0x80):
GPIO.output(mosipin, True)
else:
GPIO.output(mosipin, False)
commandout <<= 1
GPIO.output(clockpin, True)
GPIO.output(clockpin, False)
adcout = 0
# read in one empty bit, one null bit and 10 ADC bits
for i in range(12):
GPIO.output(clockpin, True)
GPIO.output(clockpin, False)
adcout <<= 1
if (GPIO.input(misopin)):
adcout |= 0x1
GPIO.output(cspin, True)
adcout >>= 1 # first bit is 'null' so drop it
return adcout
def caliberate():
# A dict with sensors and their load resistances
sensor_apins = {mq135_apin : 20, mq2_apin: 20, mq4_apin: 20}
# sensor_apins = (mq135_apin, mq2_apin)
R0_list = []
analog_read = readadc(1,SPICLK,SPIMOSI,SPIMISO,SPICS)
print("analog_read: {}".format(analog_read))
for j in tqdm(sensor_apins):
m = 0
#print(j)
sensor_volt = None
RS_air = None
R0 = None
sensor_value = 0.0
analog_read = readadc(j,SPICLK,SPIMOSI,SPIMISO,SPICS)
#print("analog_read: {}".format(analog_read))
#print("check")
for x in range(5000):
if m != 500:
if analog_read != 0 and analog_read <= 1023:
#print("check1")
sensor_value = sensor_value + analog_read
m = m + 1
elif analog_read > 1023:
analog_read = 1023
sensor_value = sensor_value + analog_read
m = m + 1
else:
pass
elif m == 500:
sensor_value = sensor_value/500.0
#print("sensor {}:{}".format(j,sensor_value))
sensor_volt = sensor_value*(5.0/1023.0)
#print("sensor_volt:{}(in caliberating)".format(sensor_volt))
#print("sensor_apins[j] {}:".format(sensor_apins[j]))
RS_air = ((5.0*sensor_apins[j])/sensor_volt) - 10.0
#print("RS_air:{}".format(RS_air))
if j == mq135_apin:
ratio_air = 3.7
elif j == mq2_apin:
ratio_air = 9.8
elif j == mq4_apin:
ratio_air = 4.4
R0 = RS_air/ratio_air
R0_list.append(R0)
time.sleep(0.5)
break
print(R0_list)
print("Caliberating Done")
return R0_list
def mq135_Calc(R0):
#print("inside mq135 calc")
loadRes = 20
#Calculating for smoke
m = -0.318
b = 1.13
sensorVal = readadc(mq135_apin, SPICLK, SPIMOSI, SPIMISO, SPICS)
#print("sensorVal: {}".format(sensorVal))
if sensorVal !=0 and sensorVal <= 1023:
sensor_volt = sensorVal*(5.0/1023.0)
#print("sensor_volt(<=1023): {}".format(sensor_volt))
RS_gas = ((5.0*loadRes)/sensor_volt) - 10
#print("RS_gas: {}".format(RS_gas))
ratio = RS_gas/R0
#print("ratio: {}".format(ratio))
if ratio >0:
ppm_log = (math.log10(ratio) - b)/m
ppm = pow(10, ppm_log)
percentage = ppm/10000
return ppm
else:
pass
elif sensorVal > 1023:
sensorVal = 1023
sensor_volt = sensorVal*(5.0/1023.0)
#print("sensor_volt(>1023): {}".format(sensor_volt))
RS_gas = ((5.0*loadRes)/sensor_volt) - 10
ratio = RS_gas/R0
#print("ratio: {}".format(ratio))
if ratio >0:
ppm_log = (math.log10(ratio) - b)/m
ppm = pow(10, ppm_log)
percentage = ppm/10000
return ppm
else:
pass
elif sensorVal == 0:
pass
def mq2_Calc(R0):
#calculations for smoke
#print("Inside mq2")
loadRes = 20
#print(R0)
m = -0.476
b = 1.683
sensorVal = readadc(mq2_apin, SPICLK, SPIMOSI, SPIMISO, SPICS)
#print("sensor_val: {}".format(sensorVal))
if sensorVal !=0 and sensorVal <= 1023:
sensor_volt = sensorVal*(5.0/1023.0)
#print("sensor_volt(<=1023): {}".format(sensor_volt))
RS_gas = ((5.0*loadRes)/sensor_volt) - 10
#print("RS_gas: {}".format(RS_gas))
ratio = RS_gas/R0
#print("ratio: {}".format(ratio))
if ratio >0:
ppm_log = (math.log10(ratio) - b)/m
ppm = pow(10, ppm_log)
percentage = ppm/10000
return ppm
else:
pass
elif sensorVal > 1023:
sensorVal = 1023
sensor_volt = sensorVal*(5.0/1023.0)
#print("sensor_volt(>1023): {}".format(sensor_volt))
RS_gas = ((5.0*loadRes)/sensor_volt) - 10
ratio = RS_gas/R0
#print("ratio: {}".format(ratio))
if ratio >0:
ppm_log = (math.log10(ratio) - b)/m
ppm = pow(10, ppm_log)
percentage = ppm/10000
return ppm
else:
pass
elif sensorVal == 0:
pass
def mq4_Calc(R0):
loadRes = 20
#calculating for LPG
m = -0.318
b = 1.13
sensorVal = readadc(mq4_apin, SPICLK, SPIMOSI, SPIMISO, SPICS)
if sensorVal !=0 and sensorVal <= 1023:
sensor_volt = sensorVal*(5.0/1023.0)
#print("sensor_volt(<=1023): {}".format(sensor_volt))
RS_gas = ((5.0*loadRes)/sensor_volt) - 10
#print("RS_gas: {}".format(RS_gas))
ratio = RS_gas/R0
#print("ratio: {}".format(ratio))
if ratio >0:
ppm_log = (math.log10(ratio) - b)/m
ppm = pow(10, ppm_log)
percentage = ppm/10000
return ppm
else:
pass
elif sensorVal > 1023:
sensorVal = 1023
sensor_volt = sensorVal*(5.0/1023.0)
#print("sensor_volt(>1023): {}".format(sensor_volt))
RS_gas = ((5.0*loadRes)/sensor_volt) - 10
ratio = RS_gas/R0
#print("ratio: {}".format(ratio))
if ratio >0:
ppm_log = (math.log10(ratio) - b)/m
ppm = pow(10, ppm_log)
percentage = ppm/10000
return ppm
else:
pass
elif sensorVal == 0:
pass
#main ioop
def main():
init()
print("please wait...")
print("Starting Caliberaing sensors")
R0_list = caliberate()
print("processing")
time.sleep(5)
print(GPIO.input(mq2_dpin))
print(GPIO.input(mq135_dpin))
print(GPIO.input(mq4_dpin))
while True:
if GPIO.input(mq2_dpin) == 1 and GPIO.input(mq135_dpin) == 0 and GPIO.input(mq4_dpin) == 1:
pass
# print("Normal")
# time.sleep(0.5)
else:
print("Gas leakage")
#print("Current Gas AD vaule = " +str("%.2f"%((COlevel/1024.)*3.3))+" V")
time.sleep(0.5)
# Assigning ppm values to temp variables in case of null values
mq135_ppm = mq135_Calc(R0_list[0])
if mq135_ppm != None:
mq135_ppm_temp = mq135_ppm
mq2_ppm = mq2_Calc(R0_list[1])
if mq2_ppm != None:
mq2_ppm_temp = mq2_ppm
mq4_ppm = mq4_Calc(R0_list[2])
if mq2_ppm != None:
mq4_ppm_temp = mq4_ppm
# print("ppm from mq135: {}".format(mq135_ppm))
if mq135_ppm != None:
print("ppm from mq135: {}".format(mq135_ppm))
else:
try:
print("ppm from mq135: {}".format(mq135_ppm_temp))
except:
pass
#
#
if mq2_ppm != None:
print("ppm from mq2: {}".format(mq2_ppm))
else:
try:
print("ppm from mq2: {}".format(mq2_ppm_temp))
except:
pass
if mq4_ppm != None:
print("ppm from mq4: {}".format(mq4_ppm))
else:
try:
print("ppm from mq4: {}".format(mq4_ppm_temp))
except:
pass
print(" ")
if mq135_ppm == None and mq2_ppm == None and mq4_ppm == None:
result = firebase.post('Air-Quality',{'mq135_ppm':str(mq135_ppm_temp),'mq2_ppm':str(mq2_ppm_temp),'mq4_ppm':str(mq4_ppm_temp)})
elif mq135_ppm == None and mq2_ppm != None and mq4_ppm != None:
result = firebase.post('Air-Quality',{'mq135_ppm':str(mq135_ppm_temp),'mq2_ppm':str(mq2_ppm),'mq4_ppm':str(mq4_ppm)})
elif mq135_ppm != None and mq2_ppm == None and mq4_ppm != None:
try:
result = firebase.post('Air-Quality',{'mq135_ppm':str(mq135_ppm),'mq2_ppm':str(mq2_ppm_temp),'mq4_ppm':str(mq4_ppm)})
except:
result = firebase.post('Air-Quality',{'mq135_ppm':str(mq135_ppm),'mq2_ppm':str(mq2_ppm),'mq4_ppm':str(mq4_ppm)})
elif mq135_ppm != None and mq2_ppm != None and mq4_ppm == None:
result = firebase.post('Air-Quality',{'mq135_ppm':str(mq135_ppm),'mq2_ppm':str(mq2_ppm),'mq4_ppm':str(mq4_ppm_temp)})
elif mq135_ppm == None and mq2_ppm == None and mq4_ppm != None:
try:
result = firebase.post('Air-Quality',{'mq135_ppm':str(mq135_ppm_temp),'mq2_ppm':str(mq2_ppm_temp),'mq4_ppm':str(mq4_ppm)})
except:
result = firebase.post('Air-Quality',{'mq135_ppm':str(mq135_ppm),'mq2_ppm':str(mq2_ppm),'mq4_ppm':str(mq4_ppm)})
elif mq135_ppm == None and mq2_ppm != None and mq4_ppm == None:
result = firebase.post('Air-Quality',{'mq135_ppm':str(mq135_ppm_temp),'mq2_ppm':str(mq2_ppm),'mq4_ppm':str(mq4_ppm_temp)})
elif mq135_ppm != None and mq2_ppm == None and mq4_ppm == None:
result = firebase.post('Air-Quality',{'mq135_ppm':str(mq135_ppm),'mq2_ppm':str(mq2_ppm_temp),'mq4_ppm':str(mq4_ppm_temp)})
else:
result = firebase.post('Air-Quality',{'mq135_ppm':str(mq135_ppm),'mq2_ppm':str(mq2_ppm),'mq4_ppm':str(mq4_ppm)})
time.sleep(5)
if __name__ =='__main__':
try:
main()
pass
except KeyboardInterrupt:
pass
GPIO.cleanup()