-
Notifications
You must be signed in to change notification settings - Fork 0
/
CarShaker-V01.ino
499 lines (431 loc) · 11.3 KB
/
CarShaker-V01.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
#include <SoftwareSerial.h>
#include <TinyGPS++.h>
#include "MPU9250.h"
//#include "Adafruit_FONA.h"
#define FONA_RX 3
#define FONA_TX 4
#define FONA_RST 2
// The TinyGPS++ object
TinyGPSPlus gps;
//Create software serial object to communicate with SIM800L
SoftwareSerial ssGSM(3, 4); //SIM800L Tx & Rx is connected to Arduino #3 & #2
//SoftwareSerial ssGSM(11, 12); //blind gelegt, damit die Variable definiert ist.
SoftwareSerial ssGPS(6, 7); //GPS Tx & Rx is connected to Arduino #7 & #6
SoftwareSerial *fonaSerial = &ssGSM;
// an MPU9250 object with the MPU-9250 sensor on I2C bus 0 with address 0x68
MPU9250 IMU(Wire,0x68);
//Adafruit_FONA fona = Adafruit_FONA(FONA_RST);
int status;
int awake = 0;
unsigned long start = millis();
unsigned long rm2timer = millis();
// runmodes:
// 0 unarmed, waiting for SMS to arm
// 1 armed, waiting for gyro to alert or SMS to disarm
// 2 alert, reading GPS, after 9 Min going to runmode 3
// 3 send, sending last good GPS Position and return to runmode 2
// 99 debugging , only execute statements in block
int runmode = 0;
void setup()
{
//Begin serial communication with Arduino and Arduino IDE (Serial Monitor)
Serial.begin(9600);
Serial.print("Initializing");
delay(100);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
//Begin serial communication with Arduino and GPS
Serial.print(".");
ssGPS.begin(4800);
ssGPS.stopListening();
//Begin serial communication with Arduino and SIM800L
Serial.print(".");
//ssGSM.begin(9600);
fonaSerial->begin(4800);
/*
if (! fona.begin(*fonaSerial)) {
Serial.println(F("Couldn't find FONA"));
while(1);
}
Serial.print(F("FONA is OK "));
Serial.print(F("Type: "));
Serial.println(fona.type());
while (!(fona.getNetworkStatus() == 1 or fona.getNetworkStatus() == 5 )) {
delay(200);
Serial.print(".");
}
Serial.println("Phone connected");
*/
/*
Serial.print(".");
ssGSM.listen();
Serial.print(".");
ssGSM.println("AT"); //Once the handshake test is successful, it will back to OK
Serial.print(".");
Serial.print(ssGSM.readString());
updateSerialGSM();
ssGSM.println("ATE0"); //Echo aus, das stört nur.
Serial.print(".");
Serial.print(ssGSM.readString());
updateSerialGSM();
ssGSM.println("AT+CPIN=8352"); //PIN
Serial.print(".");
Serial.print(ssGSM.readString());
updateSerialGSM();
Serial.print("-");
delay(1000);
Serial.print(ssGSM.readString());
ssGSM.println("AT+CMGF=1"); //SMS-Format setzen
Serial.print(".");
Serial.print(ssGSM.readString());
ssGSM.println("AT+CMGD=1,4"); //löschen aller SMS auf der Karte nach dem einlesen
Serial.print(".");
Serial.print(ssGSM.readString());
updateSerialGSM();
// Also das funtioniert schon mal.
ssGSM.println("AT+CMGS=\"+49phonenumber\"");
Serial.print(ssGSM.readString());
updateSerialGSM();
ssGSM.print("Big Master Test");
ssGSM.print(" - ");
ssGSM.print("Part 2");
ssGSM.print((char)26);
Serial.print(ssGSM.readString());
*/
//mySerial.println("AT+CCID"); //Read SIM information to confirm whether the SIM is plugged
//updateSerial();
//mySerial.println("AT+CREG?"); //Check whether it has registered in the network
//updateSerial();
Serial.print(".");
status = IMU.begin();
if (status < 0) {
Serial.println("IMU initialization unsuccessful");
Serial.println("Check IMU wiring or try cycling power");
Serial.print("Status: ");
Serial.println(status);
while (1) {}
}
Serial.print(".");
// setting the accelerometer full scale range to +/-8G
IMU.setAccelRange(MPU9250::ACCEL_RANGE_8G);
Serial.print(".");
// setting the gyroscope full scale range to +/-500 deg/s
IMU.setGyroRange(MPU9250::GYRO_RANGE_500DPS);
Serial.print(".");
// setting DLPF bandwidth to 20 Hz
IMU.setDlpfBandwidth(MPU9250::DLPF_BANDWIDTH_20HZ);
Serial.print(".");
// setting SRD to 19 for a 50 Hz update rate
IMU.setSrd(19);
// enabling wake on motion low power mode with a threshold of 400 mg and
// an accelerometer data rate of 15.63 Hz.
//IMU.enableWakeOnMotion(400,MPU9250::LP_ACCEL_ODR_15_63HZ);
// attaching the interrupt to microcontroller pin 1
//pinMode(1,INPUT);
//attachInterrupt(1,wakeUp,RISING);
Serial.print(".");
//fona.sendSMS("+49phonenumber", "System up and running");
Serial.println("done.");
//while(1);
}
void loop()
{
String res = "init";
float LocLat = 0;
float LocLng = 0;
float runtime;
float GPStime = 30000;
char smsBuffer[250];
uint16_t smslen;
digitalWrite(8, LOW); // turn the LED off by making the voltage LOW
digitalWrite(9, LOW); // turn the LED off by making the voltage LOW
digitalWrite(10, LOW); // turn the LED off by making the voltage LOW
switch (runmode) {
case 0:
{
Serial.println("Runmode 0");
/*
if (fona.readSMS(1, smsBuffer, 250, &smslen)) {
Serial.print("Len: ");
Serial.println(smslen);
res = "x123";
/*
for ( int i = 0; i < smslen; i++){
Serial.print(smsBuffer[i]);
Serial.print(res.concat(smsBuffer[i]));
}
*/
Serial.println();
Serial.print("Res: 123");
Serial.println(res);
if (res.indexOf("Scharf") > 0)
{
runmode = 1;
Serial.println("Switch to Runmode 1");
}
//fona.deleteSMS(1);
// }
delay(500);
/*
res = ssGSM.readString();
if (res.indexOf("+CMTI:") > 0) //haben wir eine SMS bekommen?
{
Serial.println("incoming SMS");
ssGSM.println("AT+CMGR=1");
res = ssGSM.readString();
Serial.print ("SMS:");
Serial.print(res);
Serial.println (":");
ssGSM.println("AT+CMGD=1,4"); //löschen aller SMS auf der Karte nach dem einlesen
resX = ssGSM.readString();
if (res.indexOf("Scharf") > 0)
{
runmode = 1;
Serial.println("Switch to Runmode 1");
}
}
*/
break;
}
case 1:
{
Serial.println("Runmode 1");
digitalWrite(10, HIGH); // turn the LED on (HIGH is the voltage level)
res = ssGSM.readString();
if (res.indexOf("+CMTI:") > 0) //haben wir eine SMS bekommen?
{
Serial.println("incoming SMS");
ssGSM.println("AT+CMGR=1");
res = ssGSM.readString();
Serial.print ("SMS:");
Serial.print(res);
Serial.println (":");
ssGSM.println("AT+CMGD=1,4"); //löschen aller SMS auf der Karte nach dem einlesen
//resX = ssGSM.readString();
if (res.indexOf("Unscharf") > 0)
{
runmode = 0;
Serial.println("Switch to Runmode 0");
}
}
if (CheckMoving() > 0)
{
runmode = 2;
Serial.println("Switch to Runmode 2");
// GSM aus und GPS an
ssGSM.stopListening();
LocLat = 0;
LocLng = 0;
ssGPS.listen();
rm2timer = millis();
}
break;
}
case 2:
{
Serial.print("Runmode 2");
digitalWrite(9, HIGH); // turn the LED on (HIGH is the voltage level)
while (ssGPS.available())
gps.encode(ssGPS.read());
if (gps.location.isValid())
{
LocLat = gps.location.lat();
LocLng = gps.location.lng();
Serial.print(" - ");
Serial.print(LocLat,6);
Serial.print(" - ");
Serial.print(LocLng,6);
}
Serial.println();
runtime = millis() - rm2timer;
Serial.print("Runtime: ");
Serial.println(runtime,2);
if (runtime > GPStime) //nach 10 Minuten wechseln wir in runmode 3, bis dahin sollten wir eine GPS-Position haben!
{
runmode = 3;
Serial.println("Switch to Runmode 3");
// GPS aus und GSM an
ssGPS.stopListening();
ssGSM.listen();
}
break;
}
case 3:
{
Serial.print("Runmode 3 ");
digitalWrite(8, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100);
// senden der Position über SMS
Serial.print("Sending SMS ");
Serial.print(ssGSM.readString());
updateSerialGSM();
ssGSM.println("AT+CMGS=\"+49phonenumber\"");
Serial.print(ssGSM.readString());
Serial.print(ssGSM.readString());
Serial.print(ssGSM.readString());
updateSerialGSM();
ssGSM.print(LocLat,3);
ssGSM.print(" - ");
ssGSM.print(LocLng,3);
ssGSM.print((char)26);
Serial.print(ssGSM.readString());
updateSerialGSM();
Serial.println("Switch to Runmode 2");
runmode = 2;
// GSM aus und GPS an
ssGSM.stopListening();
ssGPS.listen();
rm2timer = millis();
break;
}
case 99:
{
Serial.println("Runmode 99");
CheckMoving();
res = ssGSM.readString();
Serial.print ("SMS:");
Serial.print(res);
Serial.println (":");
break;
}
}
//updateGyro();
/*
if (millis() - start > 1000)
{
start = millis();
//printDateTime(gps.date, gps.time);
Serial.print(awake);
Serial.println();
awake = 0;
}
*/
delay(200);
}
int CheckMoving()
{
int Moving;
float trigger;
Serial.print("GetGyroDeltas");
Moving = 0;
trigger = 0;
// read the sensor
IMU.readSensor();
if (IMU.getAccelX_mss() > 0.1)
{
trigger = IMU.getAccelX_mss();
Moving = 1;
}
if (IMU.getAccelY_mss() > 0.4)
{
trigger = IMU.getAccelY_mss();
Moving = 2;
}
if (IMU.getAccelZ_mss() > 0.05)
{
trigger = IMU.getAccelZ_mss();
Moving = 3;
}
if (IMU.getMagX_uT() > 0.05)
{
trigger = IMU.getMagX_uT();
Moving = 4;
}
if (IMU.getMagY_uT() > 0.05)
{
trigger = IMU.getMagY_uT();
Moving = 5;
}
if (IMU.getMagZ_uT() > 31)
{
trigger = IMU.getMagZ_uT();
Moving = 6;
}
if (IMU.getGyroX_rads() > 0.05)
{
trigger = IMU.getGyroX_rads();
Moving = 7;
}
if (IMU.getGyroY_rads() > 0.05)
{
trigger = IMU.getGyroY_rads();
Moving = 8;
}
if (IMU.getGyroZ_rads() > 0.05)
{
trigger = IMU.getGyroZ_rads();
Moving = 9;
}
Serial.print("Moving: ");
Serial.print(Moving);
Serial.print(" - ");
Serial.print(trigger, 6);
return Moving;
}
void GSMBufferClear()
{
String trash;
while(ssGSM.available())
{
trash = ssGSM.read();
}
}
void updateGyro()
{
}
/*
void wakeUp() {
awake = 1;
Serial.println("Awake!");
}
*/
void updateSerialGSM()
{
while (Serial.available())
{
ssGSM.write(Serial.read());//Forward what Serial received to Software Serial Port
}
while(ssGSM.available())
{
Serial.write(ssGSM.read());//Forward what Software Serial received to Serial Port
}
}
/*
static void printDateTime(TinyGPSDate &d, TinyGPSTime &t)
{
if (!d.isValid())
{
Serial.print(F("********** "));
}
else
{
char sz[32];
sprintf(sz, "%02d/%02d/%02d ", d.month(), d.day(), d.year());
Serial.print(sz);
}
if (!t.isValid())
{
Serial.print(F("******** "));
}
else
{
char sz[32];
sprintf(sz, "%02d:%02d:%02d ", t.hour(), t.minute(), t.second());
Serial.print(sz);
}
printInt(d.age(), d.isValid(), 5);
}
static void printInt(unsigned long val, bool valid, int len)
{
char sz[32] = "*****************";
if (valid)
sprintf(sz, "%ld", val);
sz[len] = 0;
for (int i=strlen(sz); i<len; ++i)
sz[i] = ' ';
if (len > 0)
sz[len-1] = ' ';
Serial.print(sz);
}
*/