Skip to content

Commit 79a2a46

Browse files
committed
Extract GPRS functins and fix some commands
1 parent 0aca589 commit 79a2a46

9 files changed

Lines changed: 179 additions & 176 deletions

File tree

Ftp.cpp

Lines changed: 25 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
*/
2727

2828
#include "Ftp.h"
29-
#include <string.h>
3029

3130
const char AT_FTPCID[] PROGMEM = "AT+FTPCID=1\r\n";
3231
const char AT_FTPSERV[] PROGMEM = "AT+FTPSERV=\"%s\"\r\n";
@@ -46,135 +45,68 @@ const char REGISTRATION_STATUS[] PROGMEM = "AT+CREG?\r\n";
4645
const char SLEEP_MODE_2[] PROGMEM = "AT+CSCLK=2\r\n";
4746

4847
const char OK[] PROGMEM = "OK";
48+
const char OK_[] = "OK";
4949
const char AT_FTPPUT1_RESP[] PROGMEM = "1,1";
5050
const char AT_FTPPUT2_RESP[] PROGMEM = "+FTPPUT: 2";
5151
const char AT_FTPPUT20_RESP[] PROGMEM = "1,0";
5252
const char CONNECTED[] PROGMEM = "+CREG: 0,1";
5353
const char ROAMING[] PROGMEM = "+CREG: 0,5";
5454
const char BEARER_OPEN[] PROGMEM = "+SAPBR: 1,1";
5555

