-
Notifications
You must be signed in to change notification settings - Fork 5
/
MC20_Common.cpp
executable file
·433 lines (385 loc) · 11.6 KB
/
MC20_Common.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
/*
* MC20_Arduino_Interface.cpp
* A library for SeeedStudio GPS Tracker
*
* Copyright (c) 2017 seeed technology inc.
* Website : www.seeed.cc
* Author : lawliet zou, lambor
* Create Time: April 2017
* Change Log :
*
* The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdio.h>
#include "MC20_Common.h"
// GPSTracker* GPSTracker::inst;
GPSTracker::GPSTracker()
{
// inst = this;
// MC20_init();
// io_init();
pinMode(RGB_PIN, OUTPUT);
digitalWrite(RGB_PIN, LOW);
}
bool GPSTracker::init(void)
{
if(!MC20_check_with_cmd(F("AT\r\n"),"OK\r\n",CMD)){
return false;
}
if(!MC20_check_with_cmd(F("AT+CPIN?\r\n"),"OK\r\n",CMD)){
return false;
}
if(!checkSIMStatus()) {
return false;
}
return true;
}
bool GPSTracker::Check_If_Power_On(void)
{
return MC20_check_with_cmd(F("AT\n\r"), "OK", CMD, 2, 2000);
}
void GPSTracker::Power_On(void)
{
MC20_init();
if(Check_If_Power_On()){
return;
}
pinMode(PWR_BAT, OUTPUT);
pinMode(PWR_KEY, OUTPUT);
digitalWrite(PWR_BAT, HIGH);
digitalWrite(PWR_KEY, HIGH);
delay(2000);
digitalWrite(PWR_KEY, LOW);
// delay(2000);
}
void GPSTracker::powerReset(void)
{
digitalWrite(PWR_KEY, LOW);
}
void GPSTracker::io_init()
{
for(int i = 0; i< 20; i++){
pinMode(12, OUTPUT);
digitalWrite(i, LOW);
}
}
bool GPSTracker::checkSIMStatus(void)
{
char mc20_Buffer[32];
int count = 0;
MC20_clean_buffer(mc20_Buffer,32);
while(count < 3) {
MC20_send_cmd("AT+CPIN?\r\n");
MC20_read_buffer(mc20_Buffer,32,DEFAULT_TIMEOUT);
if((NULL != strstr(mc20_Buffer,"+CPIN: READY"))) {
break;
}
count++;
delay(300);
}
if(count == 3) {
return false;
}
return true;
}
bool GPSTracker::waitForNetworkRegister(void)
{
int errCounts = 0;
//
while(!MC20_check_with_cmd("AT+CREG?\n\r", "+CREG: 0,1", CMD, 2, 2000)){
errCounts++;
if(errCounts > 30) // Check for 30 times
{
return false;
}
delay(1000);
}
errCounts = 0;
while(!MC20_check_with_cmd("AT+CGREG?\n\r", "+CGREG: 0,1", CMD, 2, 2000)){
errCounts++;
if(errCounts > 30) // Check for 30 times
{
return false;
}
delay(1000);
}
return true;
}
bool GPSTracker::sendSMS(char *number, char *data)
{
//char cmd[32];
if(!MC20_check_with_cmd(F("AT+CMGF=1\r\n"), "OK\r\n", CMD)) { // Set message mode to ASCII
return false;
}
delay(500);
MC20_flush_serial();
MC20_send_cmd("AT+CMGS=\"");
MC20_send_cmd(number);
if(!MC20_check_with_cmd(F("\"\r\n"),">",CMD)) {
return false;
}
delay(1000);
MC20_send_cmd(data);
delay(500);
MC20_send_End_Mark();
return MC20_wait_for_resp("OK\r\n", CMD, 10);
}
char GPSTracker::isSMSunread()
{
char mc20_Buffer[48]; //48 is enough to see +CMGL:
char *s;
//List of all UNREAD SMS and DON'T change the SMS UNREAD STATUS
MC20_send_cmd("AT+CMGL=\"REC UNREAD\",1\r\n");
/*If you want to change SMS status to READ you will need to send:
AT+CMGL=\"REC UNREAD\"\r\n
This command will list all UNREAD SMS and change all of them to READ
If there is not SMS, response is (30 chars)
AT+CMGL="REC UNREAD",1 --> 22 + 2
--> 2
OK --> 2 + 2
If there is SMS, response is like (>64 chars)
AT+CMGL="REC UNREAD",1
+CMGL: 9,"REC UNREAD","XXXXXXXXX","","14/10/16,21:40:08+08"
Here SMS text.
OK
or
AT+CMGL="REC UNREAD",1
+CMGL: 9,"REC UNREAD","XXXXXXXXX","","14/10/16,21:40:08+08"
Here SMS text.
+CMGL: 10,"REC UNREAD","YYYYYYYYY","","14/10/16,21:40:08+08"
Here second SMS
OK
*/
MC20_clean_buffer(mc20_Buffer,31);
MC20_read_buffer(mc20_Buffer,30,DEFAULT_TIMEOUT);
//Serial.print("Buffer isSMSunread: ");Serial.println(mc20_Buffer);
if(NULL != ( s = strstr(mc20_Buffer,"OK"))) {
//In 30 bytes "doesn't" fit whole +CMGL: response, if recieve only "OK"
// means you don't have any UNREAD SMS
delay(50);
return 0;
} else {
//More buffer to read
//We are going to flush serial data until OK is recieved
MC20_wait_for_resp("OK\r\n", CMD);
//MC20_flush_serial();
//We have to call command again
MC20_send_cmd("AT+CMGL=\"REC UNREAD\",1\r\n");
MC20_clean_buffer(mc20_Buffer,48);
MC20_read_buffer(mc20_Buffer,47,DEFAULT_TIMEOUT);
//Serial.print("Buffer isSMSunread 2: ");Serial.println(mc20_Buffer);
if(NULL != ( s = strstr(mc20_Buffer,"+CMGL:"))) {
//There is at least one UNREAD SMS, get index/position
s = strstr(mc20_Buffer,":");
if (s != NULL) {
//We are going to flush serial data until OK is recieved
MC20_wait_for_resp("OK\r\n", CMD);
return atoi(s+1);
}
} else {
return -1;
}
}
return -1;
}
bool GPSTracker::readSMS(int messageIndex, char *message, int length, char *phone, char *datetime)
{
/* Response is like:
AT+CMGR=2
+CMGR: "REC READ","XXXXXXXXXXX","","14/10/09,17:30:17+08"
SMS text here
So we need (more or lees), 80 chars plus expected message length in buffer. CAUTION FREE MEMORY
*/
int i = 0;
char mc20_Buffer[80 + length];
//char cmd[16];
char num[4];
char *p,*p2,*s;
MC20_check_with_cmd(F("AT+CMGF=1\r\n"),"OK\r\n",CMD);
delay(1000);
//sprintf(cmd,"AT+CMGR=%d\r\n",messageIndex);
//MC20_send_cmd(cmd);
MC20_send_cmd("AT+CMGR=");
itoa(messageIndex, num, 10);
MC20_send_cmd(num);
MC20_send_cmd("\r\n");
MC20_clean_buffer(mc20_Buffer,sizeof(mc20_Buffer));
MC20_read_buffer(mc20_Buffer,sizeof(mc20_Buffer));
if(NULL != ( s = strstr(mc20_Buffer,"READ\",\""))){
// Extract phone number string
p = strstr(s,",");
p2 = p + 2; //We are in the first phone number character
p = strstr((char *)(p2), "\"");
if (NULL != p) {
i = 0;
while (p2 < p) {
phone[i++] = *(p2++);
}
phone[i] = '\0';
}
// Extract date time string
p = strstr((char *)(p2),",");
p2 = p + 1;
p = strstr((char *)(p2), ",");
p2 = p + 2; //We are in the first date time character
p = strstr((char *)(p2), "\"");
if (NULL != p) {
i = 0;
while (p2 < p) {
datetime[i++] = *(p2++);
}
datetime[i] = '\0';
}
if(NULL != ( s = strstr(s,"\r\n"))){
i = 0;
p = s + 2;
while((*p != '\r')&&(i < length-1)) {
message[i++] = *(p++);
}
message[i] = '\0';
}
return true;
}
return false;
}
bool GPSTracker::readSMS(int messageIndex, char *message,int length)
{
int i = 0;
char mc20_Buffer[100];
//char cmd[16];
char num[4];
char *p,*s;
MC20_check_with_cmd(F("AT+CMGF=1\r\n"),"OK\r\n",CMD);
delay(1000);
MC20_send_cmd("AT+CMGR=");
itoa(messageIndex, num, 10);
MC20_send_cmd(num);
// MC20_send_cmd("\r\n");
MC20_send_cmd("\r\n");
// sprintf(cmd,"AT+CMGR=%d\r\n",messageIndex);
// MC20_send_cmd(cmd);
MC20_clean_buffer(mc20_Buffer,sizeof(mc20_Buffer));
MC20_read_buffer(mc20_Buffer,sizeof(mc20_Buffer),DEFAULT_TIMEOUT);
if(NULL != ( s = strstr(mc20_Buffer,"+CMGR:"))){
if(NULL != ( s = strstr(s,"\r\n"))){
p = s + 2;
while((*p != '\r')&&(i < length-1)) {
message[i++] = *(p++);
}
message[i] = '\0';
return true;
}
}
return false;
}
bool GPSTracker::deleteSMS(int index)
{
//char cmd[16];
char num[4];
//sprintf(cmd,"AT+CMGD=%d\r\n",index);
MC20_send_cmd("AT+CMGD=");
itoa(index, num, 10);
MC20_send_cmd(num);
//snprintf(cmd,sizeof(cmd),"AT+CMGD=%d\r\n",index);
//MC20_send_cmd(cmd);
//return 0;
// We have to wait OK response
//return MC20_check_with_cmd(cmd,"OK\r\n",CMD);
return MC20_check_with_cmd(F("\r"),"OK\r\n",CMD);
}
bool GPSTracker::callUp(char *number)
{
//char cmd[24];
if(!MC20_check_with_cmd(F("AT+COLP=1\r\n"),"OK\r\n",CMD)) {
return false;
}
delay(1000);
//HACERR quitar SPRINTF para ahorar memoria ???
//sprintf(cmd,"ATD%s;\r\n", number);
//MC20_send_cmd(cmd);
MC20_send_cmd("ATD");
MC20_send_cmd(number);
MC20_send_cmd(";\r\n");
return true;
}
void GPSTracker::answer(void)
{
MC20_send_cmd("ATA\r\n"); //TO CHECK: ATA doesnt return "OK" ????
}
bool GPSTracker::hangup(void)
{
return MC20_check_with_cmd(F("ATH\r\n"),"OK\r\n",CMD);
}
bool GPSTracker::getSignalStrength(int *buffer)
{
//AT+CSQ --> 6 + CR = 10
//+CSQ: <rssi>,<ber> --> CRLF + 5 + CRLF = 9
//OK --> CRLF + 2 + CRLF = 6
byte i = 0;
char mc20_Buffer[26];
char *p, *s;
char buffers[4];
MC20_flush_serial();
MC20_send_cmd("AT+CSQ\r");
MC20_clean_buffer(mc20_Buffer, 26);
MC20_read_buffer(mc20_Buffer, 26, DEFAULT_TIMEOUT);
if (NULL != (s = strstr(mc20_Buffer, "+CSQ:"))) {
s = strstr((char *)(s), " ");
s = s + 1; //We are in the first phone number character
p = strstr((char *)(s), ","); //p is last character """
if (NULL != s) {
i = 0;
while (s < p) {
buffers[i++] = *(s++);
}
buffers[i] = '\0';
}
*buffer = atoi(buffers);
return true;
}
return false;
}
int GPSTracker::recv(char* buf, int len)
{
MC20_clean_buffer(buf,len);
MC20_read_buffer(buf,len); //Ya he llamado a la funcion con la longitud del buffer - 1 y luego le estoy añadiendo el 0
return strlen(buf);
}
bool GPSTracker::GSM_work_mode(int mode)
{
char buf_w[20];
MC20_clean_buffer(buf_w, 20);
sprintf(buf_w, "AT+CFUN=%d", mode);
MC20_send_cmd(buf_w);
return MC20_check_with_cmd("\n\r", "OK", CMD, 2, 2000, UART_DEBUG);
}
bool GPSTracker::GSM_config_slow_clk(int mode)
{
char buf_w[20];
MC20_clean_buffer(buf_w, 20);
sprintf(buf_w, "AT+QSCLK=%d", mode);
MC20_send_cmd(buf_w);
return MC20_check_with_cmd("\n\r", "OK", CMD, 2, 2000, UART_DEBUG);
}
bool GPSTracker::AT_PowerDown(void)
{
return MC20_check_with_cmd("AT+QPOWD=1\n\r", "NORMAL POWER DOWN", CMD, 5, 2000, UART_DEBUG);
}