-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSAFE-ALERT.ino
More file actions
339 lines (310 loc) · 11.1 KB
/
Copy pathSAFE-ALERT.ino
File metadata and controls
339 lines (310 loc) · 11.1 KB
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
#include<LiquidCrystal_I2C.h>
#include <AltSoftSerial.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <math.h>
#include<Wire.h>
//must add i2c lcd address use i2c-scanner.ino file
LiquidCrystal_I2C lcd(0x27, 16, 2);
//--------------------------------------------------------------
//emergency phone number with country code
const String EMERGENCY_PHONE = "xxxxxxxxx";
//--------------------------------------------------------------
//GSM Module RX pin to Arduino 3
//GSM Module TX pin to Arduino 2
#define rxPin 2
#define txPin 3
SoftwareSerial sim800(rxPin,txPin);
//--------------------------------------------------------------
//GPS Module RX pin to Arduino 9
//GPS Module TX pin to Arduino 8
AltSoftSerial neogps;
TinyGPSPlus gps;
//--------------------------------------------------------------
String sms_status,sender_number,received_date,msg;
String latitude, longitude;
//--------------------------------------------------------------
#define BUZZER 12
#define BUTTON 11
//--------------------------------------------------------------
#define xPin A1
#define yPin A2
#define zPin A3
//--------------------------------------------------------------
byte updateflag;
int xaxis = 0, yaxis = 0, zaxis = 0;
int deltx = 0, delty = 0, deltz = 0;
int vibration = 2, devibrate = 75;
int magnitude = 0;
int sensitivity = 110;
double angle;
boolean impact_detected = false;
//Used to run impact routine every 2mS.
unsigned long time1;
unsigned long impact_time;
unsigned long alert_delay = 10000; //30 seconds
//--------------------------------------------------------------
/*****************************************************************************************
* setup() function
*****************************************************************************************/
void setup()
{
//--------------------------------------------------------------
//Serial.println("Arduino serial initialize");
Serial.begin(9600);
//--------------------------------------------------------------
//Serial.println("SIM800L serial initialize");
sim800.begin(9600);
//--------------------------------------------------------------
//Serial.println("NEO6M serial initialize");
neogps.begin(9600);
//--------------------------------------------------------------
pinMode(BUZZER, OUTPUT);
pinMode(BUTTON, INPUT_PULLUP);
//--------------------------------------------------------------
//initialize lcd screen
lcd.begin();
// turn on the backlight
lcd.backlight();
lcd.clear();
//--------------------------------------------------------------
sms_status = "";
sender_number="";
received_date="";
msg="";
//--------------------------------------------------------------
sim800.println("AT"); //Check GSM Module
delay(1000);
//SendAT("AT", "OK", 2000); //Check GSM Module
sim800.println("ATE1"); //Echo ON
delay(1000);
//SendAT("ATE1", "OK", 2000); //Echo ON
sim800.println("AT+CPIN?"); //Check SIM ready
delay(1000);
//SendAT("AT+CPIN?", "READY", 2000); //Check SIM ready
sim800.println("AT+CMGF=1"); //SMS text mode
delay(1000);
//SendAT("AT+CMGF=1", "OK", 2000); //SMS text mode
sim800.println("AT+CNMI=1,1,0,0,0"); /// Decides how newly arrived SMS should be handled
delay(1000);
//SendAT("AT+CNMI=1,1,0,0,0", "OK", 2000); //set sms received format
//AT +CNMI = 2,1,0,0,0 - AT +CNMI = 2,2,0,0,0 (both are same)
//--------------------------------------------------------------
time1 = micros();
//Serial.print("time1 = "); Serial.println(time1);
//--------------------------------------------------------------
//read calibrated values. otherwise false impact will trigger
//when you reset your Arduino. (By pressing reset button)
xaxis = analogRead(xPin);
yaxis = analogRead(yPin);
zaxis = analogRead(zPin);
//--------------------------------------------------------------
}
/*****************************************************************************************
* loop() function
*****************************************************************************************/
void loop()
{
//--------------------------------------------------------------
//call impact routine every 2mS
if (micros() - time1 > 1999) Impact();
//--------------------------------------------------------------
if(updateflag > 0)
{
updateflag=0;
Serial.println("Impact detected!!");
Serial.print("Magnitude:"); Serial.println(magnitude);
getGps();
digitalWrite(BUZZER, HIGH);
impact_detected = true;
impact_time = millis();
lcd.clear();
lcd.setCursor(0,0); //col=0 row=0
lcd.print("Crash Detected");
lcd.setCursor(0,1); //col=0 row=1
lcd.print("Magnitude:"+String(magnitude));
}
//--------------------------------------------------------------
if(impact_detected == true)
{
if(millis() - impact_time >= alert_delay) {
digitalWrite(BUZZER, LOW);
makeCall();
delay(1000);
sendAlert();
impact_detected = false;
impact_time = 0;
}
}
if(digitalRead(BUTTON) == LOW){
delay(200);
digitalWrite(BUZZER, LOW);
impact_detected = false;
impact_time = 0;
}
//--------------------------------------------------------------
while(sim800.available()){
parseData(sim800.readString());
}
//--------------------------------------------------------------
while(Serial.available()) {
sim800.println(Serial.readString());
}
//--------------------------------------------------------------
}
/*****************************************************************************************
* Impact() function
*****************************************************************************************/
void Impact()
{
//--------------------------------------------------------------
time1 = micros(); // resets time value
//--------------------------------------------------------------
int oldx = xaxis; //store previous axis readings for comparison
int oldy = yaxis;
int oldz = zaxis;
xaxis = analogRead(xPin);
yaxis = analogRead(yPin);
zaxis = analogRead(zPin);
//--------------------------------------------------------------
//loop counter prevents false triggering. Vibration resets if there is an impact. Don't detect new changes until that "time" has passed.
vibration--;
//Serial.print("Vibration = "); Serial.println(vibration);
if(vibration < 0) vibration = 0;
//Serial.println("Vibration Reset!");
if(vibration > 0) return;
//--------------------------------------------------------------
deltx = xaxis - oldx;
delty = yaxis - oldy;
deltz = zaxis - oldz;
//Magnitude to calculate force of impact.
magnitude = sqrt(sq(deltx) + sq(delty) + sq(deltz));
//NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
if (magnitude >= sensitivity) //impact detected
{
updateflag=1;
// reset anti-vibration counter
vibration = devibrate;
}
//NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
else
{
//if (magnitude > 15)
//Serial.println(magnitude);
//reset magnitude of impact to 0
magnitude=0;
}
//NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
}
/*****************************************************************************************
* parseData() function
*****************************************************************************************/
void parseData(String buff){
Serial.println(buff);
unsigned int len, index;
//Remove sent "AT Command" from the response string.
index = buff.indexOf("\r");
buff.remove(0, index+2);
buff.trim();
if(buff != "OK"){
index = buff.indexOf(":");
String cmd = buff.substring(0, index);
cmd.trim();
buff.remove(0, index+2);
if(cmd == "+CMTI"){
//get newly arrived memory location and store it in temp
//temp = 4
index = buff.indexOf(",");
String temp = buff.substring(index+1, buff.length());
temp = "AT+CMGR=" + temp + "\r";
//AT+CMGR=4 i.e. get message stored at memory location 4
sim800.println(temp);
}
else if(cmd == "+CMGR"){
//extractSms(buff);
//Serial.println(buff.indexOf(EMERGENCY_PHONE));
if(buff.indexOf(EMERGENCY_PHONE) > 1){
buff.toLowerCase();
//Serial.println(buff.indexOf("get gps"));
if(buff.indexOf("get gps") > 1){
getGps();
String sms_data;
sms_data = "GPS Location Data\r";
sms_data += "http://maps.google.com/maps?q=loc:";
sms_data += latitude + "," + longitude;
sendSms(sms_data);
}
}
}
}
else{
//The result of AT Command is "OK"
}
}
/*****************************************************************************************
* getGps() Function
*****************************************************************************************/
void getGps()
{
// Can take up to 60 seconds
boolean newData = false;
for (unsigned long start = millis(); millis() - start < 2000;){
while (neogps.available()){
if (gps.encode(neogps.read())){
newData = true;
break;
}
}
}
if (newData) //If newData is true
{
latitude = String(gps.location.lat(), 6);
longitude = String(gps.location.lng(), 6);
newData = false;
}
else {
Serial.println("No GPS data is available");
latitude = "";
longitude = "";
}
Serial.print("Latitude= "); Serial.println(latitude);
Serial.print("Lngitude= "); Serial.println(longitude);
}
/*****************************************************************************************
* sendAlert() function
*****************************************************************************************/
void sendAlert()
{
String sms_data;
sms_data = "Accident Alert!!\r";
sms_data += "http://maps.google.com/maps?q=loc:";
sms_data += latitude + "," + longitude;
sendSms(sms_data);
}
/*****************************************************************************************
* makeCall() function
*****************************************************************************************/
void makeCall()
{
Serial.println("calling....");
sim800.println("ATD"+EMERGENCY_PHONE+";");
delay(20000); //20 sec delay
sim800.println("ATH");
delay(1000); //1 sec delay
}
/*****************************************************************************************
* sendSms() function
*****************************************************************************************/
void sendSms(String text)
{
//return;
sim800.print("AT+CMGF=1\r");
delay(1000);
sim800.print("AT+CMGS=\""+EMERGENCY_PHONE+"\"\r");
delay(1000);
sim800.print(text);
delay(100);
sim800.write(0x1A); //ascii code for ctrl-26 //sim800.println((char)26); //ascii code for ctrl-26
delay(1000);
Serial.println("SMS Sent Successfully.");
}