56-
Result FTP::configureBearer(const char *apn)
57-
{
58-
Result result = SUCCESS;
59-
char buffer[64];
60-
char resp1[12];
61-
char resp2[12];
62-
unsigned int attempts = 0;
63-
unsigned int MAX_ATTEMPTS = 10;
64-
65-
sendATTest();
66-
67-
strcpy_P(buffer, REGISTRATION_STATUS);
68-
strcpy_P(resp1, CONNECTED);
69-
strcpy_P(resp2, ROAMING);
70-
while ((sendCmdAndWaitForResp(buffer, resp1, 2000) != TRUE &&
71-
sendCmdAndWaitForResp(buffer, resp2, 2000) != TRUE) &&
72-
attempts < MAX_ATTEMPTS)
73-
{
74-
attempts++;
75-
delay(1000 * attempts);
76-
if (attempts == MAX_ATTEMPTS)
77-
{
78-
attempts = 0;
79-
preInit();
80-
}
81-
}
82-
attempts = 0;
83-
84-
strcpy_P(buffer, BEARER_PROFILE_GPRS);
85-
strcpy_P(resp1, OK);
86-
if (sendCmdAndWaitForResp(buffer, resp1, 2000) == FALSE)
87-
result = ERROR_BEARER_PROFILE_GPRS;
88-
89-
sprintf_P(buffer, BEARER_PROFILE_APN, apn);
90-
strcpy_P(resp1, OK);
91-
if (sendCmdAndWaitForResp(buffer, resp1, 2000) == FALSE)
92-
result = ERROR_BEARER_PROFILE_APN;
93-
94-
strcpy_P(buffer, QUERY_BEARER);
95-
strcpy_P(resp1, BEARER_OPEN);
96-
while (sendCmdAndWaitForResp(buffer, resp1, 2000) == FALSE && attempts < MAX_ATTEMPTS)
97-
{
98-
attempts++;
99-
if (attempts == MAX_ATTEMPTS)
100-
{
101-
result = ERROR_OPEN_GPRS_CONTEXT;
102-
}
103-
}
104-
105-
attempts = 0;
106-
107-
strcpy_P(buffer, OPEN_GPRS_CONTEXT);
108-
strcpy_P(resp1, OK);
109-
while (sendCmdAndWaitForResp(buffer, resp1, 2000) == FALSE && attempts < MAX_ATTEMPTS)
110-
{
111-
attempts++;
112-
if (attempts == MAX_ATTEMPTS)
113-
{
114-
result = ERROR_QUERY_GPRS_CONTEXT;
115-
}
116-
}
56+
#include "GPRS.h"
11757

118-
return result;
119-
}
120-
121-
Result FTP::putBegin(const char *fileName,
58+
Result FTP::putBegin(const char *apn,
59+
const char *fileName,
12260
const char *server,
12361
const char *usr,
12462
const char *pass,
12563
const char *path)
12664
{
127-
Result result = SUCCESS;
65+
Result result = openGPRSContext(*this, apn);
12866

12967
char buffer[64];
130-
char resp[12];
68+
char tmp[24];
13169

13270
delay(10000);
133-
strcpy_P(buffer, AT_FTPCID);
134-
strcpy_P(resp, OK);
135-
if (sendCmdAndWaitForResp(buffer, resp, 2000) == FALSE)
71+
if (sendCmdAndWaitForResp_P(AT_FTPCID, OK, 2000) == FALSE)
13672
{
13773
return ERROR_FTPCID;
13874
}
13975

140-
sprintf_P(buffer, AT_FTPSERV, server);
141-
strcpy_P(resp, OK);
142-
if (sendCmdAndWaitForResp(buffer, resp, 2000) == FALSE)
76+
strcpy_P(tmp, server);
77+
sprintf_P(buffer, AT_FTPSERV, tmp);
78+
if (sendCmdAndWaitForResp(buffer, OK, 2000) == FALSE)
14379
{
14480
return ERROR_FTPSERV;
14581
}
14682

147-
sprintf_P(buffer, AT_FTPUN, usr);
148-
strcpy_P(resp, OK);
149-
if (sendCmdAndWaitForResp(buffer, resp, 2000) == FALSE)
83+
strcpy_P(tmp, usr);
84+
sprintf_P(buffer, AT_FTPUN, tmp);
85+
if (sendCmdAndWaitForResp(buffer, OK_, 2000) == FALSE)
15086
{
15187
return ERROR_FTPUN;
15288
}
15389

154-
sprintf_P(buffer, AT_FTPPW, pass);
155-
strcpy_P(resp, OK);
156-
if (sendCmdAndWaitForResp(buffer, resp, 2000) == FALSE)
90+
strcpy_P(tmp, pass);
91+
sprintf_P(buffer, AT_FTPPW, tmp);
92+
if (sendCmdAndWaitForResp(buffer, OK_, 2000) == FALSE)
15793
{
15894
return ERROR_FTPPW;
15995
}
16096

16197
sprintf_P(buffer, AT_FTPPUTNAME, fileName);
162-
strcpy_P(resp, OK);
163-
if (sendCmdAndWaitForResp(buffer, resp, 2000) == FALSE)
98+
if (sendCmdAndWaitForResp(buffer, OK_, 2000) == FALSE)
16499
{
165100
return ERROR_FTPPUTNAME;
166101
}
167102

168103
sprintf_P(buffer, AT_FTPPUTPATH, path);
169-
strcpy_P(resp, OK);
170-
if (sendCmdAndWaitForResp(buffer, resp, 2000) == FALSE)
104+
if (sendCmdAndWaitForResp(buffer, OK_, 2000) == FALSE)
171105
{
172106
return ERROR_FTPPUTPATH;
173107
}
174108

175-
strcpy_P(buffer, AT_FTPPUT1);
176-
strcpy_P(resp, AT_FTPPUT1_RESP);
177-
if (sendCmdAndWaitForResp(buffer, resp, 10000) == FALSE)
109+
if (sendCmdAndWaitForResp_P(AT_FTPPUT1, AT_FTPPUT1_RESP, 10000) == FALSE)
178110
{
179111
return ERROR_FTPPUT1;
180112
}
@@ -186,8 +118,8 @@ Result FTP::putWrite(const char *data, unsigned int size)
186118
{
187119
Result result = SUCCESS;
188120

189-
unsigned int attempts = 0;
190-
unsigned int MAX_ATTEMPTS = 10;
121+
uint8_t attempts = 0;
122+
uint8_t MAX_ATTEMPTS = 10;
191123

192124
while (putWriteStart(size) != SUCCESS || putWriteEnd(data, size) != SUCCESS)
193125
{
@@ -221,12 +153,10 @@ Result FTP::putWriteStart(unsigned int size)
221153
Result FTP::putWriteEnd(const char *data, unsigned int size)
222154
{
223155
Result result = SUCCESS;
224-
225-
char buffer[32];
226-
char resp[32];
156+
char resp[8];
227157

228158
write(data, size);
229-
159+
230160
strcpy_P(resp, AT_FTPPUT1_RESP);
231161
if (waitForResp(resp, 2000) == FALSE)
232162
{
@@ -238,15 +168,9 @@ Result FTP::putWriteEnd(const char *data, unsigned int size)
238168

239169
Result FTP::putEnd()
240170
{
241-
Result result = SUCCESS;
242-
serialSIM800.flush();
243-
244-
char buffer[32];
245-
char resp[12];
171+
Result result = closeGPRSContext(*this);
246172

247-
strcpy_P(buffer, AT_FTPPUT20);
248-
strcpy_P(resp, AT_FTPPUT20_RESP);
249-
if (sendCmdAndWaitForResp(AT_FTPPUT20, AT_FTPPUT20_RESP, 2000) == FALSE)
173+
if (sendCmdAndWaitForResp_P(AT_FTPPUT20, AT_FTPPUT20_RESP, 2000) == FALSE)
250174
{
251175
return ERROR_FTPPUT20;
252176
}

Ftp.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ class FTP : public SIM800
4040
unsigned int txPin,
4141
unsigned int rstPin,
4242
bool debug = TRUE) : SIM800(baudRate, rxPin, txPin, rstPin, debug){};
43-
Result configureBearer(const char *apn);
44-
Result putBegin(const char *fileName,
43+
44+
Result putBegin(const char *apn,
45+
const char *fileName,
4546
const char *server,
4647
const char *usr,
4748
const char *pass,

GPRS.cpp

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* GPRS.cpp
3+
* GPRS module that implements the basic AT sequences to interact with GPRS
4+
*
5+
* Copyright 2019 Antonio Carrasco
6+
*
7+
* The MIT License (MIT)
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy
10+
* of this software and associated documentation files (the "Software"), to deal
11+
* in the Software without restriction, including without limitation the rights
12+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
* copies of the Software, and to permit persons to whom the Software is
14+
* furnished to do so, subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included in
17+
* all copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#include "GPRS.h"
29+
#include <string.h>
30+
31+
const char BEARER_PROFILE_GPRS[] PROGMEM = "AT+SAPBR=3,1,\"Contype\",\"GPRS\"\r\n";
32+
const char BEARER_PROFILE_APN[] PROGMEM = "AT+SAPBR=3,1,\"APN\",\"%s\"\r\n";
33+
const char QUERY_BEARER[] PROGMEM = "AT+SAPBR=2,1\r\n";
34+
const char OPEN_GPRS_CONTEXT[] PROGMEM = "AT+SAPBR=1,1\r\n";
35+
const char CLOSE_GPRS_CONTEXT[] PROGMEM = "AT+SAPBR=0,1\r\n";
36+
37+
const char SIGNAL_QUALITY[] PROGMEM = "AT+CSQ\r\n";
38+
const char READ_VOLTAGE[] PROGMEM = "AT+CBC\r\n";
39+
const char REGISTRATION_STATUS[] PROGMEM = "AT+CREG?\r\n";
40+
41+
const char CONNECTED[] PROGMEM = "+CREG: 0,1";
42+
const char ROAMING[] PROGMEM = "+CREG: 0,5";
43+
const char BEARER_OPEN[] PROGMEM = "+SAPBR: 1,1";
44+
const char OK[] PROGMEM = "OK";
45+
const char OK_ PROGMEM = "OK";
46+
47+
Result openGPRSContext(SIM800& sim800, const char *apn)
48+
{
49+
Result result = SUCCESS;
50+
uint8_t attempts = 0;
51+
uint8_t MAX_ATTEMPTS = 10;
52+
53+
sim800.sendATTest();
54+
55+
while ((sim800.sendCmdAndWaitForResp_P(REGISTRATION_STATUS, CONNECTED, 2000) != TRUE &&
56+
sim800.sendCmdAndWaitForResp_P(REGISTRATION_STATUS, ROAMING, 2000) != TRUE) &&
57+
attempts < MAX_ATTEMPTS)
58+
{
59+
sim800.sendCmdAndWaitForResp_P(READ_VOLTAGE, OK, 1000);
60+
sim800.sendCmdAndWaitForResp_P(SIGNAL_QUALITY, OK, 1000);
61+
attempts++;
62+
delay(1000 * attempts);
63+
if (attempts == MAX_ATTEMPTS)
64+
{
65+
attempts = 0;
66+
sim800.preInit();
67+
}
68+
}
69+
70+
if (sim800.sendCmdAndWaitForResp_P(BEARER_PROFILE_GPRS, OK, 2000) == FALSE)
71+
result = ERROR_BEARER_PROFILE_GPRS;
72+
73+
char httpApn[64];
74+
char tmp[24];
75+
strcpy_P(tmp, apn);
76+
sprintf_P(httpApn, BEARER_PROFILE_APN, tmp);
77+
if (sim800.sendCmdAndWaitForResp(httpApn, OK_, 2000) == FALSE)
78+
result = ERROR_BEARER_PROFILE_APN;
79+
80+
while (sim800.sendCmdAndWaitForResp_P(QUERY_BEARER, BEARER_OPEN, 2000) == FALSE && attempts < MAX_ATTEMPTS)
81+
{
82+
attempts++;
83+
if (sim800.sendCmdAndWaitForResp_P(OPEN_GPRS_CONTEXT, OK, 2000) == FALSE)
84+
{
85+
result = ERROR_OPEN_GPRS_CONTEXT;
86+
}
87+
else
88+
{
89+
result = SUCCESS;
90+
}
91+
}
92+
93+
return result;
94+
}
95+
96+
Result closeGPRSContext(SIM800& sim800)
97+
{
98+
Result result = SUCCESS;
99+
100+
if (sim800.sendCmdAndWaitForResp_P(CLOSE_GPRS_CONTEXT, OK, 2000) == FALSE)
101+
result = ERROR_CLOSE_GPRS_CONTEXT;
102+
103+
return result;
104+
}

GPRS.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* GPRS.h
3+
* GPRS module that implements the basic AT sequences to interact with GPRS
4+
*
5+
* Copyright 2019 Antonio Carrasco
6+
*
7+
* The MIT License (MIT)
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy
10+
* of this software and associated documentation files (the "Software"), to deal
11+
* in the Software without restriction, including without limitation the rights
12+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
* copies of the Software, and to permit persons to whom the Software is
14+
* furnished to do so, subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included in
17+
* all copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#ifndef __GPRS_H__
29+
#define __GPRS_H__
30+
31+
#include "Result.h"
32+
#include "Sim800.h"
33+
34+
Result openGPRSContext(SIM800& sim800, const char *apn);
35+
Result closeGPRSContext(SIM800& sim800);
36+
37+
#endif

Geo.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
#include "Geo.h"
2929
#include "Parser.h"
30-
#include <string.h>
3130

3231
#define READ_GPS "AT+CIPGSMLOC=1,1\r\n"
3332

0 commit comments

Comments
 (0)