-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAir_Analyzer.ino
248 lines (210 loc) · 6.27 KB
/
Air_Analyzer.ino
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
#include <MQUnifiedsensor.h>
#include <SPI.h>
#include <WiFiNINA.h>
//const char* SSID
//const char* PASSWORD
//const char* SCRIPT_ID
const char* HOST = "script.google.com";
const int Pin3 = A0;
const int Pin4 = A1;
const int Pin135 = A2;
const int Pin7 = A3;
const int Pin8 = A4;
const int Pin9 = A5;
const float RatioMQ3CleanAir = 60;
const float RatioMQ4CleanAir = 4.4;
const float RatioMQ135CleanAir = 3.6;
const float RatioMQ7CleanAir = 27.5;
const float RatioMQ8CleanAir = 70;
const float RatioMQ9CleanAir = 9.6;
const int ADC_Bit_Resolution = 10;
const float Voltage_Resolution = 5;
const String Board = "MKR1010";
MQUnifiedsensor MQ3(Board.c_str(), Voltage_Resolution, ADC_Bit_Resolution, Pin3, Board.c_str());
MQUnifiedsensor MQ4(Board.c_str(), Voltage_Resolution, ADC_Bit_Resolution, Pin4, Board.c_str());
MQUnifiedsensor MQ135(Board.c_str(), Voltage_Resolution, ADC_Bit_Resolution, Pin135, Board.c_str());
MQUnifiedsensor MQ7(Board.c_str(), Voltage_Resolution, ADC_Bit_Resolution, Pin7, Board.c_str());
MQUnifiedsensor MQ8(Board.c_str(), Voltage_Resolution, ADC_Bit_Resolution, Pin8, Board.c_str());
MQUnifiedsensor MQ9(Board.c_str(), Voltage_Resolution, ADC_Bit_Resolution, Pin9, Board.c_str());
float Alcohol = 0.0;
float Benzene = 0.0;
float Hexane = 0.0;
float Methane = 0.0;
float Smoke = 0.0;
float Carbondioxide = 0.0;
float Toluene = 0.0;
float Ammonia = 0.0;
float Acetone = 0.0;
float Carbonmonoxide = 0.0;
float Hydrogen = 0.0;
float Flaming_gas = 0.0;
unsigned long time_ms = 0;
unsigned long time_1000_ms_buf = 0;
unsigned long time_sheet_update_buf = 0;
void setup() {
Serial.begin(9600);
connectToWiFi();
MQ3.init();
MQ3.setRegressionMethod(1);
MQ3.setR0(0.45);
MQ4.init();
MQ4.setRegressionMethod(1);
MQ4.setR0(14.23);
MQ135.init();
MQ135.setRegressionMethod(1);
MQ135.setR0(9.03);
MQ7.init();
MQ7.setRegressionMethod(1);
MQ7.setR0(5.90);
MQ8.init();
MQ8.setRegressionMethod(1);
MQ8.setR0(0.91);
MQ9.init();
MQ9.setRegressionMethod(1);
MQ9.setR0(13.93);
Serial.print("Calibrating, please wait.");
float MQ3calcR0 = 0.0;
float MQ4calcR0 = 0.0;
float MQ135calcR0 = 0.0;
float MQ7calcR0 = 0.0;
float MQ8calcR0 = 0.0;
float MQ9calcR0 = 0.0;
for (int i = 1; i <= 10; i++) {
MQ3.update();
MQ4.update();
MQ135.update();
MQ7.update();
MQ8.update();
MQ9.update();
MQ3calcR0 += MQ3.calibrate(RatioMQ3CleanAir);
MQ4calcR0 += MQ4.calibrate(RatioMQ4CleanAir);
MQ135calcR0 += MQ135.calibrate(RatioMQ135CleanAir);
MQ7calcR0 += MQ7.calibrate(RatioMQ7CleanAir);
MQ8calcR0 += MQ8.calibrate(RatioMQ8CleanAir);
MQ9calcR0 += MQ9.calibrate(RatioMQ9CleanAir);
Serial.print(".");
}
MQ3.setR0(MQ3calcR0 / 10);
MQ4.setR0(MQ4calcR0 / 10);
MQ135.setR0(MQ135calcR0 / 10);
MQ7.setR0(MQ7calcR0 / 10);
MQ8.setR0(MQ8calcR0 / 10);
MQ9.setR0(MQ9calcR0 / 10);
Serial.println(" done!.");
Serial.print("(MQ3 - MQ9):");
Serial.print(MQ3calcR0 / 10);
Serial.print(" | ");
Serial.print(MQ4calcR0 / 10);
Serial.print(" | ");
Serial.print(MQ135calcR0 / 10);
Serial.print(" | ");
Serial.print(MQ7calcR0 / 10);
Serial.print(" | ");
Serial.print(MQ8calcR0 / 10);
Serial.print(" | ");
Serial.print(MQ9calcR0 / 10);
Serial.println(" |");
}
void loop() {
time_ms = millis();
if (time_ms - time_1000_ms_buf >= 1000) {
time_1000_ms_buf = time_ms;
readGasSensors();
}
if (time_ms - time_sheet_update_buf >= 60000) {
time_sheet_update_buf = time_ms;
updateGoogleSheet();
}
}
void connectToWiFi() {
WiFi.begin(SSID, PASSWORD);
Serial.print("Connecting to " + String(SSID));
while (WiFi.status() != WL_CONNECTED) {
WiFi.begin(SSID, PASSWORD);
delay(5000);
Serial.print(".");
}
Serial.println("\nWiFi connected");
Serial.print("IP: ");
Serial.println(WiFi.localIP());
}
void readGasSensors() {
MQ3.update();
MQ4.update();
MQ135.update();
MQ7.update();
MQ8.update();
MQ9.update();
MQ3.setA(0.3934);
MQ3.setB(-1.504);
Alcohol = MQ3.readSensor();
MQ3.setA(4.8387);
MQ3.setB(-2.68);
Benzene = MQ3.readSensor();
MQ3.setA(7585.3);
MQ3.setB(-2.849);
Hexane = MQ3.readSensor();
MQ4.setA(1012.7);
MQ4.setB(-2.786);
Methane = MQ4.readSensor() / 10;
MQ4.setA(30000000);
MQ4.setB(-8.308);
Smoke = MQ4.readSensor() / 10000;
MQ135.setA(110.47);
MQ135.setB(-2.862);
Carbondioxide = MQ135.readSensor() * 150;
MQ135.setA(44.947);
MQ135.setB(-3.445);
Toluene = MQ135.readSensor();
MQ135.setA(102.2);
MQ135.setB(-2.473);
Ammonia = MQ135.readSensor() / 1000;
MQ135.setA(34.668);
MQ135.setB(-3.369);
Acetone = MQ135.readSensor();
MQ7.setA(99.042);
MQ7.setB(-1.518);
Carbonmonoxide = MQ7.readSensor();
MQ8.setA(976.97);
MQ8.setB(-0.688);
Hydrogen = MQ8.readSensor() / 60;
MQ9.setA(1000.5);
MQ9.setB(-2.186);
Flaming_gas = MQ9.readSensor() / 6;
}
String buildQueryString() {
String queryString = "Alcohol=" + String(Alcohol) +
"&Benzene=" + String(Benzene) +
"&Hexane=" + String(Hexane) +
"&Methane=" + String(Methane) +
"&Smoke=" + String(Smoke) +
"&Carbondioxide=" + String(Carbondioxide) +
"&Toluene=" + String(Toluene) +
"&Ammonia=" + String(Ammonia) +
"&Acetone=" + String(Acetone) +
"&Carbonmonoxide=" + String(Carbonmonoxide) +
"&Hydrogen=" + String(Hydrogen) +
"&Flaming_gas=" + String(Flaming_gas);
return queryString;
}
void updateGoogleSheet() {
connectToWiFi();
Serial.print("Connecting to ");
Serial.println(HOST);
WiFiSSLClient sheetClient;
const int sheetHttpsPort = 443;
if (!sheetClient.connect(HOST, sheetHttpsPort)) {
Serial.println("Connection failed");
return updateGoogleSheet();
}
String path = String("https://script.google.com") + "/macros/s/" + String(SCRIPT_ID) + "/dev";
String queryString = buildQueryString();
String url = path + "?" + queryString;
Serial.print("Requesting URL: ");
Serial.println(url);
sheetClient.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + HOST + "\r\n" +
"Connection: close\r\n\r\n");
Serial.println();
Serial.println("Closing connection");
}