-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCall_SMS.ino
230 lines (197 loc) · 5.77 KB
/
Call_SMS.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
#include "SIM900.h"
#include <SoftwareSerial.h>
#include "call.h"
#include "sms.h"
#define ACTIVE LOW//for buttons
const int ledPinC = 13; //number of the LED pin
const int ledPinS = 12;
const int buttonPinC = 7; //number of the Button pin
const int buttonPinS = 4;
//Call
CallGSM call;
boolean started=false;
int buttonStateC = 1;
boolean calling = false;
boolean caller = false;
//SMS
SMSGSM sms;
int buttonStateS = 1;
int numdata;
char smsbuffer[160];
char n[20];
char sms_position;
char phone_number[20];
char sms_text[100];
int i;
boolean listener=false;
boolean sender=false;
void setup(){
Serial.begin(9600);
pinMode(ledPinC,OUTPUT);
pinMode(ledPinS,OUTPUT);
pinMode(buttonPinC, INPUT);
pinMode(buttonPinS, INPUT);
//when a button is pressed voltage goes to ground (Input reads LOW)
digitalWrite(buttonPinC,HIGH);
digitalWrite(buttonPinS,HIGH);
if (gsm.begin(9600))
{
Serial.println("\nstatus=READY");
started=true;
blink(4,'b');
}
else{
Serial.println("\nstatus=IDLE");
}
//cleaning the memory of the shield so that it has room to show new sms messages
sms_position=sms.IsSMSPresent(SMS_READ);
sms.DeleteSMS(sms_position);
}
void loop(){
checkButtons();
//----------------------
//listen for sms and incoming calls
//when both buttons are pressed at the same time
if(buttonStateC == ACTIVE && buttonStateS == ACTIVE){
delay(500);
checkButtons();
if(buttonStateC == ACTIVE && buttonStateS == ACTIVE){
blink(10,'b');
if(!listener){
Serial.println("Listener Mode...");
}else if(listener){
Serial.println("Exiting Listener Mode...");
}
listener = !listener; //switch
}
}
//call prefixed phone number
//when the yellow button is pressed
if(buttonStateC == ACTIVE && buttonStateS != ACTIVE){
delay(400);
checkButtons();
if(buttonStateC == ACTIVE && buttonStateS !=ACTIVE){
if(!calling){
Serial.println("Caller Mode...");
}else if(calling){
Serial.println("Exiting Caller Mode...");
}
caller = true;
}
}
//send sms to a prefixed number
//when the red button is pressed.
if(buttonStateS == ACTIVE && buttonStateC != ACTIVE){
delay(400);
checkButtons();
if(buttonStateS == ACTIVE && buttonStateC == HIGH){
Serial.println("SMS Mode...");
sender = true;
}
}
//-------------------------
/////////
if (caller && started) {//call
/*3 cases for yellow button
CALL_NONE - no call activity
CALL_INCOM_VOICE - incoming voice
CALL_ACTIVE_VOICE - active voice
CALL_NO_RESPONSE - no response to the AT command
CALL_COMM_LINE_BUSY - comm line is not free*/
if(calling)
{
digitalWrite(ledPinC,LOW);
calling = false;
Serial.println("\nHanging up...\n");
call.HangUp();
delay(1000);
}else if(calling==false && call.CallStatus() == CALL_NONE)
{
calling = true;
digitalWrite(ledPinC, HIGH);
delay(1000);
Serial.println("\nCalling...\n");
call.Call("INSERT_PHONE_NUMBER_HERE");
}else if(calling==false && call.CallStatus() == CALL_INCOM_VOICE){
calling = true;
digitalWrite(ledPinC, HIGH);
delay(1000);
Serial.println("\nSpeaking...\n");
call.PickUp();
}
caller=false;
}
//////////
if (sender && started) { //sms
digitalWrite(ledPinS, HIGH);
delay(3000);
if (sms.SendSMS("INSERT_PHONE_NUMBER_HERE", "Arduino Phone SMS Message")){
blink(3,'r');
Serial.println("\nSMS sent OK.");
}
else
{
digitalWrite(ledPinS, LOW);
Serial.println("\nError sending SMS.");
}
sender=false;
}
///////////
if(listener && started){ //listen
//Read if there are messages on SIM card and print them.
sms_position=sms.IsSMSPresent(SMS_UNREAD);
if (sms_position) {
// read new SMS
Serial.print("SMS postion:");
Serial.println(sms_position,DEC);
sms.GetSMS(sms_position, phone_number, sms_text, 100);
// now we have phone number string in phone_num
Serial.println(phone_number);
// and SMS text in sms_text
Serial.println(sms_text);
}
else{
Serial.println("NO NEW SMS,WAITTING");
}
//check for incoming calls
if(call.CallStatus() == CALL_INCOM_VOICE){
Serial.println("INCOMING CALL!");
blink(12,'b');
}else{
Serial.println("NO NEW CALL, WAITING");
}
delay(2000);
}
}
//blink function
void blink(int rep,char x){//blinks LEDS
if(x=='y'){//blink yellow
for(int i=0;i<rep;i++){
digitalWrite(ledPinC, HIGH);
delay(100);
digitalWrite(ledPinC, LOW);
delay(100);
}
}else if(x=='r'){//blink red
for(int i=0;i<rep;i++){
digitalWrite(ledPinS, HIGH);
delay(100);
digitalWrite(ledPinS, LOW);
delay(100);
}
}else{//blink both
for(int i=0;i<rep;i++){
digitalWrite(ledPinC, HIGH);
digitalWrite(ledPinS, HIGH);
delay(100);
digitalWrite(ledPinC, LOW);
digitalWrite(ledPinS, LOW);
delay(100);
}
}
}
//refreshes button states
void checkButtons(){
buttonStateC = digitalRead(buttonPinC);
buttonStateS = digitalRead(buttonPinS);
}