-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharduino_2.ino
341 lines (299 loc) · 8.5 KB
/
arduino_2.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
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
#include <dht.h> // Include library
#include <SFE_BMP180.h>
#include <Wire.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#define ALTITUDE 1655.0
#define DHT11_PIN 5 // Defines pin number to which the sensor is connected
#define PIR_PIN 3
#define BUZZER_PIN 9
#define MQ5_PIN 7
#define MQ2_PIN 1
#define MQ7_PIN 2
int RXPin = 6;
int TXPin = 10;
SFE_BMP180 pressure;
dht DHT; // Creates a DHT object
TinyGPSPlus gps;
SoftwareSerial gpsSerial(RXPin, TXPin);
long unsigned int lowIn; // the amount of milliseconds the sensor has to be low //before we assume all motion has stopp
long unsigned int pause = 5000;
float sensorValue;
boolean lockLow = true;
boolean takeLowTime;
int mq5_gas;
float mq2_gas;
void buzz(int time_delay)
{
tone(BUZZER_PIN, 1000);
delay(time_delay);
noTone(BUZZER_PIN);
delay(time_delay);
}
void setup_pir()
{
pinMode(PIR_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
const int calibrationTime = 30;
digitalWrite(PIR_PIN, LOW);
Serial.print("calibrating sensor ");
for (int i = 0; i < calibrationTime; i++)
{
Serial.print(".");
delay(1000);
}
Serial.println(" done");
Serial.println("SENSOR ACTIVE");
delay(50);
}
void setup_bmp180()
{
Serial.println("BMP180 REBOOT");
// Initialize the sensor (it is important to get calibration values stored on the device).
if (pressure.begin())
Serial.println("BMP180 init success");
else
{
// Oops, something went wrong, this is usually a connection problem,
// see the comments at the top of this sketch for the proper connections.
Serial.println("BMP180 init fail\n\n");
while (1)
; // Pause forever.
}
}
void setup_mq3()
{
Serial.println("MQ3 warming up!");
delay(20000); // allow the MQ3 to warm up
}
void setup_mq2()
{
delay(20000);
}
void setup_gps()
{
gpsSerial.begin(9600);
}
void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
setup_pir();
setup_bmp180();
setup_mq3();
setup_mq2();
setup_gps();
}
void loop_dht11()
{
int readData = DHT.read11(DHT11_PIN);
float t = DHT.temperature; // Read temperature
float h = DHT.humidity; // Read humidity
Serial.print("Temperature = ");
Serial.print(DHT.temperature);
Serial.print("C | ");
Serial.print((t * 9.0) / 5.0 + 32.0); // Convert celsius to fahrenheit
Serial.println("F ");
Serial.print("Humidity = ");
Serial.print(h);
Serial.println("% ");
Serial.println("");
delay(3000); // wait two seconds
}
void loop_pir()
{
if (digitalRead(PIR_PIN) == HIGH)
{
if (lockLow)
{ // makes sure we wait for a transition to LOW before any further output is made:
lockLow = false;
Serial.println("---");
Serial.print("motion detected at ");
buzz(500);
Serial.print(millis() / 1000);
Serial.println(" sec");
delay(50);
}
takeLowTime = true;
}
if (digitalRead(PIR_PIN) == LOW)
{
if (takeLowTime)
{
lowIn = millis(); // save the time of the transition from high to LOW
takeLowTime = false; // make sure this is only done at the start of a LOW phase
} // if the sensor is low for more than the given pause, //we assume that no more motion is going to happen
if (!lockLow && millis() - lowIn > pause)
{ // makes sure this block of code is only executed again after //a new motion sequence has been detected
lockLow = true;
Serial.print("motion ended at "); // output
buzz(500);
Serial.print((millis() - pause) / 1000);
Serial.println(" sec");
delay(50);
}
}
}
void loop_bmp180()
{
char status;
double T, P, p0, a;
Serial.println();
Serial.print("provided altitude: ");
Serial.print(ALTITUDE, 0);
Serial.print(" meters, ");
Serial.print(ALTITUDE * 3.28084, 0);
Serial.println(" feet");
status = pressure.startTemperature();
if (status != 0)
{
// Wait for the measurement to complete:
delay(status);
status = pressure.getTemperature(T);
if (status != 0)
{
// Print out the measurement:
Serial.print("temperature: ");
Serial.print(T, 2);
Serial.print(" deg C, ");
Serial.print((9.0 / 5.0) * T + 32.0, 2);
Serial.println(" deg F");
status = pressure.startPressure(3);
if (status != 0)
{
// Wait for the measurement to complete:
delay(status);
status = pressure.getPressure(P, T);
if (status != 0)
{
// Print out the measurement:
Serial.print("absolute pressure: ");
Serial.print(P, 2);
Serial.print(" mb, ");
Serial.print(P * 0.0295333727, 2);
Serial.println(" inHg");
p0 = pressure.sealevel(P, ALTITUDE); // we're at 1655 meters (Boulder, CO)
Serial.print("relative (sea-level) pressure: ");
Serial.print(p0, 2);
Serial.print(" mb, ");
Serial.print(p0 * 0.0295333727, 2);
Serial.println(" inHg");
a = pressure.altitude(P, p0);
Serial.print("computed altitude: ");
Serial.print(a, 0);
Serial.print(" meters, ");
Serial.print(a * 3.28084, 0);
Serial.println(" feet");
}
else
Serial.println("error retrieving pressure measurement\n");
}
else
Serial.println("error starting pressure measurement\n");
}
else
Serial.println("error retrieving temperature measurement\n");
}
else
Serial.println("error starting temperature measurement\n");
delay(5000); // Pause for 5 seconds.
}
void loop_mq5()
{
sensorValue = analogRead(MQ5_PIN); // read analog input pin 0
Serial.print("Sensor Value: ");
Serial.println(sensorValue);
delay(2000); // wait 2s for next reading
}
void loop_mq2()
{
mq2_gas = analogRead(MQ2_PIN);
Serial.print("Sensor Value: ");
Serial.println(mq2_gas);
delay(2000); // wait 2s for next reading
}
void loop_mq7()
{
float sensorValue = analogRead(MQ7_PIN);
Serial.println(sensorValue);
delay(1000);
}
void displayInfo()
{
if (gps.location.isValid())
{
Serial.print("Latitude: ");
Serial.println(gps.location.lat(), 6);
Serial.print("Longitude: ");
Serial.println(gps.location.lng(), 6);
Serial.print("Altitude: ");
Serial.println(gps.altitude.meters());
}
else
{
Serial.println("Location: Not Available");
}
Serial.print("Date: ");
if (gps.date.isValid())
{
Serial.print(gps.date.month());
Serial.print("/");
Serial.print(gps.date.day());
Serial.print("/");
Serial.println(gps.date.year());
}
else
{
Serial.println("Not Available");
}
Serial.print("Time: ");
if (gps.time.isValid())
{
if (gps.time.hour() < 10)
Serial.print(F("0"));
Serial.print(gps.time.hour());
Serial.print(":");
if (gps.time.minute() < 10)
Serial.print(F("0"));
Serial.print(gps.time.minute());
Serial.print(":");
if (gps.time.second() < 10)
Serial.print(F("0"));
Serial.print(gps.time.second());
Serial.print(".");
if (gps.time.centisecond() < 10)
Serial.print(F("0"));
Serial.println(gps.time.centisecond());
}
else
{
Serial.println("Not Available");
}
Serial.println();
Serial.println();
delay(1000);
}
void loop_gps()
{
while (gpsSerial.available() > 0)
if (gps.encode(gpsSerial.read()))
displayInfo();
// If 5000 milliseconds pass and there are no characters coming in
// over the software serial port, show a "No GPS detected" error
if (millis() > 5000 && gps.charsProcessed() < 10)
{
Serial.println("No GPS detected");
while (true)
;
}
}
void loop()
{
loop_dht11();
loop_pir();
loop_bmp180();
loop_mq5();
loop_mq2();
loop_mq7();
loop_gps();
}