-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsim900MiniLib.cpp
355 lines (304 loc) · 9 KB
/
sim900MiniLib.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
// sim900MiniLib.cpp
#include "sim900MiniLib.h"
// Constructor /////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////
/////////////////////////////
// Init function
sim900MiniLib::sim900MiniLib(SoftwareSerial * SIM900Serial, HardwareSerial * hwSerial, boolean debug) {
_serial = SIM900Serial;
_hwSerial = hwSerial;
_debug = debug;
}
// Private Methods /////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////
/////////////////////////////
// Forward what Software Serial received to Serial Port
void sim900MiniLib::_updateSerial() {
if (_debug) {
delay(500);
while (_serial->available() > 0) {
_hwSerial->write(_serial->read());
}
}
}
// Print debug logs when debug is enable
void sim900MiniLib::_printDebug(const __FlashStringHelper* message, boolean newLine) {
if (_debug) {
if (newLine) {
_hwSerial->println(message);
}
else {
_hwSerial->print(message);
}
}
}
// Check return of the previous cmd, DEFAULT ARG = OK
boolean sim900MiniLib::_execCmd(const String cmd, boolean (*function)(int), boolean returnResult, String dataToReturn[1], const String resultMustBe, const __FlashStringHelper* prefix)
{
String dataS;
if (prefix != F("")) {this->_printDebug(prefix, false);}
_serial->println(cmd);
delay(150);
for (int i=0; i <= 5; i++) {
while (_serial->available() > 0) {
char data = _serial->read();
if ((function)(data)) {
dataS.concat(data);
}
}
if (dataS != "") {
break;
}
else {
delay(20);
}
}
// if returnResult is true and resultMustBe is "" then assign dataToReturn + return true
// if returnResult is true and resultMustBe is not "" then assing dataToReturn + continue the function
if (returnResult) {
dataToReturn[0] = dataS;
if (resultMustBe == "") {
return true;
}
}
// resultMustBe is mandatory if returnResult is false
if (!returnResult && resultMustBe == ""){
this->_printDebug(F("please provide resultMustBe"));
return false;
}
// check the result
if (dataS.endsWith(resultMustBe)) {
this->_printDebug(F(" is ok"));
return true;
}
else {
this->_printDebug(F(" is NOTOK"));
return false;
}
}
// Public Methods //////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////
/////////////////////////////
// Print debug mode (enabled/disabled)
void sim900MiniLib::checkDebug() {
if (_debug) {
_hwSerial->println(F("Debug enabled"));
}
else {
_hwSerial->println(F("Debug disabled"));
}
}
// Start the GSM _serial->module
void sim900MiniLib::startORstop(const int pin) {
pinMode(pin, OUTPUT);
digitalWrite(pin, HIGH);
delay(1000);
digitalWrite(pin, LOW);
delay(5000);
}
// Check status of SIM900 module
boolean sim900MiniLib::status() {
String trash[1];
return this->_execCmd("AT", isAlphaNumeric, false, trash, "ATOK", F("[S900] check is started :"));
}
// AT command to set _serial->to TEXT mode
boolean sim900MiniLib::textMode(boolean check) {
String trash[1];
if (check) {
return this->_execCmd("AT+CMGF?", isAlphaNumeric, false, trash, "CMGF1OK", F("[S900] check txt mode :"));
}
else {
return this->_execCmd("AT+CMGF=1", isAlphaNumeric, false, trash, "OK", F("[S900] enable txt mode :"));
}
}
// AT command to set _serial->to automatic timezone mode
boolean sim900MiniLib::autoTimezone(boolean check) {
String trash[1];
if (check) {
return this->_execCmd("AT+CTZU?", isAlphaNumeric, false, trash, "CTZU1OK", F("[S900] check autoTimezone :"));
}
else {
return this->_execCmd("AT+CTZU=1", isAlphaNumeric, false, trash, "OK", F("[S900] enable autoTimezone :"));
}
}
// Enable SMS Received mode
// Set module to send SMS data to serial out upon receipt
boolean sim900MiniLib::receiveSMSMode(const String action) {
String cmd, resultMustBe, trash[1];
const __FlashStringHelper* preffix;
if (action == "check") {
return this->_execCmd("AT+CNMI?", isAlphaNumeric, false, trash, "CNMI22000OK", F("[S900] check rcvdSmsMode :"));
}
else if (action == "enable") {
cmd = "AT+CNMI=2,2,0,0,0";
resultMustBe = "CNMI22000OK";
preffix = F("[S900] en rcvdSmsMode :");
}
else if (action == "disable") {
cmd = "AT+CNMI=0,0,0,0,0";
resultMustBe = "CNMI00000OK";
preffix = F("[S900] dis rcvdSmsMode :");
}
// for enable disable allow 5 retry
for (int i=0; i <= 5; i++) {
if (this->_execCmd(cmd, isAlphaNumeric, false, trash, resultMustBe, preffix)) {
return true;
} else {
delay(250);
}
}
return false;
}
// Check GMS registration
// 0 is false
// 1 is true (registrated)
// 2 is asking for registrated (you can retry your check)
boolean sim900MiniLib::checkRegistration() {
String dataS[1];
this->_execCmd("AT+CREG?", isDigit, true, dataS, "", F("[S900] check GSM : "));
if (dataS[0] == "01" || dataS[0] == "05") {
this->_printDebug(F("is registrated"));
return 1;
}
else if (dataS[0] == "02") {
this->_printDebug(F("is asking for registration"));
return 2;
}
else {
this->_printDebug(F("is not registrated"));
return 0;
}
}
// Extract phone number, index, date and message from the SMS
boolean sim900MiniLib::readSMS(String smsData[4]) {
int ligneDelim = 0; // delimiter = '\n'
int SMSInfoDelim = 0; //delimiter = '"'
int rcvdCheck = 0;
boolean rcvdIsSMS = false;
String SMSSender = "";
String SMSIndex = "";
String SMSDate = "";
String SMSMessage = "";
while (_serial->available() > 0) {
char data = _serial->read();
if (data == '\n') {ligneDelim++;}
if (data == '"') {SMSInfoDelim++;}
if (ligneDelim == 1) {
if (!rcvdIsSMS) {
if (data == '+') {rcvdCheck++;}
if (data == 'C') {rcvdCheck++;}
if (data == 'M') {rcvdCheck++;}
if (data == 'T') {rcvdCheck++;}
if (data == ':') {rcvdCheck++;}
if (rcvdCheck == 5) {rcvdIsSMS = true;}
}
if (rcvdIsSMS == 1 && data != '"') {
if (SMSInfoDelim == 1) {SMSSender.concat(data);}
if (SMSInfoDelim == 3) {SMSIndex.concat(data);}
if (SMSInfoDelim == 5) {SMSDate.concat(data);}
}
}
if (ligneDelim == 2 && rcvdIsSMS && data != '\n' && data != ' ') {
SMSMessage.concat(data);
}
if (ligneDelim == 3 && rcvdIsSMS) {
SMSSender.trim();
SMSIndex.trim();
SMSDate.trim();
SMSMessage.trim();
SMSMessage.toLowerCase();
smsData[0] = SMSSender;
smsData[1] = SMSIndex;
smsData[2] = SMSDate;
smsData[3] = SMSMessage;
this->_printDebug(F("[S900] sms received"));
return true;
}
}
return false;
}
// Send SMS
boolean sim900MiniLib::sendSMS(const String phoneNumber, const String textSMS) {
// send sms
this->_printDebug(F("[S900] reply sms :"));
_serial->println("AT + CMGS = \"" + phoneNumber + "\"");
delay(70);
_serial->print(textSMS); // End AT command with ASCII code 26
delay(70);
_serial->write(26);
// wait 30s for sms send
for (int i = 0; i <= 120; i++) {
String dataS;
while (_serial->available() > 0) {
char data = _serial->read();
if (isAlpha(data)) {
dataS.concat(data);
}
}
if (dataS.endsWith("CMGSOK")) {
this->_printDebug(F("is ok"));
return true;
}
else {
this->_printDebug(F("waiting"));
}
delay(250);
}
this->_printDebug(F("is NOTOK"));
return false;
}
// Get time
boolean sim900MiniLib::time(String timeInfos[7]) {
String dataToReturn[1];
if(!this->_execCmd("AT+CCLK?", isAlphaNumeric, true, dataToReturn, "OK", F("[S900] time :"))) {
return false;
}
char charArr[26];
dataToReturn[0].toCharArray(charArr, 26);
String digit;
int x=1;
int y=0;
for (int i=0; i < 26; i++) {
if (isDigit(charArr[i])) {
if (x == 3) {
x = 1; y++;
if (y == 6) { break; }
}
if (x <= 2) {
timeInfos[y].concat(charArr[i]);
x++;
}
}
}
return true;
}
// Call someone
void sim900MiniLib::callSomeone(const String phoneNumber, long int delayBeforeHangUp) {
delay(500); // wait to avoid freeze from the sim900 module
String trash[1];
boolean terminated = false;
long initialTime = millis();
long callTimeout = initialTime + delayBeforeHangUp;
long actualTime = millis();
// start the call
this->_execCmd("ATD" + phoneNumber + ";", isAlphaNumeric, false, trash, "OK", F("[S900] calling :"));
while (actualTime < callTimeout) {
actualTime = millis();
// security for avoid bugs with arduino uptime reset
if ( actualTime < initialTime) {
break;
}
String callStatus[1];
this->_execCmd("AT+CPAS", isDigit, true, callStatus, "", F(""));
if (callStatus[0] == "0") {
this->_printDebug(F("terminated"));
terminated = true;
break;
}
this->_printDebug(F("progress"));
delay(500);
}
if(!terminated) {
this->_execCmd("ATH", isAlphaNumeric, false, trash, "ATH", F("[S900] hang up :"));
}
}