forked from Seeed-Studio/Seeeduino_GPRS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gprs.cpp
196 lines (178 loc) · 4.79 KB
/
gprs.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
/*
* gprs.cpp
* A library for SeeedStudio seeeduino GPRS shield
*
* Copyright (c) 2013 seeed technology inc.
* Author : lawliet zou
* Create Time : Dec 2013
* 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 "gprs.h"
int GPRS::init(void)
{
for(int i = 0; i < 2; i++){
sendCmd("AT\r\n");
delay(100);
}
sendCmd("AT+CFUN=1\r\n");
if(0 != checkSIMStatus()) {
ERROR("ERROR:checkSIMStatus");
return -1;
}
return 0;
}
int GPRS::checkSIMStatus(void)
{
char gprsBuffer[32];
int count = 0;
cleanBuffer(gprsBuffer,32);
while(count < 3) {
sendCmd("AT+CPIN?\r\n");
readBuffer(gprsBuffer,32,DEFAULT_TIMEOUT);
if((NULL != strstr(gprsBuffer,"+CPIN: READY"))) {
break;
}
count++;
delay(1000);
}
if(count == 3) {
return -1;
}
return 0;
}
int GPRS::networkCheck(void)
{
delay(1000);
if(0 != sendCmdAndWaitForResp("AT+CGREG?\r\n","+CGREG: 0,1",DEFAULT_TIMEOUT*3)) {
ERROR("ERROR:CGREG");
return -1;
}
delay(1000);
if(0 != sendCmdAndWaitForResp("AT+CGATT?\r\n","+CGATT: 1",DEFAULT_TIMEOUT)) {
ERROR("ERROR:CGATT");
return -1;
}
return 0;
}
int GPRS::sendSMS(char *number, char *data)
{
char cmd[32];
if(0 != sendCmdAndWaitForResp("AT+CMGF=1\r\n", "OK", DEFAULT_TIMEOUT)) { // Set message mode to ASCII
ERROR("ERROR:CMGF");
return -1;
}
delay(500);
snprintf(cmd, sizeof(cmd),"AT+CMGS=\"%s\"\r\n", number);
if(0 != sendCmdAndWaitForResp(cmd,">",DEFAULT_TIMEOUT)) {
ERROR("ERROR:CMGS");
return -1;
}
delay(1000);
serialSIM800.write(data);
delay(500);
sendEndMark();
return 0;
}
int GPRS::readSMS(int messageIndex, char *message,int length)
{
int i = 0;
char gprsBuffer[100];
char cmd[16];
char *p,*s;
sendCmdAndWaitForResp("AT+CMGF=1\r\n","OK",DEFAULT_TIMEOUT);
delay(1000);
sprintf(cmd,"AT+CMGR=%d\r\n",messageIndex);
serialSIM800.write(cmd);
cleanBuffer(gprsBuffer,100);
readBuffer(gprsBuffer,100,DEFAULT_TIMEOUT);
if(NULL != ( s = strstr(gprsBuffer,"+CMGR"))){
if(NULL != ( s = strstr(gprsBuffer,"+32"))){
p = s + 6;
while((*p != '$')&&(i < length-1)) {
message[i++] = *(p++);
}
message[i] = '\0';
}
}
return 0;
}
int GPRS::deleteSMS(int index)
{
char cmd[16];
snprintf(cmd,sizeof(cmd),"AT+CMGD=%d\r\n",index);
sendCmd(cmd);
return 0;
}
int GPRS::callUp(char *number)
{
char cmd[24];
if(0 != sendCmdAndWaitForResp("AT+COLP=1\r\n","OK",5)) {
ERROR("COLP");
return -1;
}
delay(1000);
sprintf(cmd,"\r\nATD%s;\r\n", number);
serialSIM800.write(cmd);
return 0;
}
int GPRS::answer(void)
{
serialSIM800.write("ATA\r\n");
return 0;
}
int GPRS::connectTCP(const char *ip, int port)
{
char cipstart[50];
sprintf(cipstart, "AT+CIPSTART=\"TCP\",\"%s\",\"%d\"\r\n", ip, port);
if(0 != sendCmdAndWaitForResp(cipstart, "CONNECT OK", 10)) {// connect tcp
ERROR("ERROR:CIPSTART");
return -1;
}
return 0;
}
int GPRS::sendTCPData(char *data)
{
char cmd[32];
int len = strlen(data);
snprintf(cmd,sizeof(cmd),"AT+CIPSEND=%d\r\n",len);
if(0 != sendCmdAndWaitForResp(cmd,">",2*DEFAULT_TIMEOUT)) {
ERROR("ERROR:CIPSEND");
return -1;
}
if(0 != sendCmdAndWaitForResp(data,"SEND OK",2*DEFAULT_TIMEOUT)) {
ERROR("ERROR:SendTCPData");
return -1;
}
return 0;
}
int GPRS::closeTCP(void)
{
sendCmd("AT+CIPCLOSE\r\n");
return 0;
}
int GPRS::shutTCP(void)
{
sendCmd("AT+CIPSHUT\r\n");
return 0;
}