-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSIM900.cpp
More file actions
346 lines (301 loc) · 8.3 KB
/
SIM900.cpp
File metadata and controls
346 lines (301 loc) · 8.3 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
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
#include "Arduino.h"
#include "SIM900.h"
SIM900::SIM900(SoftwareSerial *ser, uint8_t pwrPin){
simSwSerial = ser;
powerPin = pwrPin;
buffer = new char[BUFFER_LEN];
}
void SIM900::init(char* pinNumber){
pinMode(powerPin, OUTPUT);
simSwSerial->begin(19200);
uint8_t answer=0;
boolean simPinNeeded = false;
//auto-bauding : send "AT"
delay(500);
answer = sendAtCommandAndCheckOK("AT",buffer,1000);
if(answer==2 && strstr(buffer,"RDY")!=NULL) answer = 0;
if(answer == 2){
//timeout
//On démarre le SIM900
Serial.println("power on pulse");
digitalWrite(powerPin,HIGH);
delay(500);
digitalWrite(powerPin,LOW);
delay(2500);
}
// waits for an answer from the module
//Si on est en timeout, AT n'a pas répondu OK. Ca peut etre une RDY
unsigned long baudrates [] = {115200, 57600, 38400, 19200, 9600, 4800, 2400, 1200};
uint8_t idx = 0;
uint8_t retry = 0;
while(answer>0){
// Send AT every two seconds and wait for the answer
answer = sendAtCommandAndCheckOK("AT",buffer,1000);
if(answer==2 && strstr(buffer,"RDY")!=NULL) answer = 0;
if(retry++ > 2 && idx<=8){
Serial.print("Set baudrate=");Serial.println(baudrates[idx]);
simSwSerial->begin(baudrates[idx++]);
retry = 0;
}
}
/*
* ATE0 : command echo off
*/
sendAtCommandAndCheckOK("ATE0",buffer,500);
/*
* set auto baud
*/
//sendAtCommandAndCheckOK("AT+IPR?",buffer,500);
sendAtCommandAndCheckOK("AT+IPR=0",buffer,500);
/*
* Check phone functionnality
*/
answer = sendAtCommandAndCheckOK("AT+CFUN?",buffer,500);
if(answer == 0 && strstr(buffer,"+CFUN: 1")==NULL){
sendAtCommandAndCheckOK("AT+CFUN=1,1",buffer,10000);
}
/*
* Check PIN input need
*/
if(pinNumber != NULL){
answer = sendAtCommandAndCheckOK("AT+CPIN?",buffer,500);
if(answer == 0 && strstr(buffer,"+CPIN: SIM PIN")!=NULL){
sprintf(buffer,"AT+CPIN=%s", pinNumber);
answer = sendAtCommandAndCheckOK(buffer,buffer, 500);
if(answer>0) return;
}
}
/*
* Mode texte
*/
sendAtCommandAndCheckOK("AT+CMGF=1",buffer,500); // sets the SMS mode to text
}
/*
* Envoi une command AT au SIM900, et vérifie que la réponse contient la chaine attendue ()
* Attend jusqu'au timeout.
* Réponse :
* 0:OK - response match
* 1:Empty
* 2:Timeout
* 3:Overflow
* 4:Response does not match
*/
uint8_t SIM900::sendAtCommandAndCheckOK(char* command, char* response, unsigned int timeout){
sendAtCommand(command);
return checkOK(response,timeout);
}
/*
* Envoi une command AT au SIM900
*/
void SIM900::sendAtCommand(char* command){
delay(200);
boolean flag = false;
while(simSwSerial->available()){
char c = simSwSerial->read();
if(!flag){
Serial.print(" SKIPPED : ");
flag = true;
}
Serial.print(c);
}
//if(flag) Serial.println("");
Serial.print("sendAtCommand : "); Serial.println(command);
simSwSerial->println(command);
}
/*
* Attend et vérifie que la réponse contient la chaine attendue
* Attend jusqu'au timeout.
* Réponse :
* 0:OK - response match
* 1:Empty
* 2:Timeout
* 3:Overflow
* 4:ERROR
*/
uint8_t SIM900::checkOK(char* response, unsigned int timeout){
//La réponse commence éventuellement par un "echo" de la requete, puis une ligne vide
//On ignore cette première partie
uint8_t answer = readLine(response,timeout,false);
if(answer==2){
//Serial.println("TIMEOUT sur le première ligne");
return answer;
}
do{
answer = readLine(response, timeout, false);
}
while(answer==1); //Tant que c'est une ligne vide, on passe
char* sub;
while(answer==0 || answer==1){
//On regarde si la réponse se termine par OK
//sub=strstr(response,"\r\nOK");
unsigned int idx = strlen(response)-6;
if(idx>=0 && strcmp(&response[idx],"\r\nOK\r\n") == 0){
//Reponse OK
//On tronque le buffer pour supprimer "OK"
response[idx] = '\0';
break;
}
//On regarde si la réponse commence par OK
if(strcmp("OK",response)<0){
response[0] = '\0';
break;
}
//On regarde si la réponse commence ou termine par "ERROR"
idx = strlen(response)-9;
if(strcmp("ERROR",response)<0 || idx>=0 && strcmp(&response[idx],"\r\nERROR\r\n") == 0){
//Reponse ERROR
answer = 4;
break;
}
//On lit les lignes suivantes tant qu'on n'a pas le réponse escomptée
answer = readLine(response, timeout, true);
}
switch(answer){
case 0: Serial.print(" OK "); break;
case 1: Serial.print(" OK-EMPTY"); break;
case 2: Serial.print(" TIMEOUT "); break;
case 3: Serial.print(" OVERFLOW"); break;
case 4: Serial.print(" ERROR:");break;
}
Serial.print(" (nb chars="); Serial.print(strlen(response)); Serial.println(") : ");
Serial.println(response);
if(answer==4) debugLastError();
return answer;
}
void SIM900::debugLastError(){
sendAtCommandAndCheckOK("AT+CEER=0",buffer,500); //message d'erreur textuel
sendAtCommandAndCheckOK("AT+CEER",buffer,500);
Serial.print(buffer);
}
/*
* Lit une ligne en provenance du SIM900
* Attend jusqu'au timeout.
* Réponse :
* 0:OK
* 1:Empty
* 2:Timeout
* 3:Overflow
*
*/
uint8_t SIM900::readLine(char* response, unsigned int timeout, boolean append){
uint8_t idx=0;
if(append){
idx = strlen(response);
}
else{
memset(response, '\0', BUFFER_LEN); // Initialice the string
}
uint8_t answer=2; //Timeout par défaut
unsigned long previous = millis();
while((millis()-previous)<timeout){
if(simSwSerial->available() != 0){
byte c = simSwSerial->read();
//On vérifie que ça rentre dans le buffer
if(idx+1>=BUFFER_LEN){
Serial.println("ERROR: Buffer overflow");
answer = 3;
break;
}
response[idx] = c;
idx = idx+1;
if(c==10){
//C'est la fin de ligne
//if(idx>0 && response[idx-1]==13) response[idx--] = '\0';
answer = 0;
if(idx==2) answer = 1; //uniquement les caractères CR(13) LF(10)
break;
}
}
}
return answer;
}
/*
* Attend jusqu'au timeout l'invite ">" de la part de la SIM
* Réponse :
* 0:OK
* 2:Timeout
*/
uint8_t SIM900::waitPrompt(unsigned int timeout){
Serial.print("SIM900::WaitPrompt : ");
uint8_t idx=0;
uint8_t answer=2; //Timeout par défaut
unsigned long previous = millis();
while((millis()-previous)<timeout){
if(simSwSerial->available() != 0){
byte c = simSwSerial->read();
if(c==10 || c==13){
//on ignore les caractères CR LF
}
else if(c=='>'){
//on a le prompt
answer = 0;
break;
}
}
}
switch(answer){
case 0: Serial.print("OK"); break;
case 2: Serial.print("TIMEOUT"); break;
}
Serial.println();
return answer;
}
/*
* Envoi d'un SMS
*/
uint8_t SIM900::sendSMS(char* phoneNumber,char* msg){
Serial.println("SIM900::sendSMS");
//On initie l'envoi de SMS avec la commande AT et le n° de tel destinataire
sprintf(buffer,"AT+CMGS=\"%s\"", phoneNumber);
sendAtCommand(buffer);
answer = waitPrompt(2000);
if(answer == 0){
//Envoi du message
Serial.print(">"); Serial.println(msg);
simSwSerial->println(msg);
simSwSerial->write(0x1A);
//On attend la réponse
answer = checkOK(buffer,10000);
}
return answer;
}
/*
* Lecture d'un SMS
* Réponse: le message du SMS, ou NULL si erreur
*/
char* SIM900::readSMS(uint8_t smsIdx, char* num_tel,unsigned int timeout){
Serial.println("SIM900::readSMS");
answer = sendAtCommandAndCheckOK("AT+CMGR=1",buffer,timeout); //Récupère le SMS n°1
if( answer==0 && strcmp("+CMGR:",buffer)<0 ){
//Il y a un SMS
char* tmp = strchr(&buffer[2],10);
char* msg = &tmp[1];
tmp[0] = '\0';
strtok(buffer,"\""); //1° token : +CMGR:
char* stat = strtok(NULL,"\""); //2° token : REC UNREAD ou REC READ
strtok(NULL,"\""); //3° token : ","
char* tel = strtok(NULL,"\""); //4° token : n° de tel
//Serial.print("stat:"); Serial.println(stat);
//Serial.print("tel :"); Serial.println(tel);
if(num_tel != NULL) strcpy(num_tel,tel);
return msg;
}
else{
Serial.print("response:"); Serial.println(answer);
return NULL;
}
}
/*
* Supression d'un SMS
*/
uint8_t SIM900::deleteSMS(uint8_t smsIdx){
return sendAtCommandAndCheckOK("AT+CMGD=1",buffer,500);
}
/*
* Est-ce qu'on est enregistré auprès du réseau?
*/
boolean SIM900::isRegistered(){
answer = sendAtCommandAndCheckOK("AT+CREG?",buffer, 500);
if(answer==0 && ( strstr(buffer,"+CREG: 0,1") != NULL || strstr(buffer,"+CREG: 0,5") != NULL ) ) return true;
else return false;
}