forked from DFRobot/DFRobot_SIM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DFRobot_SIMclient.cpp
164 lines (158 loc) · 4.26 KB
/
DFRobot_SIMclient.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
#include "DFRobot_SIMclient.h"
int DFRobot_SIMclient::checkSignalQuality(void)
{
char signalBuffer[26];
int i = 0,j = 0,k = 0;
char *signalQuality;
SIMcore.cleanBuffer(signalBuffer,26);
SIMcore.send_cmd("AT+CSQ\r\n");
SIMcore.readBuffer(signalBuffer,26);
if (NULL != (signalQuality = strstr(signalBuffer, "+CSQ:"))){
i = *(signalQuality + 6) - 48;
j = *(signalQuality + 7) - 48;
k = (i * 10) + j;
}else{
SIMcore.closeCommand();
return 0;
}
if( k == 99){
SIMcore.closeCommand();
return 0;
}else{
SIMcore.setCommandCounter(1);
return k;
}
}
bool DFRobot_SIMclient::initNet(void)
{
int count = 0;
while(count < 3){
if(SIMcore.check_send_cmd("AT+CGATT?\r\n","1")){
break;
}else{
count++;
delay(500);
}
}
if(count == 3){
SIMcore.closeCommand();
return false;
}
if(SIMcore.check_send_cmd("AT+CSTT\r\n","OK")){
if(SIMcore.check_send_cmd("AT+CIICR\r\n","OK")){
SIMcore.setCommandCounter(2);
return true;
}else{
SIMcore.closeCommand();
return false;
}
}else{
SIMcore.closeCommand();
return false;
}
}
bool DFRobot_SIMclient::connect(char *server,Protocol ptl,int port)
{
if(SIMcore.getCommandCounter() == 2){
char num[4];
char serverIP[100];
SIMcore.cleanBuffer(serverIP,100);
itoa(port, num, 10);
SIMcore.send_cmd("AT+CIPSTART=\"");
if(ptl == TCP){
SIMcore.send_cmd("TCP\",\"");
}else if(ptl == UDP){
SIMcore.send_cmd("UDP\",\"");
}else{
Serial.println("Wrong protocol");
return false;
}
SIMcore.send_cmd(server);
SIMcore.send_cmd("\",");
SIMcore.send_cmd(num);
SIMcore.send_cmd("\r\n");
while(1){
while(SIMcore.checkReadable()){
SIMcore.readBuffer(serverIP,100);
if(NULL != strstr(serverIP,"ALREADY CONNECT")){
SIMcore.setCommandCounter(3);
return true;
}
if(NULL != strstr(serverIP,"CONNECT OK")){
SIMcore.setCommandCounter(3);
return true;
}
if(NULL != strstr(serverIP,"CONNECT FAIL")){
return false;
}
}
}
}else{
return false;
}
}
bool DFRobot_SIMclient::send(char *data){
if(SIMcore.getCommandCounter() == 3){
char num[4];
char resp[20];
int len = strlen(data);
itoa(len, num, 10);
SIMcore.send_cmd("AT+CIPSEND=");
SIMcore.send_cmd(num);
if(SIMcore.check_send_cmd("\r\n",">")){
SIMcore.send_cmd(data);
while(1){
while(SIMcore.checkReadable()){
SIMcore.readBuffer(resp,20);
if(NULL != strstr(resp,"OK")){
return true;
}
if(NULL != strstr(resp,"ERROR")){
return false;
}
}
}
}else{
SIMcore.closeCommand();
return false;
}
}else{
return false;
}
}
bool DFRobot_SIMclient::send(void *buffer,size_t len)
{
if(SIMcore.getCommandCounter() == 3){
char num[4];
itoa(len, num, 10);
SIMcore.send_cmd("AT+CIPSEND=");
SIMcore.send_cmd(num);
if(SIMcore.check_send_cmd("\r\n",">")){
if(SIMcore.check_send_cmd(buffer,"OK")){
return true;
}else{
SIMcore.setCommandCounter(3);
return false;
}
}else{
SIMcore.setCommandCounter(3);
return false;
}
}else{
return false;
}
}
bool DFRobot_SIMclient::close(void)
{
if(SIMcore.getCommandCounter() == 3){
if(SIMcore.check_send_cmd("AT+CIPSHUT\r\n","OK")){
SIMcore.setCommandCounter(2);
return true;
}else{
SIMcore.setCommandCounter(2);
return false;
}
}else{
return false;
}
}