-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_new.ino
749 lines (689 loc) · 17.5 KB
/
update_new.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
#include <SoftwareSerial.h>
#include <TinyGPS.h>
SoftwareSerial GPS(2,3); // RX, TX
//Initialization of Each pins and Module
TinyGPS gps;
void gpsdump(TinyGPS &gps);
bool feedgps();
void getGPS();
long lat, lon;
float LAT, LON;
int pinButton = 6;
int pinButton_panic = A1;
int mqSixValue;
int panicValue;
int input_sensor = A5;
int buz_pin = 13;
int xpin = 11; // x-axis of the accelerometer
int ypin = 10; // y-axis
int zpin = 9; // z-axis
String latitude="No Range..";
String longitude="No Range..";
int temp=0,i;
int track=0;
int trig=A3;
int echo=A4;
long duration;
int distance;
void setup()
{
//Pin configurations of Ultra Sonic
pinMode(trig,OUTPUT);
pinMode(echo,INPUT);
//Pin configurations of Trigger Pins from RF Transmitter and Reciever
pinMode(pinButton, INPUT);
pinMode(pinButton_panic, OUTPUT);
// Starting of GPS and Our Unit
Serial.begin(9600);
GPS.begin(9600);
Serial.begin(9600);
Serial.println("Accident Detection,Messaging,Tracking and Security System");
delay(2000);
gsm_init();
Serial.println("AT+CNMI=2,2,0,0,0");
Serial.println("GPS Initializing");
Serial.println(" No GPS Range ");
delay(2000);
Serial.print("Latitude : ");
Serial.print(LAT/1000000,8);
Serial.print(" :: Longitude : ");
Serial.println(LON/1000000,8);
get_gps();
Serial.println("GPS Range Discovered");
Serial.println("GPS is Ready");
delay(2000);
Serial.println("Configuring Network");
delay(2000);
Serial.println("Done!...");
Serial.flush();
Serial.flush();
Serial.println("System Ready");
temp=0;
// End of Setup
}
void loop()
{
//Checking Status Of Trigger Pins of RF and Panic Switch(To inform if any false case)
int stateButton = digitalRead(pinButton);
panicValue = analogRead(pinButton_panic);
float voltagePanic = panicValue * (5.0 / 1023.0);
if(stateButton==1 && voltagePanic<=3.00)
{
serialEvent();
get_gps();
tracking();
}
if(voltagePanic>=4.00 && stateButton==0){
serialEvent();
get_gps();
panictracking();
}
//End
//Setting Up Ultrasonic Module
digitalWrite(trig,LOW);
delayMicroseconds(2);
digitalWrite(trig,HIGH);
delayMicroseconds(10);
digitalWrite(trig,LOW);
// Finding distance using distance formula(with velocity of sound and duration(in cm))
duration=pulseIn(echo,HIGH);
distance=duration*0.034/2;
/*
* (Trigger live distance )
Serial.print("Distance :");
Serial.println(distance);
*/
// Trigger if SMS and HTTP
if(distance==3)
{
for(int f=0;f<1;f++){
get_gps();
airLocation();
delay(2000); // sets the LED on
}
}
/*
* For car tilt system
* Connected to Triple Axis Accelerometer
*/
if(digitalRead(zpin)==0){
for(int m=0;m<1;m++){
get_gps();
carTilt();
}
Serial.print(digitalRead(zpin));
// print a tab between values:
Serial.print("\t");
Serial.println("Accident !!");
}
delay(1000);
// Defined for MQ6 Gas Sensor : Check for the conc. values 0-1024
mqSixValue = analogRead(input_sensor);
if (mqSixValue >= 750)
{
digitalWrite(buz_pin, HIGH);
gasLeak();
delay(1000); // sets the LED on
}
else
{
digitalWrite(buz_pin, LOW); // sets the LED off
}
//For Vehicle Tracking
//Track<space>Your Vehicle Number (Send from phone to defined number in AT)
vehicle_tracking();
if(track==1){
get_gps();
vehicleLocation();
}
/*
* Theft Security System
* Check for the Analog Outputs and Trigger
* */
int sensorValue = analogRead(A0);
float voltage = sensorValue * (5.0 / 1023.0);
int sensorValue_two = analogRead(A2);
float voltage_two = sensorValue_two * (5.0 / 1023.0);
if (voltage == 5.00 && voltage_two <= 4.90){
get_gps();
theftSecurity();
delay(8000);
}else if (voltage <= 4.90 && voltage_two==5.00){
get_gps();
theftSecurity();
delay(8000);
}else if(voltage < 4.90 && voltage_two < 4.90){
get_gps();
theftSecurityPlus();
delay(8000);
}
//End of Loop
}
// State Check
void serialEvent()
{
int stateButton = digitalRead(pinButton);
// int stateButton_panic = digitalRead(pinButton_panic);
while(stateButton){
if(stateButton==1)
{
temp=1;
break;
}
else
temp=0;
}
}
// GSM SetUP
void gsm_init()
{
Serial.println("Finding GSM Module...");
boolean at_flag=1;
while(at_flag)
{
Serial.println("AT");
while(Serial.available()>0)
{
if(Serial.find("OK"))
at_flag=0;
}
delay(1000);
}
Serial.println("GSM Module Connected");
delay(1000);
Serial.println("Disabling ECHO");
boolean echo_flag=1;
while(echo_flag)
{
Serial.println("ATE0");
while(Serial.available()>0)
{
if(Serial.find("OK"))
echo_flag=0;
}
delay(1000);
}
Serial.println("Echo OFF");
delay(1000);
Serial.println("Finding Network..");
boolean net_flag=1;
while(net_flag)
{
Serial.println("AT+CPIN?");
while(Serial.available()>0)
{
if(Serial.find("+CPIN: READY"))
net_flag=0;
}
delay(1000);
}
Serial.println("Network Found..");
delay(1000);
}
// Retrieving GPS Data (Coordinates)
void get_gps()
{
bool newdata = false;
unsigned long start = millis();
// Every 1 seconds we print an update
while (millis() - start < 1000)
{
if (feedgps ()){
newdata = true;
}
}
if (newdata)
{
gpsdump(gps);
}
Serial.print("Latitude : ");
Serial.print(LAT/1000000,8);
Serial.print(" :: Longitude : ");
Serial.println(LON/1000000,8);
}
bool feedgps(){
while (GPS.available())
{
if (gps.encode(GPS.read()))
return true;
}
return 0;
}
void gpsdump(TinyGPS &gps)
{
//byte month, day, hour, minute, second, hundredths;
gps.get_position(&lat, &lon);
LAT = lat;
LON = lon;
{
feedgps(); // If we don't feed the gps during this long
//routine, we may drop characters and get checksum errors
}
}
// AT initialization
void init_sms()
{
Serial.println("AT+CMGF=1");
delay(400);
Serial.println("AT+CMGS=\"+919495707364\""); // use your 10 digit cell no. here
delay(400);
}
// SMS Content
void send_data(String message)
{
Serial.println(message);
delay(200);
}
// Coordinate Transmission
void send_coord(float message)
{
Serial.println(message/1000000,8);
delay(200);
}
void send_sms()
{
Serial.write(26);
}
void serial_status()
{
Serial.println("Message Sent");
delay(2000);
Serial.println("System Ready");
return;
}
void tracking()
{
init_sms();
send_data("Accident Alert !");
Serial.print("\nLatitude:");
send_coord(LAT);
Serial.print("\nLongitude:");
send_coord(LON);
// String Concatenate
float latitude = LAT;
float longitude = LON;
String buf;
buf += F("http://maps.google.com/maps?saddr=My%20location&daddr=");
buf += String(latitude/1000000,7);
buf += F(",");
buf += String(longitude/1000000,7);
buf +=F("&mode=driving");
Serial.print("\nMap:");
send_data(buf);
send_sms();
delay(2000);
serial_status();
// HTTP Requests
Serial.println("AT+CGATT?");
delay(100);
Serial.println("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"");
delay(2000);
//NSP Settings
Serial.println("AT+SAPBR=3,1,\"APN\",\"idea\"");
delay(2000);
Serial.println("AT+SAPBR=1,1");
delay(2000);
Serial.println("AT+HTTPINIT");
delay(2000);
// HTTP URL Posting
String url;
url +=F("AT+HTTPPARA=\"URL\",\"http://www.mylifedimensions.com/test/accident_alert.php?lat=");
url +=String(latitude/1000000,7);
url += F("&lon=");
url +=String(longitude/1000000,7);
url += F("&typ=Accident%20Alert&lin=");
url += F("http://maps.google.com/maps?");
url += String(latitude/1000000,7);
url += F(",");
url += String(longitude/1000000,7);
url +=F("&mode=driving\"");
Serial.println(url);
delay(8000);
Serial.println("AT+HTTPACTION=0");
delay(7000);
// read server response
Serial.println("AT+HTTPREAD");
delay(2000);
Serial.println("");
Serial.println("AT+HTTPTERM");
delay(1000);
Serial.println("");
delay(10000);
Serial.println(url);
delay(5000);
Serial.println("AT+HTTPACTION=0");
delay(6000);
Serial.println("Server Upload Successful");
}
void panictracking()
{
init_sms();
send_data("False Alert !");
Serial.print("\nLatitude:");
send_coord(LAT);
Serial.print("\nLongitude:");
send_coord(LON);
float latitude = LAT;
float longitude = LON;
String buf;
buf += F("http://maps.google.com/maps?saddr=My%20location&daddr=");
buf += String(latitude/1000000,7);
buf += F(",");
buf += String(longitude/1000000,7);
buf +=F("&mode=driving");
Serial.print("\nDrive:");
send_data(buf);
//send_data("Sorry for false alert \nThank You");
send_sms();
delay(2000);
serial_status();
Serial.println("AT+CGATT?");
delay(100);
Serial.println("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"");
delay(2000);
Serial.println("AT+SAPBR=3,1,\"APN\",\"idea\"");
delay(2000);
Serial.println("AT+SAPBR=1,1");
delay(2000);
Serial.println("AT+HTTPINIT");
delay(2000);
String url;
url +=F("AT+HTTPPARA=\"URL\",\"http://www.mylifedimensions.com/test/accident_alert.php?lat=");
url +=String(latitude/1000000,7);
url += F("&lon=");
url +=String(longitude/1000000,7);
url += F("&typ=False%20Alert&lin=");
url += F("http://maps.google.com/maps?");
url += String(latitude/1000000,7);
url += F(",");
url += String(longitude/1000000,7);
url +=F("&mode=driving\"");
Serial.println(url);
delay(8000);
Serial.println("AT+HTTPACTION=0");
delay(7000);
// read server response
Serial.println("AT+HTTPREAD");
delay(2000);
Serial.println("");
Serial.println("AT+HTTPTERM");
delay(1000);
Serial.println("");
delay(10000);
Serial.println(url);
delay(5000);
Serial.println("AT+HTTPACTION=0");
delay(6000);
Serial.println("Server Upload Successful");
}
void vehicle_tracking()
{
while(Serial.available())
{
if(Serial.find("Track KL46D5782")){
track=1;
}
else
track=0;
}
}
void carTilt(){
init_sms();
send_data("Car Tilt");
//send_data("Your Vehicle Current Location is:");
Serial.print("\nLatitude:");
send_coord(LAT);
Serial.print("\nLongitude:");
send_coord(LON);
float latitude = LAT;
float longitude = LON;
String buf;
buf += F("http://maps.google.com/maps?saddr=My%20location&daddr=");
buf += String(latitude/1000000,7);
buf += F(",");
buf += String(longitude/1000000,7);
buf +=F("&mode=driving");
Serial.print("\nDrive:");
send_data(buf);
//send_data("Sorry for false alert \nThank You");
send_sms();
delay(2000);
serial_status();
Serial.println("AT+CGATT?");
delay(100);
Serial.println("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"");
delay(2000);
Serial.println("AT+SAPBR=3,1,\"APN\",\"idea\"");
delay(2000);
Serial.println("AT+SAPBR=1,1");
delay(2000);
Serial.println("AT+HTTPINIT");
delay(2000);
String url;
url +=F("AT+HTTPPARA=\"URL\",\"http://www.mylifedimensions.com/test/accident_alert.php?lat=");
url +=String(latitude/1000000,7);
url += F("&lon=");
url +=String(longitude/1000000,7);
url += F("&typ=Car%20Tilt&lin=");
url += F("http://maps.google.com/maps?");
url += String(latitude/1000000,7);
url += F(",");
url += String(longitude/1000000,7);
url +=F("&mode=driving\"");
Serial.println(url);
delay(8000);
Serial.println("AT+HTTPACTION=0");
delay(7000);
// read server response
Serial.println("AT+HTTPREAD");
delay(2000);
Serial.println("");
Serial.println("AT+HTTPTERM");
delay(1000);
Serial.println("");
delay(10000);
Serial.println(url);
delay(5000);
Serial.println("AT+HTTPACTION=0");
delay(6000);
Serial.println("Server Upload Successful");
}
void theftSecurity(){
init_sms();
//send_data("Vehicle Number:KL46D5782");
//send_data("Your Vehicle Current Location is:");
send_data("Single Line Cut!");
Serial.print("Latitude:");
send_coord(LAT);
Serial.print("\nLongitude:");
send_coord(LON);
float latitude = LAT;
float longitude = LON;
String buf;
buf += F("http://maps.google.com/maps?saddr=My%20location&daddr=");
buf += String(latitude/1000000,7);
buf += F(",");
buf += String(longitude/1000000,7);
buf +=F("&mode=driving");
Serial.print("\nDrive:");
send_data(buf);
//send_data("Sorry for false alert \nThank You");
send_sms();
delay(2000);
serial_status();
}
void theftSecurityPlus(){
init_sms();
send_data("Line 1&2 Cut!");
Serial.print("Latitude:");
send_coord(LAT);
Serial.print("\nLongitude:");
send_coord(LON);
float latitude = LAT;
float longitude = LON;
String buf;
buf += F("http://maps.google.com/maps?saddr=My%20location&daddr=");
buf += String(latitude/1000000,7);
buf += F(",");
buf += String(longitude/1000000,7);
buf +=F("&mode=driving");
Serial.print("Map:");
send_data(buf);
//send_data("Sorry for false alert \nThank You");
send_sms();
delay(2000);
serial_status();
}
void vehicleLocation(){
init_sms();
send_data("Currently At");
//send_data("Your Vehicle Current Location is:");
Serial.print("\nLatitude:");
send_coord(LAT);
Serial.print("\nLongitude:");
send_coord(LON);
float latitude = LAT;
float longitude = LON;
String buf;
buf += F("http://maps.google.com/maps?saddr=My%20location&daddr=");
buf += String(latitude/1000000,7);
buf += F(",");
buf += String(longitude/1000000,7);
buf +=F("&mode=driving");
Serial.print("\nLocation Map:");
send_data(buf);
//send_data("Sorry for false alert \nThank You");
send_sms();
delay(2000);
serial_status();
}
void airLocation(){
init_sms();
send_data("Airline@");
//send_data("Your Vehicle Current Location is:");
Serial.print("\nLatitude:");
send_coord(LAT);
Serial.print("\nLongitude:");
send_coord(LON);
float latitude = LAT;
float longitude = LON;
String buf;
buf += F("http://maps.google.com/maps?saddr=My%20location&daddr=");
buf += String(latitude/1000000,7);
buf += F(",");
buf += String(longitude/1000000,7);
buf +=F("&mode=driving");
Serial.print("\nLocation Map:");
send_data(buf);
//send_data("Sorry for false alert \nThank You");
send_sms();
delay(2000);
serial_status();
Serial.println("AT+CGATT?");
delay(100);
Serial.println("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"");
delay(2000);
Serial.println("AT+SAPBR=3,1,\"APN\",\"idea\"");
delay(2000);
Serial.println("AT+SAPBR=1,1");
delay(2000);
Serial.println("AT+HTTPINIT");
delay(2000);
String url;
url +=F("AT+HTTPPARA=\"URL\",\"http://www.mylifedimensions.com/test/accident_alert.php?lat=");
url +=String(latitude/1000000,7);
url += F("&lon=");
url +=String(longitude/1000000,7);
url += F("&typ=Airline@&lin=");
url += F("http://maps.google.com/maps?");
url += String(latitude/1000000,7);
url += F(",");
url += String(longitude/1000000,7);
url +=F("&mode=driving\"");
Serial.println(url);
delay(8000);
Serial.println("AT+HTTPACTION=0");
delay(7000);
// read server response
Serial.println("AT+HTTPREAD");
delay(2000);
Serial.println("");
Serial.println("AT+HTTPTERM");
delay(1000);
Serial.println("");
delay(10000);
Serial.println(url);
delay(5000);
Serial.println("AT+HTTPACTION=0");
delay(6000);
Serial.println("Server Upload Successful");
}
void gasLeak()
{
init_sms();
send_data("Gas Leak!");
//send_data("Your Vehicle Current Location is:");
if (mqSixValue >= 950)
{
Serial.print("Concentration Level Very High");
send_data("Concentration Level Very High");
// sets the LED on
}
Serial.print("\nLatitude:");
send_coord(LAT);
Serial.print("\nLongitude:");
send_coord(LON);
float latitude = LAT;
float longitude = LON;
String buf;
buf += F("http://maps.google.com/maps?saddr=My%20location&daddr=");
buf += String(latitude/1000000,7);
buf += F(",");
buf += String(longitude/1000000,7);
buf +=F("&mode=driving");
Serial.print("\nMap:");
send_data(buf);
//send_data("Sorry for false alert \nThank You");
send_sms();
delay(2000);
serial_status();
Serial.println("AT+CGATT?");
delay(100);
Serial.println("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"");
delay(2000);
Serial.println("AT+SAPBR=3,1,\"APN\",\"idea\"");
delay(2000);
Serial.println("AT+SAPBR=1,1");
delay(2000);
Serial.println("AT+HTTPINIT");
delay(2000);
String url;
url +=F("AT+HTTPPARA=\"URL\",\"http://www.mylifedimensions.com/test/accident_alert.php?lat=");
url +=String(latitude/1000000,7);
url += F("&lon=");
url +=String(longitude/1000000,7);
url += F("&typ=Gas%20Leak&lin=");
url += F("http://maps.google.com/maps?");
url += String(latitude/1000000,7);
url += F(",");
url += String(longitude/1000000,7);
url +=F("&mode=driving\"");
Serial.println(url);
delay(8000);
Serial.println("AT+HTTPACTION=0");
delay(7000);
// read server response
Serial.println("AT+HTTPREAD");
delay(2000);
Serial.println("");
Serial.println("AT+HTTPTERM");
delay(1000);
Serial.println("");
delay(10000);
Serial.println(url);
delay(5000);
Serial.println("AT+HTTPACTION=0");
delay(6000);
Serial.println("Server Upload Successful");
}
// End Of Program