Skip to content

Commit 072ea5c

Browse files
committed
Extract GPRS and common functions
1 parent 79a2a46 commit 072ea5c

12 files changed

Lines changed: 330 additions & 52 deletions

File tree

Ftp.cpp

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -62,54 +62,40 @@ Result FTP::putBegin(const char *apn,
6262
const char *pass,
6363
const char *path)
6464
{
65-
Result result = openGPRSContext(*this, apn);
65+
Result result = openGPRSContext(this, apn);
6666

6767
char buffer[64];
6868
char tmp[24];
6969

7070
delay(10000);
7171
if (sendCmdAndWaitForResp_P(AT_FTPCID, OK, 2000) == FALSE)
72-
{
7372
return ERROR_FTPCID;
74-
}
7573

7674
strcpy_P(tmp, server);
7775
sprintf_P(buffer, AT_FTPSERV, tmp);
7876
if (sendCmdAndWaitForResp(buffer, OK, 2000) == FALSE)
79-
{
8077
return ERROR_FTPSERV;
81-
}
8278

8379
strcpy_P(tmp, usr);
8480
sprintf_P(buffer, AT_FTPUN, tmp);
8581
if (sendCmdAndWaitForResp(buffer, OK_, 2000) == FALSE)
86-
{
8782
return ERROR_FTPUN;
88-
}
8983

9084
strcpy_P(tmp, pass);
9185
sprintf_P(buffer, AT_FTPPW, tmp);
9286
if (sendCmdAndWaitForResp(buffer, OK_, 2000) == FALSE)
93-
{
9487
return ERROR_FTPPW;
95-
}
9688

9789
sprintf_P(buffer, AT_FTPPUTNAME, fileName);
9890
if (sendCmdAndWaitForResp(buffer, OK_, 2000) == FALSE)
99-
{
10091
return ERROR_FTPPUTNAME;
101-
}
10292

10393
sprintf_P(buffer, AT_FTPPUTPATH, path);
10494
if (sendCmdAndWaitForResp(buffer, OK_, 2000) == FALSE)
105-
{
10695
return ERROR_FTPPUTPATH;
107-
}
10896

10997
if (sendCmdAndWaitForResp_P(AT_FTPPUT1, AT_FTPPUT1_RESP, 10000) == FALSE)
110-
{
11198
return ERROR_FTPPUT1;
112-
}
11399

114100
return result;
115101
}
@@ -125,9 +111,7 @@ Result FTP::putWrite(const char *data, unsigned int size)
125111
{
126112
attempts++;
127113
if (attempts == MAX_ATTEMPTS)
128-
{
129114
return ERROR_FTPPUT11;
130-
}
131115
}
132116

133117
return result;
@@ -137,42 +121,37 @@ Result FTP::putWriteStart(unsigned int size)
137121
{
138122
Result result = SUCCESS;
139123

140-
char buffer[32];
141-
char resp[32];
124+
char buffer[22];
125+
char resp[11];
142126

143127
sprintf_P(buffer, AT_FTPPUT2, size);
144128
strcpy_P(resp, AT_FTPPUT2_RESP);
145129
if (sendCmdAndWaitForResp(buffer, resp, 2000) == FALSE)
146-
{
147130
return ERROR_FTPPUT2;
148-
}
149131

150132
return result;
151133
}
152134

153135
Result FTP::putWriteEnd(const char *data, unsigned int size)
154136
{
155137
Result result = SUCCESS;
156-
char resp[8];
138+
char resp[4];
157139

158140
write(data, size);
159-
141+
160142
strcpy_P(resp, AT_FTPPUT1_RESP);
161143
if (waitForResp(resp, 2000) == FALSE)
162-
{
163144
return ERROR_FTPPUT11;
164-
}
165145

166146
return result;
167147
}
168148

169149
Result FTP::putEnd()
170150
{
171-
Result result = closeGPRSContext(*this);
151+
Result result = closeGPRSContext(this);
172152

173153
if (sendCmdAndWaitForResp_P(AT_FTPPUT20, AT_FTPPUT20_RESP, 2000) == FALSE)
174-
{
175154
return ERROR_FTPPUT20;
176-
}
155+
177156
return result;
178157
}

GPRS.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,43 +44,43 @@ const char BEARER_OPEN[] PROGMEM = "+SAPBR: 1,1";
4444
const char OK[] PROGMEM = "OK";
4545
const char OK_ PROGMEM = "OK";
4646

