-
Notifications
You must be signed in to change notification settings - Fork 0
/
gsm_start.ino
411 lines (362 loc) · 10.1 KB
/
gsm_start.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
/***************************************************
*/
#include <Adafruit_FONA.h>
#include <Adafruit_SleepyDog.h>
#include <avr/sleep.h>
#include <EEPROM.h>
//#define SLEEP_ENABLED // controls whether we have a watchdog or not
// enables all debug prints. this gets screwy with USB Serial so tied
// directly to power savings enterSleep() command
//#define DEBUG
// debug prints
#ifdef DEBUG
#undef DEBUG_PRINT // fix shadowing DEFs
#undef DEBUG_PRINTLN // fix shadowing DEFs
#define DEBUG_PRINT(x) Serial.print(x)
#define DEBUG_PRINTLN(x) Serial.println(x)
#else
#undef DEBUG_PRINT // fix shadowing DEFs
#undef DEBUG_PRINTLN // fix shadowing DEFs
#define DEBUG_PRINT(x)
#define DEBUG_PRINTLN(x)
#endif
// key fob pin definitions
#define FOB_LOCK 12
#define FOB_UNLOCK 13
#define FOB_START 14
#define FOB_ALARM 15
#define FOB_TRUNK 16
// gsm module pin definitions
#define FONA_RST 3
#define FONA_PS 4
#define FONA_KEY 5
#define FONA_RI 6
#define FONA_TX 7
#define FONA_RX 8
#define FONA_NS 9
// unused pins array - used to save some power
unsigned int unused[] = {0, 1, 2, 10, 11, 17, 18, 19, 20, 21, 22, 23};
// eeprom constants
const int EEPROM_MIN_ADDR = 0;
const int EEPROM_MAX_ADDR = 511;
// this is a large buffer for replies
char replybuffer[255];
// Hardware serial is also possible!
HardwareSerial *fonaSerial = &Serial1;
Adafruit_FONA fona = Adafruit_FONA(FONA_RST);
volatile bool gotSMS = false;
void setup() {
// determine last reset source
boolean resetByWatchdog = bit_is_set(MCUSR, WDRF);
#ifndef SLEEP_ENABLED
Watchdog.enable(8000);
#endif
#ifdef DEBUG
Serial.begin(115200);
#endif
DEBUG_PRINT(F("Reset by Watchdog?: ")); DEBUG_PRINTLN(resetByWatchdog);
DEBUG_PRINTLN(F("GSM Start Module"));
DEBUG_PRINTLN(F("Initializing FONA....(May take 3 seconds)"));
#ifndef SLEEP_ENABLED
Watchdog.reset();
#endif
// pin config for unused pins
for(unsigned int i = 0; i < sizeof(unused)/sizeof(unsigned int); i++) {
pinMode(i, OUTPUT);
digitalWrite(i, LOW);
}
#ifndef SLEEP_ENABLED
Watchdog.reset();
#endif
// pin config for Key Fob
digitalWrite(FOB_LOCK, HIGH);
pinMode(FOB_LOCK, OUTPUT);
digitalWrite(FOB_UNLOCK, HIGH);
pinMode(FOB_UNLOCK, OUTPUT);
digitalWrite(FOB_START, HIGH);
pinMode(FOB_START, OUTPUT);
digitalWrite(FOB_ALARM, HIGH);
pinMode(FOB_ALARM, OUTPUT);
digitalWrite(FOB_TRUNK, HIGH);
pinMode(FOB_TRUNK, OUTPUT);
#ifndef SLEEP_ENABLED
Watchdog.reset();
#endif
// pin config for fona
// FONA_RST handled by Fona library
pinMode(FONA_PS, INPUT);
digitalWrite(FONA_KEY, HIGH);
pinMode(FONA_KEY, OUTPUT);
pinMode(FONA_RI, INPUT);
// FONA_TX -> Hardware Serial RX
// FONA_RX -> Hardware Serial TX
pinMode(FONA_NS, INPUT);
#ifndef SLEEP_ENABLED
Watchdog.reset();
#endif
// power off the Fona if we took a hit from Watchdog
// will get powered on by next block
if (resetByWatchdog) {
DEBUG_PRINTLN(F("Powering off Fona"));
digitalWrite(FONA_KEY, LOW);
delay(2000);
digitalWrite(FONA_KEY, HIGH);
}
#ifndef SLEEP_ENABLED
Watchdog.reset();
#endif
// power on the Fona if it is off
if (!digitalRead(FONA_PS)) {
DEBUG_PRINTLN(F("Powering on Fona"));
digitalWrite(FONA_KEY, LOW);
delay(2000);
digitalWrite(FONA_KEY, HIGH);
}
#ifndef SLEEP_ENABLED
Watchdog.reset();
#endif
// make it slow so its easy to read!
fonaSerial->begin(4800);
if (! fona.begin(*fonaSerial)) {
DEBUG_PRINTLN(F("Couldn't find FONA"));
while (1);
}
DEBUG_PRINTLN(F("FONA is OK"));
#ifndef SLEEP_ENABLED
Watchdog.reset();
#endif
// Print SIM card IMEI number.
char imei[15] = {0}; // MUST use a 16 character buffer for IMEI!
uint8_t imeiLen = fona.getIMEI(imei);
if (imeiLen > 0) {
DEBUG_PRINT("SIM card IMEI: "); DEBUG_PRINTLN(imei);
}
#ifndef SLEEP_ENABLED
Watchdog.reset();
#endif
fona.setSMSInterrupt(1);
attachInterrupt(digitalPinToInterrupt(FONA_RI), message, LOW);
DEBUG_PRINTLN(F("FONA Ready, Arduino Ready"));
#ifndef SLEEP_ENABLED
Watchdog.reset();
#endif
DEBUG_PRINTLN(F("Clearing all SMS Messages"));
clearSMS();
}
void loop() {
if (gotSMS) {
delay(5000);
handleSMS();
} else {
#ifdef SLEEP_ENABLED
#ifndef DEBUG
enterSleep();
#endif
#else
delay(1000);
Watchdog.reset();
#endif
}
}
void message() {
gotSMS = 1;
}
void handleSMS() {
gotSMS = 0;
boolean forceReset = false;
uint16_t smslen;
char password[20];
int8_t smsnum = fona.getNumSMS();
char callerIDbuffer[32]; //we'll store the SMS sender number here
DEBUG_PRINT(F("Num SMS: "));
DEBUG_PRINTLN(smsnum);
#ifndef SLEEP_ENABLED
Watchdog.reset();
#endif
for (int i = 1; i < smsnum + 1; i++) {
#ifndef SLEEP_ENABLED
Watchdog.reset();
#endif
// Retrieve SMS sender address/phone number.
if (! fona.getSMSSender(i, callerIDbuffer, 250)) {
DEBUG_PRINTLN(F("Didn't find SMS message in slot!"));
}
DEBUG_PRINT(F("FROM: ")); DEBUG_PRINTLN(callerIDbuffer);
#ifndef SLEEP_ENABLED
Watchdog.reset();
#endif
if (! fona.readSMS(i, replybuffer, 250, &smslen)) {
DEBUG_PRINTLN(F("Failed to read SMS!"));
}
#ifndef SLEEP_ENABLED
Watchdog.reset();
#endif
eepromReadPass(0, password, sizeof(password));
char* recText = strtok(replybuffer, ":");
if ((recText != NULL) && !strcmp(recText, password)) {
recText = strtok(NULL, "");
if ((recText != NULL) && !strcmp(recText, "start")) {
// successful command
DEBUG_PRINTLN(F("Starting Car..."));
startCar();
} else if ((recText != NULL) && !strcmp(recText, "unlock")) {
DEBUG_PRINTLN(F("Unlocking Car..."));
unlockCar();
} else if ((recText != NULL) && !strcmp(recText, "lock")) {
DEBUG_PRINTLN(F("Locking Car..."));
lockCar();
} else if ((recText != NULL) && !strcmp(recText, "alarm")) {
DEBUG_PRINTLN(F("Triggering Alarm..."));
alarmCar();
} else if ((recText != NULL) && !strcmp(recText, "trunk")) {
DEBUG_PRINTLN(F("Opening Trunk..."));
openTrunk();
} else if ((recText != NULL) && !strcmp(recText, "reset")) {
forceReset = true;
} else {
DEBUG_PRINTLN(F("Invalid Command..."));
}
} else {
DEBUG_PRINTLN(F("Bad Password or spam..."));
}
#ifndef SLEEP_ENABLED
Watchdog.reset();
#endif
// delete the original msg after it is processed
// otherwise, we will fill up all the slots
// and then we won't be able to receive SMS anymore
if (fona.deleteSMS(i)) {
DEBUG_PRINTLN(F("REMOVED SMS!"));
} else {
DEBUG_PRINTLN(F("Couldn't delete"));
}
#ifndef SLEEP_ENABLED
Watchdog.reset();
#endif
if(forceReset) {
DEBUG_PRINTLN(F("Forcing a Watchdog Reset..."));
while(1);
}
#ifndef SLEEP_ENABLED
Watchdog.reset();
#endif
}
}
void clearSMS() {
#define SLOT_MAX 10
#ifdef DEBUG
int8_t smsnum = fona.getNumSMS();
DEBUG_PRINT(F("Num SMS: "));
DEBUG_PRINTLN(smsnum);
#endif
for (int i = 1; i < SLOT_MAX + 1; i++) {
#ifndef SLEEP_ENABLED
Watchdog.reset();
#endif
if (fona.deleteSMS(i)) {
DEBUG_PRINTLN(F("REMOVED SMS!"));
} else {
DEBUG_PRINTLN(F("Couldn't delete"));
}
#ifndef SLEEP_ENABLED
Watchdog.reset();
#endif
}
}
void enterSleep() {
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sleep_mode();
sleep_disable();
}
boolean eepromAddrOk(int addr) {
return ((addr >= EEPROM_MIN_ADDR) && (addr <= EEPROM_MAX_ADDR));
}
boolean eepromWriteBytes(int startAddr, const byte* array, int numBytes) {
// counter
int i;
// both first byte and last byte addresses must fall within
// the allowed range
if (!eepromAddrOk(startAddr) || !eepromAddrOk(startAddr + numBytes)) {
return false;
}
for (i = 0; i < numBytes; i++) {
EEPROM.write(startAddr + i, array[i]);
}
return true;
}
boolean eepromWritePass(int addr, const char* string) {
int numBytes; // actual number of bytes to be written
//write the string contents plus the string terminator byte (0x00)
numBytes = strlen(string) + 1;
return eepromWriteBytes(addr, (const byte*)string, numBytes);
}
boolean eepromReadPass(int addr, char* buffer, int bufSize) {
byte ch; // byte read from eeprom
int bytesRead; // number of bytes read so far
if (!eepromAddrOk(addr)) { // check start address
return false;
}
if (bufSize == 0) { // how can we store bytes in an empty buffer ?
return false;
}
// is there is room for the string terminator only, no reason to go further
if (bufSize == 1) {
buffer[0] = 0;
return true;
}
bytesRead = 0; // initialize byte counter
ch = EEPROM.read(addr + bytesRead); // read next byte from eeprom
buffer[bytesRead] = ch; // store it into the user buffer
bytesRead++; // increment byte counter
// stop conditions:
// - the character just read is the string terminator one (0x00)
// - we have filled the user buffer
// - we have reached the last eeprom address
while ( (ch != 0x00) && (bytesRead < bufSize) && ((addr + bytesRead) <= EEPROM_MAX_ADDR) ) {
// if no stop condition is met, read the next byte from eeprom
ch = EEPROM.read(addr + bytesRead);
buffer[bytesRead] = ch; // store it into the user buffer
bytesRead++; // increment byte counter
}
// make sure the user buffer has a string terminator, (0x00) as its last byte
if ((ch != 0x00) && (bytesRead >= 1)) {
buffer[bytesRead - 1] = 0;
}
return true;
}
void pressFobButton(int pin, int onTime, int offTime) {
digitalWrite(pin, LOW);
delay(onTime);
digitalWrite(pin, HIGH);
delay(offTime);
}
boolean startCar() {
pressFobButton(FOB_LOCK, 200, 200);
pressFobButton(FOB_LOCK, 200, 200);
pressFobButton(FOB_START, 2000, 0);
//check some pin
return true;
}
boolean unlockCar() {
pressFobButton(FOB_UNLOCK, 200, 200);
pressFobButton(FOB_UNLOCK, 200, 0);
//check some pin
return true;
}
boolean lockCar() {
pressFobButton(FOB_LOCK, 200, 200);
pressFobButton(FOB_LOCK, 200, 0);
//check some pin
return true;
}
boolean alarmCar() {
pressFobButton(FOB_ALARM, 200, 200);
//check some pin
return true;
}
boolean openTrunk() {
pressFobButton(FOB_TRUNK, 2000, 0);
//check some pin
return true;
}