-
Notifications
You must be signed in to change notification settings - Fork 11
/
auto-answer-call.ino
114 lines (95 loc) · 2.72 KB
/
auto-answer-call.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
/*
Featured AT Command:
ATSO=1
Set the phone to automatically answer a phone number.
By Mike Allison
Towers of Power
*/
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3);
boolean stringComplete = false;
String inputString = "";
String fromGSM="";
void setup()
{
Serial.begin(9600);
Serial.println("Serial Connected");
mySerial.begin(9600);
//reserve some buffer space
inputString.reserve(200);
fromGSM.reserve(200);
//Turn off echo from GSM
mySerial.print("ATE0");
mySerial.print("\r");
delay(300);
//set to auto answer after 1 ring, [1-255]
mySerial.print("ATS0=1");
mySerial.print("\r");
delay(300);
}
void sendSMS(String& phone_number, char message[] ){
//Set the module to text mode
mySerial.print("AT+CMGF=1");
mySerial.print("\r");
delay(500);
//Send the following SMS to the following phone number
mySerial.print("AT+CMGS=\"");
// CHANGE THE NUMBER BELOW!
// 129 for domestic #s, 145 if with +area code in front of #
mySerial.print(phone_number+"\",129"); //this is where you would replace the phone number
mySerial.print("\r");
delay(300);
// TYPE THE BODY OF THE TEXT HERE! 160 CHAR MAX!
// mySerial.print("T4D);
mySerial.print(message);
// Special character to tell the module to send the message
mySerial.write(0x1A);
delay(500);
}
void loop() // run over and over
{
//listen from GSM Module
if (mySerial.available()){
char inChar = mySerial.read();
if (inChar == '\n') {
//check the state
if(fromGSM == "OK\r"){
Serial.println("---------IT WORKS-------");
}
else if(fromGSM == "RING\r"){
Serial.println("---------ITS RINGING-------");
}
else if(fromGSM == "ERROR\r"){
Serial.println("---------IT DOESNT WORK-------");
}
else if(fromGSM == "NO CARRIER\r"){
Serial.println("---------CALL ENDED-------");
String myNumber = "XXXXXXXXXX"; // <------ Dont forget to change this!!!
char myMessage[] = "Thanks for Calling, Mike!";
sendSMS(myNumber, myMessage);
}
//write the actual response
Serial.println(fromGSM);
//clear the buffer
fromGSM = "";
}else {
fromGSM+=inChar;
}
delay(20);
}
//only write a full message to the GSM module
if (stringComplete) {
mySerial.print(inputString);
inputString = "";
stringComplete = false;
}
}
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
inputString += inChar;
if (inChar == '\n') {
stringComplete = true;
}
}
}