-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModem.cpp
More file actions
442 lines (410 loc) · 9.99 KB
/
Copy pathModem.cpp
File metadata and controls
442 lines (410 loc) · 9.99 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
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
#include "Modem.h"
extern volatile uint8_t delayEspired;
Modem::Modem(SoftwareSerial *my) {
Serial.print(F("Start create modem"));
gsm = my;
gsm->begin(9600);
}
void Modem::changeShowCommand(uint8_t isShow) {
isShowCommand = (isShow != 0) ? true : false;
Serial.print(F("\n\tvisibility command = "));
Serial.println((isShow) ? "true" : "false");
}
void Modem::initModem() {
digitalWrite(LED_PIN, HIGH);
digitalWrite(RST_PIN, SIM_NO_RST);
// start SIM900
setEspiredTime(250);
char ch;
while (gsm->available() > 0 || delayEspired != 0) {
if (gsm->available() > 0) {
ch = gsm->read();
#if DEBUG
// Serial.write(ch);
#endif
}
if (ch == 'y')break;
delay(1);
}
if ( delayEspired == 0 ) {
// ------ if can`t get "call ready" answer then must make the reboot modem(SIM900) -------
if (isShowCommand)Serial.println(F("\n\tERROR [ can`t start SIM900 - try it reboot]\n"));
digitalWrite(RST_PIN, SIM_RST);
setEspiredTime(250);
while (delayEspired != 0) {}
rebootSIM();
}
else {
setEspiredTime(1);
// ---------------------- on success get connect tune modem ---------------------------
if (isShowCommand) Serial.println(" \nEnds of wait ");
dropGSM();
sendCommand(F("ATE0 \r")); // echo off
checkOnOK(0);
sendCommand(F("ATV1 \r")); // out on echo off
checkOnOK(0);
sendCommand(F("AT+CMGF=1 \r")); // sms text mode
checkOnOK(0);
sendCommand(F("AT+CSCS=\"GSM\" \r")); // sms gsm charset
checkOnOK(0);
sendCommand(F("AT+CPBS=\"SM\" \r")); // set "SM" phonebook
checkOnOK(0);
sendCommand(F("AT+CLIP=1 \r")); // The calling line identity(auto get caller phone number)
checkOnOK(0);
sendCommand(F("AT+CFSINIT \r")); //
delay(2000);
dropGSM();
delay(2000);
digitalWrite(LED_PIN, LOW);
Serial.println(F("\n\tsuccess Init\n"));
if (gsm->available() > 0) dropGSM();
}
}
/*
after check answer from SIM flush RX
buffer(this help avoid some missmath)
*/
void Modem::dropGSM() {
delay(20);
setEspiredTime(2);
while ( gsm->available() < 1 && delayEspired > 0) {
delay(2);
}
while (gsm->available() > 0) {
read();
delay(2);
}
}
/*
send command to SIM as complete String
*/
void Modem::sendCommand(String toSIM) {
dropGSM();
#if DEBUG
if (isShowCommand) {
Serial.print(F("\nsend command: "));
Serial.print(toSIM);
Serial.print(" ");
}
#endif
gsm->println(toSIM);
gsm->print("\r");
gsm->flush();
delay(1);
}
//================= for HTTP connection ====================================
/*
Start GPRS connect and open HTTP session
*/
bool Modem::openConnect(void){
while(Serial.available() > 0) Serial.read();
dropGSM();
sendCommand(F("AT+CFSTERM"));
dropGSM();
if(checkConnect() == false){
//Serial.println(F("Try Open GPRS connect for HTTP"));
dropGSM();
sendCommand(F("AT+SAPBR=1,1"));
if(!checkOnOK(DELAY_FOR_CONNECT)) return false;
// Serial.println(F("Success init GPRS/ Try open HTTP"));
dropGSM();
sendCommand(F("AT+HTTPINIT"));
}else{
Serial.println(F("Already connected"));
sendCommand(F("AT+HTTPINIT"));
}
return checkOnOK(0);
}
/*
return true if module is connected to GPRS
*/
bool Modem::checkConnect(void){
sendCommand(F("AT+SAPBR=2,1"));
setEspiredTime(DELAY_FOR_ANSWER);
while(available() < 22 && delayEspired > 0)delay(10);
delay(50);
uint8_t size= available();
char res[size + 1];
uint8_t n = 0;
while(n < size){
res[n]=read();
n++;
}
res[n]='\0';
Serial.println("On check connect get " + String(res));
char pattern[] = {'0','.','0','.','0'};
// First REPlace
if(strstr(res, pattern) != NULL) {
sendCommand(F("AT+HTTPTERM "));
checkOnOK(0);
return false;
}
return true;
}
/*
stop httpService and close gprs connect
*/
bool Modem::closeConnect(void) {
#if DEBUG
Serial.println(F("\ncloseConnect"));
#endif
dropGSM();
sendCommand(F("AT+HTTPTERM \r"));
dropGSM();
sendCommand(F("AT+CFSINIT"));
dropGSM();
return true;
}
/*
check answer on contain "Ok" or "ERROR"
return true if contain "OK"
*/
bool Modem::checkOnOK(uint8_t wait_time) {
delay(10);
char ch = '\0';
bool checkNext = false;
#if DEBUG
if (isShowCommand) Serial.print(F(" CheckOnOK "));
#endif
if (wait_time == 0)setEspiredTime(DELAY_FOR_OK);
else setEspiredTime(wait_time);
while ( gsm-> available() < 4 && delayEspired > 0) {
delay(1);
}
if (gsm->available() < 2)return false;
while ( gsm->available() > 0 ) {
ch = gsm->read();
if ( ch == '\r' || ch == '\n' || ch == ' ') {
delay(1);
checkNext = false;
continue;
}
#if DEBUG
if (isShowCommand)Serial.write(ch);
#endif
if ( checkNext && (ch == 'K')) {
#if DEBUG
if (isShowCommand) Serial.println(F(" : true"));
#endif
dropGSM();
delayEspired = 0;
return true;
}
if (ch == 'O')checkNext = true;
else checkNext = false;
if (ch == 'R') {
dropGSM();
delayEspired = 0;
#if DEBUG
if (isShowCommand) Serial.println(F(" : false"));
#endif
return false;
}
delay(1);
}
#if DEBUG
if (isShowCommand) Serial.println(F(" : false"));
#endif
dropGSM();
return false;
}
void Modem::rebootSIM(void) {
digitalWrite(RST_PIN, SIM_RST);
delay(9000);
Serial.println(F("\nREBOOT\n"));
initModem();
}
// ===================== utility =======================================
/*
fill client number on order in phonebook
*/
bool Modem::fillUCS2PhoneNumber(uint8_t clientNumber, char *phone) {
dropGSM();
if (setFormatSms(UCS2_FORMAT) == false)return false;
String command = String("AT+CPBR=") + clientNumber + " \r ";
#if DEBUG
/*
Serial.print(F("fillPhoneNumber(UCS2) for "));
Serial.print(clientNumber);
Serial.println(" command " + command + "\n");
dropGSM();
sendCommand(command);
delay(1);
// wait for answer and start parse gotten
setEspiredTime(DELAY_FOR_OK);
while(available()<30 && delayEspired != 0)delay(5);
while(available()>0)Serial.write(read());
Serial.println();
*/
#endif
dropGSM();
sendCommand(command);
char ch = '\0';
setEspiredTime(DELAY_FOR_OK);
while (available() < 30 && delayEspired != 0)delay(1);
uint8_t i = 0;
delay(20);
// wait for answer and start parse gotten
setEspiredTime(DELAY_FOR_OK);
while (available() > 0 && delayEspired != 0) {
if (available() > 0 && i < 3) {
if (read() == '"') i++;
}
if ( i == 3 ) break;
delay(1);
}
setEspiredTime(DELAY_FOR_OK);
while (available() < 48 && delayEspired != 0)delay(2);
// too small data -> wrong send
if (available() < 48) {
#if DEBUG
Serial.print(F(" [short or can`t get answer on reques phone: "));
while (available() > 0)Serial.write(read());
Serial.println(" ]");
#endif
phone[0] = '\0';
dropGSM();
return false;
}
// if (debug)Serial.print(F(" start parse: "));
uint8_t has_exit = 0;
i = 0;
while ( available() > 0 && !has_exit) {
ch = read();
// if (debug)Serial.write(ch);
if (ch == '"') {
has_exit = 1;
continue;
}
phone[i] = ch;
i++;
delay(1);
}
dropGSM();
/* if (debug) {
Serial.println();
Serial.println(" Phone contain " + String(i) + " Symbols ");
}
*/
if (i < (UCS2_PHONE_SIZE - 1)) {
phone[0] = '\0';
return false;
}
return true;
}
/*
fill cool themoperature from sim phonebook
*/
bool Modem::fillMinThemperature(char *T) {
return fillGSMPhone(COOL_THEMPERATURE_RECORD, T);
}
/*
* return whether send motion warning sms
*/
uint8_t Modem::isSendMotion(){
char field[13];
if(fillGSMPhone(IS_SEND_MOTION_RECORD, field)){
if(field[0] == '1')return SEND_SMS;
}
return NO_SEND_SMS;
}
/*
*/
bool Modem::fillGSMPhone(uint8_t clientNumber, char *phone) {
#if DEBUG
/*
Serial.print(F("Start Fill phone number(GSM) client: "));
Serial.print(clientNumber);
Serial.write(' ');
*/
#endif
phone[0] = '\0';
dropGSM();
if (!setFormatSms(GSM_FORMAT))return false;
String command = String("AT+CPBR=") + clientNumber + " \r ";
dropGSM();
sendCommand(command);
setEspiredTime(2);
while (available() < 31 && delayEspired != 0) delay(1);
delay(5);
if (available() < 30 ) {
Serial.println(F(" Error read phone - Wery short answer"));
return false;
}
while (read() != '"' && available() > 0)delay(1);
uint8_t i = 0;
while (available() > 0 && i < 13 ) {
phone[i] = read();
if (phone[i] == '"') {
phone[i] = '\0';
break;
}
i++;
}
if (clientNumber > CLIENT2)return true;
#if DEBUG
// for ( uint8_t j = 0; j < i; j++)Serial.write(phone[j]);
// Serial.write(' ');
#endif
if (i < 12) {
Serial.print(F(" Error read phone - Short phone number book record ="));
Serial.println(clientNumber);
phone[0] = '\0';
return false;
}
if (checkPhoneFormat(phone) == false) {
phone[0] = '\0';
return false;
}
Serial.println();
return true;
}
/*
check phone number in GSM format
*/
bool Modem::checkPhoneFormat(char *phone) {
bool retVal = true;
if (phone[0] != '+')retVal = false;
for (uint8_t i = 1; i < 12; i++) {
if ( phone[i] < 48 || phone[i] > 57 ) retVal = false;
}
if (retVal == false)Serial.println(F(" Wrong format phone number!"));
return retVal;
}
bool Modem::setFormatSms( const char *format) {
#if DEBUG
Serial.print(F(" setFormatSms : "));
#endif
char answ[16];
uint8_t i = 0;
println("AT \r");
checkOnOK(0);
sendCommand(F("AT+CSCS? \r"));
setEspiredTime(DELAY_FOR_OK);
while (available() < 15 && delayEspired != 0)delay(1);
delay(10);
if (available() < 15) return false;
while (available() > 0 && i < 15) {
answ[i] = read();
i++;
}
answ[i] = '\0';
#if DEBUG
/*
Serial.print(F(" answ : "));
Serial.print(answ);
Serial.print(" - ");
*/
#endif
if (strstr(answ , format) != NULL) {
#if DEBUG
Serial.println(F("Not change "));
#endif
return true;
}
sendCommand("AT+CSCS=\"" + String(format) + "\" \r");
#if DEBUG
Serial.println(F("changed "));
#endif
return checkOnOK(DELAY_FOR_OK);
}