-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSIM900.cpp
557 lines (426 loc) · 11.5 KB
/
SIM900.cpp
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/** SIM900 Basic communication library for SIM900 gsm module.
*
* This library provide easy way to make GET and POST petitions to a web server. Use to interactue Arduino with restful apis, webs, etc.
*
* Released under MIT license.
*
* @author Miquel Vento (http://www.emevento.com)
* @version 1.0 24/03/2015
*
*/
#include "SIM900.h"
Connection::Connection( const char * pinCode,
const char * apnName,
const char * apnUser,
const char * apnPass,
const uint8_t & enablePin,
HardwareSerial & serialPort,
uint32_t baudRate ):
_pinCode( pinCode ),
_apnName( apnName ),
_apnUser( apnUser ),
_apnPass( apnPass ),
_enablePin( enablePin ),
CREG_WAITING_RETRIES( 80 ),
SAPBR_WAITING_RETRIES( 5 ),
CGATT_WAITING_RETRIES( 30 ),
MAX_ATRESPONSE(160)
{
pinMode( enablePin, OUTPUT );
sim900Serial = &serialPort;
sim900Serial->begin( baudRate );
}
bool Connection::Configuration()
{
if ( !sim900Serial )
return false;
if ( !IsPowered() )
PowerOn();
if ( !AT_CPIN() )
return false;
if ( !AT_CREG() )
return false;
if ( !OpenBearer() )
return false;
return true;
}
bool Connection::Get( const char * host,
const char * path,
const char * url,
uint16_t & headerHttpReply,
char *& bodyReply,
const int port )
{
if ( !sim900Serial )
return false;
if ( !IsBearerOpen() )
if ( !Configuration() )
return false;
if ( SendATcommand( "AT+HTTPINIT", "OK", "ERROR", 3000) != 1 )
{
SendATcommand( "AT+HTTPTERM", "OK", 1000 );
if ( SendATcommand( "AT+HTTPINIT", "OK", "ERROR", 3000) != 1 )
return false;
}
if ( SendATcommand("AT+HTTPPARA=\"CID\",1", "OK", 5000) != 1 )
return false;
if ( AT_HTTPPARA_URL( host, path, url ) )
{
if ( AT_HTTPACTION_GET( headerHttpReply ) && headerHttpReply > 0 && headerHttpReply < 600 )
{
delay(1000);
if ( SendATcommand( "AT+HTTPREAD=0,100", "+HTTPREAD:", "ERROR", 30000 ) == 1 )
{
ReceiveData( bodyReply, 15000 );
SendATcommand( "AT+HTTPTERM", "OK", 500 );
return true;
}
}
}
return false;
}
bool Connection::Post( const char * host,
const char * path,
const char * url,
const char * data,
uint16_t & headerHttpReply,
const int port )
{
if ( !sim900Serial )
return false;
if ( !OpenBearer() )
if ( !Configuration() )
return false;
if ( SendATcommand( "AT+HTTPINIT", "OK", "ERROR", 3000) != 1 )
{
SendATcommand( "AT+HTTPTERM", "OK", 1000 );
if ( SendATcommand( "AT+HTTPINIT", "OK", "ERROR", 3000) != 1 )
return false;
}
if ( SendATcommand("AT+HTTPPARA=\"CID\",1", "OK", 5000) != 1 )
return false;
if ( AT_HTTPDATA(strlen(data), 20000 ) )
{
if ( SendATcommand( data, "OK", 10000 ) )
{
if ( AT_HTTPPARA_URL( host, path, url ) )
{
if ( AT_HTTPACTION_POST( headerHttpReply ) )
{
SendATcommand( "AT+HTTPTERM", "OK", 500 );
return true;
}
}
}
}
return false;
}
void Connection::PowerOn()
{
digitalWrite( _enablePin, HIGH );
delay( 1200 );
digitalWrite( _enablePin, LOW );
// Is necessary to wait because SIM900 sends data garbage through Serial.
delay( 2000 );
}
void Connection::PowerOff()
{
SendATcommand("ATE0", "", 1000 );
}
bool Connection::IsPowered()
{
if ( SendATcommand("AT", "OK", 200 ) > 0 )
return true;
else
return false;
}
void Connection::EchoOn( )
{
SendATcommand("ATE1", "OK", 1000 );
}
void Connection::EchoOff( )
{
sim900Serial->println( "ATE0" );
}
bool Connection::AT_CPIN()
{
int answer = SendATcommand( "AT+CPIN?", "READY", "SIM PIN", 500 );
if ( answer == 1 )
{
return true;
}
else if ( answer == 2 )
{
CleanSerialBuffer();
sim900Serial->print("AT+CPIN=\"");
sim900Serial->print( _pinCode );
sim900Serial->println( "\"" );
}
return ReceiveATReply( "OK", 2000 );
}
bool Connection::AT_CREG()
{
int errorCount;
const int totalAnswers = 5;
const char * expectedAnswers[totalAnswers] = { "+CREG: 0,0", "+CREG: 0,2", "+CREG: 0,4", "+CREG: 0,1","+CREG: 0,5" };
for ( errorCount = CREG_WAITING_RETRIES; errorCount ; errorCount-- )
{
int answer = SendATcommand( "AT+CREG?", expectedAnswers, totalAnswers, 2000 );
if ( answer >= 4 )
return true;
else if ( answer == 0 )
return false;
delay( 1000 );
}
return false;
}
bool Connection::IsBearerOpen()
{
if ( SendATcommand( "AT+SAPBR=2,1", "1,1", "1,3", 2000 ) == 1 )
return true;
return false;
}
bool Connection::OpenBearer()
{
uint8_t errorCount;
if ( IsBearerOpen() )
return true;
// Waiting for GPRS Attachment
for ( unsigned int i = CGATT_WAITING_RETRIES; i > 0; i-- )
{
if ( SendATcommand("AT+CGATT?", ": 0", ": 1", 1000 ) == 2 )
break;
delay( 1000 );
}
// Open Bearer
if ( SendATcommand( "AT+SAPBR=1,1", "OK", "ERROR", 3000 ) == 2 )
return false;
// Wait for open the bearer
for ( errorCount = SAPBR_WAITING_RETRIES; errorCount > 0; errorCount-- )
{
if ( SendATcommand( "AT+SAPBR=2,1", "1,1", "1,3", 2000 ) == 1 )
break;
delay( 1000 );
}
if ( !errorCount )
return false;
else
return true;
}
bool Connection::UpdateBearerInfo()
{
// GPRS connection
SendATcommand( "AT+SAPBR=3,1,\"Contype\",\"GPRS\"", "OK", 500 );
// Set APN Name
CleanSerialBuffer();
sim900Serial->print( "AT+SAPBR=3,1,\"APN\",\"" );
sim900Serial->print( _apnName );
sim900Serial->println( "\"" );
if ( !ReceiveATReply( "OK", 500 ) )
return false;
// Set APN User
CleanSerialBuffer();
sim900Serial->print( "AT+SAPBR=3,1,\"USER\",\"" );
sim900Serial->print( _apnUser );
sim900Serial->println( "\"" );
if ( !ReceiveATReply( "OK", 500 ) )
return false;
// Set APN Passwd
CleanSerialBuffer();
sim900Serial->print( "AT+SAPBR=3,1,\"PWD\",\"" );
sim900Serial->print( _apnPass );
sim900Serial->println( "\"" );
if ( !ReceiveATReply( "OK", 500 ) )
return false;
// Save APN Info in NVRAM
return SendATcommand( "AT+SAPBR=5,1", "OK", 500 );
}
bool Connection::AT_HTTPACTION_GET( uint16_t & httpHeader )
{
sim900Serial->println("AT+HTTPACTION=0");
return GetHttpHeader( "+HTTPACTION:0,", httpHeader, 10000 );
}
bool Connection::AT_HTTPACTION_POST( uint16_t & httpHeader )
{
sim900Serial->println("AT+HTTPACTION=1");
return GetHttpHeader( "+HTTPACTION:1,", httpHeader, 10000 );
}
bool Connection::AT_HTTPDATA( const int & size,
const int & timeout )
{
CleanSerialBuffer();
sim900Serial->print( "AT+HTTPDATA=");
sim900Serial->print( size+2 );
sim900Serial->print( "," );
sim900Serial->println( timeout );
return ReceiveATReply( "DOWNLOAD", 10000 );
}
bool Connection::AT_HTTPPARA_URL( const char * host,
const char * path,
const char * url )
{
CleanSerialBuffer();
sim900Serial->print( "AT+HTTPPARA=\"URL\",\"" );
sim900Serial->print( host );
sim900Serial->print( "/" );
sim900Serial->print( path );
sim900Serial->print( "/" );
sim900Serial->print( url );
sim900Serial->println( "\"" );
return ReceiveATReply( "OK", 10000 );
}
bool Connection::GetHttpHeader( const char * expected_answer,
uint16_t & httpHeader,
const uint16_t & timeout )
{
uint32_t previousTime;
char nextChar;
httpHeader = 0;
// this loop waits for the answer
previousTime = millis();
if ( !ReceiveATReply( expected_answer, timeout ) )
return false;
for( int i = 3; i > 0; i-- )
{
if ( !WaitingSerialAvailable( previousTime,timeout ) )
return false;
nextChar = sim900Serial->read();
// Convert char number to integer
httpHeader *= 10;
httpHeader += ( nextChar-0x30 ); // Converts ASCII character to integer
}
return true;
}
bool Connection::TimeOut( const uint32_t & previousTime,
const uint16_t & timeOut )
{
if ( millis() - previousTime > timeOut )
return true;
return false;
}
uint16_t Connection::GetReceiveDataSize( const uint16_t & timeout )
{
uint16_t dataSize = 0;
uint32_t previousTime = millis();
char nextChar;
if ( !WaitingSerialAvailable( previousTime, timeout ) )
{
return false;
}
nextChar = sim900Serial->read();
// Get Data Size
do
{
// Convert char number to integer
dataSize *= 10;
dataSize += ( nextChar-0x30 ); // Converts ASCII character to integer
if ( !WaitingSerialAvailable( previousTime, timeout ) )
return false;
nextChar = sim900Serial->read();
} while( nextChar != 0x0D );
// Consume TR character
if ( sim900Serial->available() )
sim900Serial->read();
return dataSize;
}
bool Connection::WaitingSerialAvailable( const uint32_t & previousTime,
const uint16_t & timeout )
{
while( !sim900Serial->available() )
{
if ( TimeOut( previousTime, timeout ) )
return false;
}
return true;
}
bool Connection::ReceiveData( char *& bodyReply,
const uint16_t & timeout )
{
char nextChar;
uint8_t answerCount = 0;
uint16_t dataSize = 0;
uint32_t previousTime;
previousTime = millis();
dataSize = GetReceiveDataSize( 500 );
bodyReply = new char[dataSize+1];
if ( dataSize > 0)
{
for ( int i = dataSize; i > 0; i-- )
{
if ( WaitingSerialAvailable( previousTime, timeout ) )
bodyReply[answerCount++] = sim900Serial->read();
else
return false;
}
bodyReply[answerCount] = '\0';
return true;
}
else
{
return false;
}
}
void Connection::CleanSerialBuffer()
{
while( sim900Serial->available() > 0)
{
sim900Serial->read();
}
}
int8_t Connection::SendATcommand( const char* ATcommand,
const char* expectedAnswer,
const unsigned int & timeout )
{
const char * arrayAnswers[] = { expectedAnswer };
return SendATcommand( ATcommand, arrayAnswers, 1, timeout );
}
int8_t Connection::SendATcommand( const char* ATcommand,
const char* expectedAnswer1,
const char* expectedAnswer2,
const unsigned int & timeout )
{
const char * arrayAnswers[] = { expectedAnswer1, expectedAnswer2 };
return SendATcommand( ATcommand, arrayAnswers, 2, timeout );
}
int8_t Connection::SendATcommand( const char* ATcommand,
const char ** expectedAnswers,
const int & totalAnwers,
const unsigned int & timeout )
{
CleanSerialBuffer();
sim900Serial->println( ATcommand );
return ReceiveATReply( expectedAnswers, totalAnwers, timeout );
}
int8_t Connection::ReceiveATReply( const char ** expectedAnswers,
const int & totalAnwers,
const unsigned int & timeout )
{
uint8_t answerCount = 0;
char response[MAX_ATRESPONSE];
uint32_t previousTime;
memset( response, '\0', MAX_ATRESPONSE );
delay(100);
previousTime = millis();
while( ( millis() - previousTime ) < timeout )
{
if( !WaitingSerialAvailable( previousTime, timeout ) )
{
return false;
}
response[answerCount++] = sim900Serial->read();
for ( int i = 0; i<totalAnwers; i++ )
{
if ( strstr( response, expectedAnswers[i] ) != NULL )
{
return i+1;
}
}
}
return 0;
}
int8_t Connection::ReceiveATReply( const char * expectedAnswer,
const unsigned int & timeout )
{
const char * expectedAnswers[] = { expectedAnswer };
return ReceiveATReply( expectedAnswers, 1, timeout );
}