-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGSMSwitch.ino
441 lines (374 loc) · 8.59 KB
/
GSMSwitch.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
int SMS_location_number;
const unsigned int MAX_INPUT = 165; // 160 characters for SMS plus a few extra
static unsigned int input_pos = 0;
char MyMaster[ ] = "+441234567890";
const int RESTART_DELAY = 10000; // stay off for 10sec when restarting
const int MISSED_CALL_DELAY = 10000; // hang up after MISSED_CALL_DELAY seconds
const boolean RELAY_1_DEFAULT = LOW; // set relay level to this state on startup or after the restart. LOW means relay contacts are closed
const boolean RELAY_2_DEFAULT = LOW; // set relay level to this state on startup or after the restart. LOW means relay contacts are closed
const boolean RELAY_3_DEFAULT = LOW; // set relay level to this state on startup or after the restart. LOW means relay contacts are closed
const boolean RELAY_4_DEFAULT = LOW; // set relay level to this state on startup or after the restart. LOW means relay contacts are closed
const boolean TEST_DEFAULT = HIGH; // set relay level to this state on startup or after the restart. LOW means relay contacts are closed
const unsigned int IGT = 2;
const unsigned int RELAY_1 = 3;
const unsigned int RELAY_2 = 4;
const unsigned int RELAY_3 = 5;
const unsigned int RELAY_4 = 6;
const unsigned int TEST = 13;
void setup()
{
pinMode(IGT, OUTPUT);
pinMode(RELAY_1, OUTPUT);
pinMode(RELAY_2, OUTPUT);
pinMode(RELAY_3, OUTPUT);
pinMode(RELAY_4, OUTPUT);
pinMode(TEST, OUTPUT);
initialise();
delay(5000); // Wait to GSM board to start
Serial1.begin(9600); // Starts addtional serial port for GSM
while (!Serial1);
Serial.begin(9600);
// while (!Serial);
delay(1000);
GSM_Startup();
Serial.println("System ready");
delay(1000);
}
void loop()
{
readTC35i();
}
void readTC35i()
{
static char input_line [MAX_INPUT];
if (Serial1.available () > 0)
{
while (Serial1.available () > 0)
{
char inByte = Serial1.read ();
switch (inByte)
{
case '\n': // end of text
input_line [input_pos] = 0; // terminating null byte
// terminator reached! process input_line now
process_data (input_line);
// reset buffer for next time
input_pos = 0;
break;
case '\r': // discard carriage return
break;
default:
// keep adding if not full... allow for terminating null byte
if (input_pos < (MAX_INPUT - 1))
input_line [input_pos++] = inByte;
break;
}
}
}
}
void process_data (char * data)
{
Serial.println(data); // display the data
if(strstr(data, "+CMGR:") && strstr(data, "+44XXXXXXXXXX"))
{
// Only phone number +44XXXXXXXXXX can do whats inside this if
}
if(strstr(data, "smsdelete"))
{
delete_All_SMS();
}
if(strstr(data, "^SMGO: 2"))
{ // SIM card FULL
delete_All_SMS(); // delete all SMS
}
if(strstr(data, "+CMTI:"))
{ // An SMS has arrived
char* copy = data + 12; // Read from position 12 until a non ASCII number to get the SMS location
SMS_location_number = (byte) atoi(copy); // Convert the ASCII number to an int
Serial1.print("AT+CMGR=");
Serial1.println(SMS_location_number);
}
if(strstr(strlwr(data), "ring"))
{
// Incoming call, answer it
Serial.println("Incoming call. Answering it in 1 sec...");
delay(1000);
Serial1.println("ATA");
Serial.println("Incoming call, answered. Hanging up in 5 sec...");
delay(5000);
Serial1.println("ATH");
Serial.println("Incoming call, answered and hung up.");
}
if(strstr(strlwr(data), "test on"))
{
TEST_ON();
delete_one_SMS();
CallMyMaster();
}
if(strstr(strlwr(data), "test off"))
{
TEST_OFF();
delete_one_SMS();
CallMyMaster();
}
if(strstr(strlwr(data), "test restart"))
{
TEST_RESTART();
delete_one_SMS();
CallMyMaster();
}
if(strstr(strlwr(data), "1 on"))
{
RELAY_1_ON();
delete_one_SMS();
CallMyMaster();
}
if(strstr(strlwr(data), "1 off"))
{
RELAY_1_OFF();
delete_one_SMS();
CallMyMaster();
}
if(strstr(strlwr(data), "1 restart"))
{
RELAY_1_RESTART();
delete_one_SMS();
CallMyMaster();
}
if(strstr(strlwr(data), "2 on"))
{
RELAY_2_ON();
delete_one_SMS();
CallMyMaster();
}
if(strstr(strlwr(data), "2 off"))
{
RELAY_2_OFF();
delete_one_SMS();
CallMyMaster();
}
if(strstr(strlwr(data), "2 restart"))
{
RELAY_2_RESTART();
delete_one_SMS();
CallMyMaster();
}
if(strstr(strlwr(data), "3 on"))
{
RELAY_3_ON();
delete_one_SMS();
CallMyMaster();
}
if(strstr(strlwr(data), "3 off"))
{
RELAY_3_OFF();
delete_one_SMS();
CallMyMaster();
}
if(strstr(strlwr(data), "3 restart"))
{
RELAY_3_RESTART();
delete_one_SMS();
CallMyMaster();
}
if(strstr(strlwr(data), "4 on"))
{
RELAY_4_ON();
delete_one_SMS();
CallMyMaster();
}
if(strstr(strlwr(data), "4 off"))
{
RELAY_4_OFF();
delete_one_SMS();
CallMyMaster();
}
if(strstr(strlwr(data), "4 restart"))
{
RELAY_4_RESTART();
delete_one_SMS();
CallMyMaster();
}
if(strstr(strlwr(data), "all on"))
{
ALL_ON();
delete_one_SMS();
CallMyMaster();
}
if(strstr(strlwr(data), "all off"))
{
ALL_OFF();
delete_one_SMS();
CallMyMaster();
}
if(strstr(strlwr(data), "all restart"))
{
ALL_RESTART();
delete_one_SMS();
CallMyMaster();
}
}
void delete_one_SMS()
{
Serial.println("Deleting SMS 1");
Serial1.print("AT+CMGD=");
Serial1.println("1");
delay(1000);
}
void delete_All_SMS()
{
for(int i = 1; i <= 10; i++) {
Serial1.print("AT+CMGD=");
Serial1.println(i);
Serial.write("Deleting SMS ");
Serial.println(i);
delay(500);
}
}
void GSM_Startup()
{
digitalWrite(IGT, LOW);
delay(100);
digitalWrite(IGT, HIGH);
Serial1.print("AT+CMGF=1\r");
delay(100);
Serial1.print("AT+CNMI=2,1,0,0,1\r");
delay(100);
delete_All_SMS();
}
void initialise()
{
digitalWrite(RELAY_1, RELAY_1_DEFAULT);
digitalWrite(RELAY_2, RELAY_2_DEFAULT);
digitalWrite(RELAY_3, RELAY_3_DEFAULT);
digitalWrite(RELAY_4, RELAY_4_DEFAULT);
digitalWrite(TEST, TEST_DEFAULT);
}
void TEST_ON()
{
digitalWrite(TEST, HIGH);
Serial.println("TEST is on");
delay(100);
}
void TEST_OFF()
{
digitalWrite(TEST, LOW);
Serial.println("TEST is off");
delay(100);
}
void TEST_RESTART()
{
digitalWrite(TEST, !TEST_DEFAULT);
delay(RESTART_DELAY);
digitalWrite(TEST, TEST_DEFAULT);
Serial.println("TEST restarted");
}
void RELAY_1_ON()
{
digitalWrite(RELAY_1, LOW);
Serial.println("RELAY_1 is on");
delay(100);
}
void RELAY_1_OFF()
{
digitalWrite(RELAY_1, HIGH);
Serial.println("RELAY_1 is off");
delay(100);
}
void RELAY_1_RESTART()
{
digitalWrite(RELAY_1, !RELAY_1_DEFAULT);
delay(RESTART_DELAY);
digitalWrite(RELAY_1, RELAY_1_DEFAULT);
Serial.println("RELAY_1 restarted");
}
void RELAY_2_ON()
{
digitalWrite(RELAY_2, LOW);
Serial.println("RELAY_2 is on");
delay(100);
}
void RELAY_2_OFF()
{
digitalWrite(RELAY_2, HIGH);
Serial.println("RELAY_2 is off");
delay(100);
}
void RELAY_2_RESTART()
{
digitalWrite(RELAY_2, !RELAY_2_DEFAULT);
delay(RESTART_DELAY);
digitalWrite(RELAY_2, RELAY_2_DEFAULT);
Serial.println("RELAY_2 restarted");
}
void RELAY_3_ON()
{
digitalWrite(RELAY_3, LOW);
Serial.println("RELAY_3 is on");
delay(100);
}
void RELAY_3_OFF()
{
digitalWrite(RELAY_3, HIGH);
Serial.println("RELAY_3 is off");
delay(100);
}
void RELAY_3_RESTART()
{
digitalWrite(RELAY_3, !RELAY_3_DEFAULT);
delay(RESTART_DELAY);
digitalWrite(RELAY_3, RELAY_3_DEFAULT);
Serial.println("RELAY_3 restarted");
}
void RELAY_4_ON()
{
digitalWrite(RELAY_4, LOW);
Serial.println("RELAY_4 is on");
delay(100);
}
void RELAY_4_OFF()
{
digitalWrite(RELAY_4, HIGH);
Serial.println("RELAY_4 is off");
delay(100);
}
void RELAY_4_RESTART()
{
digitalWrite(RELAY_4, !RELAY_4_DEFAULT);
delay(RESTART_DELAY);
digitalWrite(RELAY_4, RELAY_4_DEFAULT);
Serial.println("RELAY_4 restarted");
}
void ALL_ON()
{
RELAY_1_ON();
RELAY_2_ON();
RELAY_3_ON();
RELAY_4_ON();
}
void ALL_OFF()
{
RELAY_1_OFF();
RELAY_2_OFF();
RELAY_3_OFF();
RELAY_4_OFF();
}
void ALL_RESTART()
{
RELAY_1_RESTART();
RELAY_2_RESTART();
RELAY_3_RESTART();
RELAY_4_RESTART();
}
void CallMyMaster()
{
Serial.println("Calling My Master.");
Serial1.print("ATDT"); // Call
Serial1.print(MyMaster); // This number
Serial1.println(";"); // Do it!
Serial.println("Dialling...");
delay(MISSED_CALL_DELAY);
Serial.println("Hunging up...");
Serial1.println("ATH"); // end call
Serial.println("Hung up!");
}