-
Notifications
You must be signed in to change notification settings - Fork 1
/
part2_sketch.ino
368 lines (327 loc) · 7.5 KB
/
part2_sketch.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
/*
#include <SoftwareSerial.h>
SoftwareSerial SIM900A(10,11);
void setup()
{
SIM900A.begin(9600); // Setting the baud rate of GSM Module
Serial.begin(9600); // Setting the baud rate of Serial Monitor (Arduino)
Serial.println ("SIM900A Ready");
delay(100);
Serial.println ("Type s to send message or r to receive message");
}
void loop()
{
if (Serial.available()>0)
switch(Serial.read())
{
case 's':
SendMessage();
break;
case 'r':
RecieveMessage();
break;
}
if (SIM900A.available()>0)
Serial.write(SIM900A.read());
}
void SendMessage()
{
Serial.println ("Sending Message");
SIM900A.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
delay(1000);
Serial.println ("Set SMS Number");
SIM900A.println("AT+CMGS=\"+918129991427\"\r"); //Mobile phone number to send message
delay(1000);
Serial.println ("Set SMS Content");
SIM900A.println("Good morning, how are you doing?");// Messsage content
delay(100);
Serial.println ("Finish");
SIM900A.println((char)26);// ASCII code of CTRL+Z
delay(1000);
Serial.println ("Message has been sent ->SMS Selesai dikirim");
delay(1000);
SIM900A.write(0x1A); // Send message termination character
delay(1000);
}
void RecieveMessage()
{
Serial.println ("SIM900A Membaca SMS");
delay (1000);
SIM900A.println("AT+CNMI=2,2,0,0,0"); // AT Command to receive a live SMS
delay(1000);
Serial.write ("Unread Message done");
delay(1000);
SIM900A.write(0x1A); // Send message termination character
delay(1000);
}
*/
#include<SoftwareSerial.h>
SoftwareSerial Serial1(2,3); //make RX arduino line is pin 2, make TX arduino line is pin 3.
SoftwareSerial gps(10,11);
#define x A1
#define y A2
#define z A3
int xsample=0;
int ysample=0;
int zsample=0;
#define samples 10
#define minVal -50
#define MaxVal 50
int i=0,k=0;
int gps_status=0;
float latitude=0;
float logitude=0;
String Speed="";
String gpsString="";
char *test="$GPRMC";
void initModule(String cmd, char *res, int t)
{
while(1)
{
Serial.println(cmd);
Serial1.println(cmd);
delay(100);
while(Serial1.available()>0)
{
if(Serial1.find(res))
{
Serial.println(res);
delay(t);
return;
}
else
{
Serial.println("Error");
}
}
delay(t);
}
}
void setup()
{
Serial1.begin(9600);
Serial.begin(9600);
lcd.begin(16,2);
lcd.print("Accident Alert ");
lcd.setCursor(0,1);
lcd.print(" System ");
delay(2000);
lcd.clear();
lcd.print("Initializing");
lcd.setCursor(0,1);
lcd.print("Please Wait...");
delay(1000);
Serial.println("Initializing....");
initModule("AT","OK",1000);
initModule("ATE1","OK",1000);
initModule("AT+CPIN?","READY",1000);
initModule("AT+CMGF=1","OK",1000);
initModule("AT+CNMI=2,2,0,0,0","OK",1000);
Serial.println("Initialized Successfully");
lcd.clear();
lcd.print("Initialized");
lcd.setCursor(0,1);
lcd.print("Successfully");
delay(2000);
lcd.clear();
lcd.print("Callibrating ");
lcd.setCursor(0,1);
lcd.print("Acceleromiter");
for(int i=0;i<samples;i++)
{
xsample+=analogRead(x);
ysample+=analogRead(y);
zsample+=analogRead(z);
}
xsample/=samples;
ysample/=samples;
zsample/=samples;
Serial.println(xsample);
Serial.println(ysample);
Serial.println(zsample);
delay(1000);
lcd.clear();
lcd.print("Waiting For GPS");
lcd.setCursor(0,1);
lcd.print(" Signal ");
delay(2000);
gps.begin(9600);
get_gps();
show_coordinate();
delay(2000);
lcd.clear();
lcd.print("GPS is Ready");
delay(1000);
lcd.clear();
lcd.print("System Ready");
Serial.println("System Ready..");
}
void loop()
{
int value1=analogRead(x);
int value2=analogRead(y);
int value3=analogRead(z);
int xValue=xsample-value1;
int yValue=ysample-value2;
int zValue=zsample-value3;
Serial.print("x=");
Serial.println(xValue);
Serial.print("y=");
Serial.println(yValue);
Serial.print("z=");
Serial.println(zValue);
if(xValue < minVal || xValue > MaxVal || yValue < minVal || yValue > MaxVal || zValue < minVal || zValue > MaxVal)
{
get_gps();
show_coordinate();
lcd.clear();
lcd.print("Sending SMS ");
Serial.println("Sending SMS");
Send();
Serial.println("SMS Sent");
delay(2000);
lcd.clear();
lcd.print("System Ready");
}
}
void gpsEvent()
{
gpsString="";
while(1)
{
while (gps.available()>0) //Serial incoming data from GPS
{
char inChar = (char)gps.read();
gpsString+= inChar; //store incoming data from GPS to temparary string str[]
i++;
// Serial.print(inChar);
if (i < 7)
{
if(gpsString[i-1] != test[i-1]) //check for right string
{
i=0;
gpsString="";
}
}
if(inChar=='\r')
{
if(i>60)
{
gps_status=1;
break;
}
else
{
i=0;
}
}
}
if(gps_status)
break;
}
}
void get_gps()
{
lcd.clear();
lcd.print("Getting GPS Data");
lcd.setCursor(0,1);
lcd.print("Please Wait.....");
gps_status=0;
int x=0;
while(gps_status==0)
{
gpsEvent();
int str_lenth=i;
coordinate2dec();
i=0;x=0;
str_lenth=0;
}
}
void show_coordinate()
{
lcd.clear();
lcd.print("Lat:");
lcd.print(latitude);
lcd.setCursor(0,1);
lcd.print("Log:");
lcd.print(logitude);
Serial.print("Latitude:");
Serial.println(latitude);
Serial.print("Longitude:");
Serial.println(logitude);
Serial.print("Speed(in knots)=");
Serial.println(Speed);
delay(2000);
lcd.clear();
lcd.print("Speed(Knots):");
lcd.setCursor(0,1);
lcd.print(Speed);
}
void coordinate2dec()
{
String lat_degree="";
for(i=20;i<=21;i++)
lat_degree+=gpsString[i];
String lat_minut="";
for(i=22;i<=28;i++)
lat_minut+=gpsString[i];
String log_degree="";
for(i=32;i<=34;i++)
log_degree+=gpsString[i];
String log_minut="";
for(i=35;i<=41;i++)
log_minut+=gpsString[i];
Speed="";
for(i=45;i<48;i++) //extract longitude from string
Speed+=gpsString[i];
float minut= lat_minut.toFloat();
minut=minut/60;
float degree=lat_degree.toFloat();
latitude=degree+minut;
minut= log_minut.toFloat();
minut=minut/60;
degree=log_degree.toFloat();
logitude=degree+minut;
}
void Send()
{
Serial1.println("AT");
delay(500);
serialPrint();
Serial1.println("AT+CMGF=1");
delay(500);
serialPrint();
Serial1.print("AT+CMGS=");
Serial1.print('"');
Serial1.print("+919037947101"); //mobile no. for SMS alert
Serial1.println('"');
delay(500);
serialPrint();
Serial1.print("Latitude:");
Serial1.println(latitude);
delay(500);
serialPrint();
Serial1.print(" longitude:");
Serial1.println(logitude);
delay(500);
serialPrint();
Serial1.print(" Speed:");
Serial1.print(Speed);
Serial1.println("Knots");
delay(500);
serialPrint();
Serial1.print("http://maps.google.com/maps?&z=15&mrt=yp&t=k&q=");
Serial1.print(latitude,6);
Serial1.print("+"); //28.612953, 77.231545 //28.612953,77.2293563
Serial1.print(logitude,6);
Serial1.write(26);
delay(2000);
serialPrint();
}
void serialPrint()
{
while(Serial1.available()>0)
{
Serial.print(Serial1.read());
}
}