47-
Result openGPRSContext(SIM800& sim800, const char *apn)
47+
Result openGPRSContext(SIM800 *sim800, const char *apn)
4848
{
4949
Result result = SUCCESS;
5050
uint8_t attempts = 0;
5151
uint8_t MAX_ATTEMPTS = 10;
5252

53-
sim800.sendATTest();
53+
sim800->sendATTest();
5454

55-
while ((sim800.sendCmdAndWaitForResp_P(REGISTRATION_STATUS, CONNECTED, 2000) != TRUE &&
56-
sim800.sendCmdAndWaitForResp_P(REGISTRATION_STATUS, ROAMING, 2000) != TRUE) &&
55+
while ((sim800->sendCmdAndWaitForResp_P(REGISTRATION_STATUS, CONNECTED, 2000) != TRUE &&
56+
sim800->sendCmdAndWaitForResp_P(REGISTRATION_STATUS, ROAMING, 2000) != TRUE) &&
5757
attempts < MAX_ATTEMPTS)
5858
{
59-
sim800.sendCmdAndWaitForResp_P(READ_VOLTAGE, OK, 1000);
60-
sim800.sendCmdAndWaitForResp_P(SIGNAL_QUALITY, OK, 1000);
59+
sim800->sendCmdAndWaitForResp_P(READ_VOLTAGE, OK, 1000);
60+
sim800->sendCmdAndWaitForResp_P(SIGNAL_QUALITY, OK, 1000);
6161
attempts++;
6262
delay(1000 * attempts);
6363
if (attempts == MAX_ATTEMPTS)
6464
{
6565
attempts = 0;
66-
sim800.preInit();
66+
sim800->preInit();
6767
}
6868
}
6969

70-
if (sim800.sendCmdAndWaitForResp_P(BEARER_PROFILE_GPRS, OK, 2000) == FALSE)
70+
if (sim800->sendCmdAndWaitForResp_P(BEARER_PROFILE_GPRS, OK, 2000) == FALSE)
7171
result = ERROR_BEARER_PROFILE_GPRS;
7272

7373
char httpApn[64];
7474
char tmp[24];
7575
strcpy_P(tmp, apn);
7676
sprintf_P(httpApn, BEARER_PROFILE_APN, tmp);
77-
if (sim800.sendCmdAndWaitForResp(httpApn, OK_, 2000) == FALSE)
77+
if (sim800->sendCmdAndWaitForResp(httpApn, OK_, 2000) == FALSE)
7878
result = ERROR_BEARER_PROFILE_APN;
7979

80-
while (sim800.sendCmdAndWaitForResp_P(QUERY_BEARER, BEARER_OPEN, 2000) == FALSE && attempts < MAX_ATTEMPTS)
80+
while (sim800->sendCmdAndWaitForResp_P(QUERY_BEARER, BEARER_OPEN, 2000) == FALSE && attempts < MAX_ATTEMPTS)
8181
{
8282
attempts++;
83-
if (sim800.sendCmdAndWaitForResp_P(OPEN_GPRS_CONTEXT, OK, 2000) == FALSE)
83+
if (sim800->sendCmdAndWaitForResp_P(OPEN_GPRS_CONTEXT, OK, 2000) == FALSE)
8484
{
8585
result = ERROR_OPEN_GPRS_CONTEXT;
8686
}
@@ -93,11 +93,11 @@ Result openGPRSContext(SIM800& sim800, const char *apn)
9393
return result;
9494
}
9595

96-
Result closeGPRSContext(SIM800& sim800)
96+
Result closeGPRSContext(SIM800 *sim800)
9797
{
9898
Result result = SUCCESS;
9999

100-
if (sim800.sendCmdAndWaitForResp_P(CLOSE_GPRS_CONTEXT, OK, 2000) == FALSE)
100+
if (sim800->sendCmdAndWaitForResp_P(CLOSE_GPRS_CONTEXT, OK, 2000) == FALSE)
101101
result = ERROR_CLOSE_GPRS_CONTEXT;
102102

103103
return result;

GPRS.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#include "Result.h"
3232
#include "Sim800.h"
3333

34-
Result openGPRSContext(SIM800& sim800, const char *apn);
35-
Result closeGPRSContext(SIM800& sim800);
34+
Result openGPRSContext(SIM800 *sim800, const char *apn);
35+
Result closeGPRSContext(SIM800 *sim800);
3636

3737
#endif

Http.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const char HTTPS_DISABLE[] PROGMEM = "AT+HTTPSSL=0\r\n";
4242
const char NORMAL_MODE[] PROGMEM = "AT+CFUN=1,1\r\n";
4343
const char SIGNAL_QUALITY[] PROGMEM = "AT+CSQ\r\n";
4444
const char READ_VOLTAGE[] PROGMEM = "AT+CBC\r\n";
45+
4546
const char OK[] PROGMEM = "OK";
4647
const char DOWNLOAD[] PROGMEM = "DOWNLOAD";
4748
const char HTTP_2XX[] PROGMEM = ",2XX,";
@@ -52,7 +53,7 @@ const char OK_[] = "OK";
5253

5354
Result HTTP::connect(const char *apn)
5455
{
55-
Result result = openGPRSContext(*this, apn);
56+
Result result = openGPRSContext(this, apn);
5657

5758
if (sendCmdAndWaitForResp_P(HTTP_INIT, OK, 2000) == FALSE)
5859
result = ERROR_HTTP_INIT;
@@ -62,7 +63,7 @@ Result HTTP::connect(const char *apn)
6263

6364
Result HTTP::disconnect()
6465
{
65-
Result result = closeGPRSContext(*this);;
66+
Result result = closeGPRSContext(this);
6667

6768
if (sendCmdAndWaitForResp_P(HTTP_CLOSE, OK, 2000) == FALSE)
6869
result = ERROR_HTTP_CLOSE;
@@ -109,7 +110,7 @@ Result HTTP::get(const char *uri, char *response)
109110
Result result = setHTTPSession(uri);
110111
char buffer[16];
111112
char resp[16];
112-
113+
113114
if (sendCmdAndWaitForResp_P(HTTP_GET, HTTP_2XX, 2000) == TRUE)
114115
{
115116
strcpy_P(buffer, HTTP_READ);

Sim800.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ int SIM800::waitForResp(const char *resp, unsigned int timeout)
124124
char c = serialSIM800.read();
125125
if (debugMode)
126126
Serial.print(c);
127+
127128
sum = (c == resp[sum] || resp[sum] == 'X') ? sum + 1 : 0;
128129
if (sum == len)
129130
break;
@@ -160,7 +161,7 @@ int SIM800::sendCmdAndWaitForResp_P(const char *cmd, const char *resp, unsigned
160161
char respBuff[32];
161162
strcpy_P(cmdBuff, cmd);
162163
strcpy_P(respBuff, resp);
163-
164+
164165
return sendCmdAndWaitForResp(cmdBuff, respBuff, timeout);
165166
}
166167

@@ -199,18 +200,17 @@ void SIM800::write(const char *data, unsigned int size)
199200

200201
void SIM800::sleep(bool force)
201202
{
202-
char buffer[16];
203203
if (force)
204204
{
205205
sendCmdAndWaitForResp_P(SLEEP_MODE_1, OK, 2000);
206206
}
207207
else
208208
{
209-
sendCmdAndWaitForResp_P(buffer, OK, 2000);
209+
sendCmdAndWaitForResp_P(SLEEP_MODE_2, OK, 2000);
210210
}
211211
}
212212

213213
void SIM800::wakeUp()
214214
{
215-
preInit();
215+
preInit();
216216
}

Sim800.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#ifndef __SIM800_H__
3030
#define __SIM800_H__
3131

32-
#include "Arduino.h"
32+
#include <Arduino.h>
3333
#include <SoftwareSerial.h>
3434

3535
#define TRUE 1
@@ -39,6 +39,7 @@
3939
/** SIM800 class.
4040
* Used for SIM800 communication. attention that SIM800 module communicate with MCU in serial protocol
4141
*/
42+
4243
class SIM800
4344
{
4445

@@ -47,8 +48,13 @@ class SIM800
4748
* @param baudRate baud rate of uart communication
4849
* @param rxPin uart receive pin to communicate with SIM800
4950
* @param txPin uart transmit pin to communicate with SIM800
51+
* @param debug indicates if print the AT command sequence
5052
*/
51-
SIM800(unsigned int baudRate, unsigned int rxPin, unsigned int txPin, unsigned int rstPin, bool debug) : serialSIM800(txPin, rxPin)
53+
SIM800(unsigned int baudRate,
54+
unsigned int rxPin,
55+
unsigned int txPin,
56+
unsigned int rstPin,
57+
bool debug) : serialSIM800(txPin, rxPin)
5258
{
5359
serialSIM800.begin(baudRate);
5460
debugMode = debug;

0 commit comments

Comments
 (0)