forked from meirm/SerialGSM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerialGSM.cpp
More file actions
321 lines (261 loc) · 6.62 KB
/
Copy pathSerialGSM.cpp
File metadata and controls
321 lines (261 loc) · 6.62 KB
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
// SerialGSM version 1.1
// by Meir Michanie
// meirm@riunx.com
// error codes
// http://www.developershome.com/sms/resultCodes2.asp
#include <SerialGSM.h>
#include <string.h>
#include <stdlib.h>
SerialGSM::SerialGSM(int rxpin,int txpin):
SoftwareSerial(rxpin,txpin)
{
verbose=false;
lastStatusCode=0;
}
void SerialGSM::FwdSMS2Serial(){
if (verbose) Serial.println("AT+CMGF=1"); // set SMS mode to text
this->println("AT+CMGF=1"); // set SMS mode to text
WaitResp("OK", 200);
this->ReadLine();
if (verbose) Serial.println("AT+CNMI=3,3,0,0"); // set module to send SMS data to serial out upon receipt
this->println("AT+CNMI=3,3,0,0"); // set module to send SMS data to serial out upon receipt
WaitResp("OK", 200);
this->ReadLine();
}
void SerialGSM::SendSMS(char * cellnumber,char * outmsg){
if (strlen(outmsg) > 140){
Serial.println("Error: SMS Message was longer than 140 characters.");
return;
}
this->Rcpt(cellnumber);
if (verbose) Serial.println(recipient);
this->StartSMS();
this->Message(outmsg);
if (verbose) Serial.print(outMessage);
this->print(outMessage);
this->EndSMS();
delay(500);
this->ReadLine();
}
void SerialGSM::SendSMS(){
if (verbose) Serial.println(recipient);
if (verbose) Serial.println(outMessage);
this->StartSMS();
if (verbose) Serial.print(outMessage);
this->print(outMessage);
this->EndSMS();
delay(500);
this->ReadLine();
}
void SerialGSM::DeleteAllSMS(){
if (verbose) Serial.println("AT+CMGD=1,4"); // delete all SMS
this->println("AT+CMGD=1,4"); // delete all SMS
WaitResp("OK", 5000);
}
void SerialGSM::Reset(){
if (verbose) Serial.println("AT+CFUN=0,1"); // Reset Modem, Disable Auto Power Saving
this->println("AT+CFUN=0,1"); // Reset Modem, Disable Auto Power Saving
delay(200);
this->ReadLine();
}
void SerialGSM::EndSMS(){
this->print(char(26)); // ASCII equivalent of Ctrl-Z
if (verbose) Serial.println();
WaitResp("OK", 5000); // the SMS module needs time to return to OK status
}
void SerialGSM::StartSMS(){
if (verbose) Serial.println("AT+CMGF=1"); // set SMS mode to text
this->println("AT+CMGF=1"); // set SMS mode to text
WaitResp("OK", 1000);
if (verbose) Serial.print("AT+CMGS=");
this->print("AT+CMGS=");
this->print(char(34)); // ASCII equivalent of "
if (verbose) Serial.print(recipient);
this->print(recipient);
this->println(char(34)); // ASCII equivalent of "
WaitResp(">", 500); //Modem is ready for the SMS body
delay(500); //Let the module think
}
void SerialGSM::Call(char * cellnumber){
this->Rcpt(cellnumber);
if (verbose) Serial.println(recipient);
if (verbose) Serial.print("ATD");
this->print("ATD");
if (verbose) Serial.print(recipient);
this->print(recipient);
//End Command - ASCII carriage return
this->print(char(13));
if (verbose) Serial.println();
//Let the module process
WaitResp("OK", 3000);
}
void SerialGSM::Hangup(){
if (verbose) Serial.print("ATH");
this->print("ATH");
//End Command - ASCII carriage return
this->print(char(13));
if (verbose) Serial.println();
WaitResp("OK", 1000);
}
int SerialGSM::ReadLine(){
static int pos=0;
char nc;
while (this->available()){
nc=this->read();
if (nc == '\n' or (pos > MAXMSGLEN) or (((unsigned long)(millis() - lastRec) > SERIALTIMEOUT) and (pos > 0)) ){
nc='\0';
lastRec=millis();
inMessage[pos]=nc;
pos=0;
if (verbose) Serial.println(inMessage);
return 1;
}
else if (nc=='\r') {
}
else{
inMessage[pos++]=nc;
lastRec=millis();
}
}
return 0;
}
// GetGSMStatus returns one of the following status codes as reported from the GSM module
// 0 SIM card removed
// 1 SIM card inserted
// 2 Ring melody
// 3 AT module is partially ready
// 4 AT module is totally ready
// 5 ID of released calls
// 6 Released call whose ID=<idx>
// 7 The network service is available for an emergency call
// 8 The network is lost
// 9 Audio ON
// 10 Show the status of each phonebook after init phrase
// 11 Registered to network
int SerialGSM::GetGSMStatus(){
char status[2];
//Parse the Status Code
if (strstr(inMessage, "SIND: ") != NULL){
int pos = 6;
if(strstr(inMessage, "+SIND:")) pos++;
//Read in all numbers
int i = 0;
while(inMessage[pos + i] != NULL and inMessage[pos + i] != ','){
status[i] = inMessage[pos + i];
i++;
}
lastStatusCode = atoi(status);
}
return lastStatusCode;
}
boolean SerialGSM::ErrorOccured(){
if (strstr(inMessage, "ERROR") != NULL){
errorOccured = true;
}
return errorOccured;
}
int SerialGSM::ReceiveSMS(){
static boolean insms=0;
// Get the number of the sms sender in order to be able to reply
if ( strstr(inMessage, "CMT: ") != NULL ){
insms=1;
int sf=6;
if(strstr(inMessage, "+CMT:")) sf++;
for (int i=0;i < PHONESIZE;i++){
senderNumber[i]=inMessage[sf+i];
}
senderNumber[PHONESIZE]='\0';
return 0;
}
else{
if(insms) {
insms=0;
return 1;
}
}
return 0;
}
int SerialGSM::ReceiveCall(){
static boolean inCall = 0;
// Get the caller id
if ( strstr(inMessage, "CLIP: ") != NULL ){
inCall=1;
int sf=6;
if(strstr(inMessage, "+CLIP:")) sf++;
for (int i=0;i < PHONESIZE;i++){
senderNumber[i]=inMessage[sf+i];
}
senderNumber[PHONESIZE]='\0';
return 0;
}
else{
if(inCall) {
inCall=0;
return 1;
}
}
return 0;
}
//Returns true if successful, false if timed out
boolean SerialGSM::WaitResp(char * response, int timeout){
unsigned long waitStart = millis();
boolean responseReceived = false;
//Check for an incoming event
boolean incoming = ReceiveCall() || ReceiveSMS();
while((unsigned long)(millis() - waitStart) < timeout){
ReadLine();
if (!responseReceived){
responseReceived = strstr(inMessage, response) != NULL;
}
//Ensure incoming event is not truncated
if(responseReceived && !incoming){
if (verbose){
Serial.print("GSM Returned: \"");
Serial.print(response);
Serial.println("\"");
}
return true;
}
//Update events
ErrorOccured();
GetGSMStatus();
}
if (verbose){
Serial.print("GSM Timeout waiting for: \"");
Serial.print(response);
Serial.println("\"");
}
return false;
}
boolean SerialGSM::Verbose(){
return verbose;
}
void SerialGSM::Verbose(boolean var1){
verbose=var1;
}
char * SerialGSM::Sender(){
return senderNumber;
}
char * SerialGSM::Rcpt(){
return recipient;
}
char * SerialGSM::Message(){
return inMessage;
}
void SerialGSM::Sender(char * var1){
sprintf(senderNumber,"%s",var1);
}
void SerialGSM::Rcpt(char * var1){
sprintf(recipient,"%s",var1);
}
void SerialGSM::Message(char * var1){
sprintf(outMessage,"%s",var1);
}
void SerialGSM::Boot(){
int counter=0;
while(counter++ < 15){
if (verbose) Serial.print(".");
delay(1000);
}
if (verbose) Serial.println();
}