Skip to content

Commit acc5be1

Browse files
committed
Use PROGREM to efficiently store strings
1 parent 07fa8ab commit acc5be1

4 files changed

Lines changed: 97 additions & 23 deletions

File tree

Http.cpp

Lines changed: 76 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,22 @@
5959
#define ROAMING "+CREG: 0,5"
6060
#define BEARER_OPEN "+SAPBR: 1,1"
6161

62+
// FTP
63+
const char AT_FTPCID[] PROGMEM = "AT+FTPCID=1\r\n";
64+
const char AT_FTPSERV[] PROGMEM = "AT+FTPSERV=\"%s\"\r\n";
65+
const char AT_FTPUN[] PROGMEM = "AT+FTPUN=\"%s\"\r\n";
66+
const char AT_FTPPW[] PROGMEM = "AT+FTPPW=\"%s\"\r\n";
67+
const char AT_FTPPUTNAME[] PROGMEM = "AT+FTPPUTNAME=\"%s\"\r\n";
68+
const char AT_FTPPUTPATH[] PROGMEM = "AT+FTPPUTPATH=\"%s\"\r\n";
69+
const char AT_FTPPUT1[] PROGMEM = "AT+FTPPUT=1\r\n";
70+
const char AT_FTPPUT2[] PROGMEM = "AT+FTPPUT=2,%d\r\n";
71+
const char AT_FTPPUT20[] PROGMEM = "AT+FTPPUT=2,0\r\n";
72+
73+
const char OK_1[] PROGMEM = "OK\r\n";
74+
const char AT_FTPPUT1_RESP[] PROGMEM = "1,1";
75+
const char AT_FTPPUT2_RESP[] PROGMEM = "+FTPPUT: 2";
76+
const char AT_FTPPUT20_RESP[] PROGMEM = "1,0";
77+
6278
Result HTTP::configureBearer(const char *apn){
6379

6480
Result result = SUCCESS;
@@ -176,47 +192,86 @@ Result HTTP::putBegin(const char *fileName,
176192
Result result;
177193

178194
char buffer[64];
195+
char resp[12];
179196

180-
sendCmdAndWaitForResp("AT+FTPCID=1\r\n", OK, 2000);
181-
182-
sprintf(buffer, "AT+FTPSERV=\"%s\"\r\n", server);
183-
sendCmdAndWaitForResp(buffer, OK, 2000);
197+
strcpy_P(buffer, AT_FTPCID);
198+
strcpy_P(resp, OK_1);
199+
if(sendCmdAndWaitForResp(buffer, resp, 20000) == FALSE){
200+
return ERROR_FTPCID;
201+
}
184202

185-
//sendCmdAndWaitForResp("AT+FTPPORT=21\r\n", OK, 2000);
186-
//sendCmdAndWaitForResp("AT+FTPSSL=2\r\n", OK, 2000);
203+
sprintf_P(buffer, AT_FTPSERV, server);
204+
strcpy_P(resp, OK_1);
205+
if(sendCmdAndWaitForResp(buffer, resp, 20000) == FALSE){
206+
return ERROR_FTPSERV;
207+
}
187208

188-
sprintf(buffer, "AT+FTPUN=\"%s\"\r\n", usr);
189-
sendCmdAndWaitForResp(buffer, OK, 20000);
209+
sprintf_P(buffer, AT_FTPUN, usr);
210+
strcpy_P(resp, OK_1);
211+
if(sendCmdAndWaitForResp(buffer, resp, 20000) == FALSE){
212+
return ERROR_FTPUN;
213+
}
190214

191-
sprintf(buffer, "AT+FTPPW=\"%s\"\r\n", pass);
192-
sendCmdAndWaitForResp(buffer, OK, 20000);
215+
sprintf_P(buffer, AT_FTPPW, pass);
216+
strcpy_P(resp, OK_1);
217+
if(sendCmdAndWaitForResp(buffer, resp, 20000) == FALSE){
218+
return ERROR_FTPPW;
219+
}
193220

194-
sprintf(buffer, "AT+FTPPUTNAME=\"%s\"\r\n", fileName);
195-
sendCmdAndWaitForResp(buffer, OK, 20000);
221+
sprintf_P(buffer, AT_FTPPUTNAME, fileName);
222+
strcpy_P(resp, OK_1);
223+
if(sendCmdAndWaitForResp(buffer, resp, 20000) == FALSE){
224+
return ERROR_FTPPUTNAME;
225+
}
196226

197-
sprintf(buffer, "AT+FTPPUTPATH=\"%s\"\r\n", path);
198-
sendCmdAndWaitForResp(buffer, OK, 20000);
199-
sendCmdAndWaitForResp("AT+FTPPUT=1\r\n", "1,1", 20000);
227+
sprintf_P(buffer, AT_FTPPUTPATH, path);
228+
strcpy_P(resp, OK_1);
229+
if(sendCmdAndWaitForResp(buffer, resp, 20000) == FALSE){
230+
return ERROR_FTPPUTPATH;
231+
}
232+
233+
strcpy_P(buffer, AT_FTPPUT1);
234+
strcpy_P(resp, AT_FTPPUT1_RESP);
235+
if(sendCmdAndWaitForResp(buffer, resp, 20000) == FALSE){
236+
return ERROR_FTPPUT1;
237+
}
200238

201239
return result;
202240
}
203241

204242
Result HTTP::putWrite(const char *data, unsigned int size){
205243
Result result;
206244

207-
char ftpUpload[32];
208-
sprintf(ftpUpload, "AT+FTPPUT=2,%d\r\n", size);
209-
sendCmdAndWaitForResp(ftpUpload, "+FTPPUT: 2", 20000);
245+
char buffer[32];
246+
char resp[32];
247+
248+
sprintf_P(buffer, AT_FTPPUT2, size);
249+
strcpy_P(resp, AT_FTPPUT2_RESP);
250+
if(sendCmdAndWaitForResp(buffer, resp, 2000) == FALSE){
251+
return ERROR_FTPPUT2;
252+
}
253+
else {
254+
write(data, size);
255+
256+
strcpy_P(resp, AT_FTPPUT1_RESP);
257+
waitForResp(resp, 2000);
258+
}
210259

211-
write(data, size);
212-
waitForResp("1,1", 20000);
213260
return result;
214261
}
215262

216263
Result HTTP::putEnd(){
217264
Result result;
218265
serialSIM800.flush();
219-
sendCmdAndWaitForResp("AT+FTPPUT=2,0\r\n", "1,0", 20000);
266+
267+
char buffer[32];
268+
char resp[12];
269+
270+
strcpy_P(buffer, AT_FTPPUT20);
271+
strcpy_P(resp, AT_FTPPUT20_RESP);
272+
if(sendCmdAndWaitForResp(AT_FTPPUT20, AT_FTPPUT20_RESP, 2000) == FALSE){
273+
return ERROR_FTPPUT20;
274+
}
220275
return result;
221276
}
222277

Http.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,17 @@ enum Result {
5151
ERROR_NORMAL_MODE = 16,
5252
ERROR_LOW_CONSUMPTION_MODE = 17,
5353
ERROR_HTTPS_ENABLE = 18,
54-
ERROR_HTTPS_DISABLE = 19
54+
ERROR_HTTPS_DISABLE = 19,
55+
// FTP
56+
ERROR_FTPCID = 20,
57+
ERROR_FTPSERV = 21,
58+
ERROR_FTPUN = 22,
59+
ERROR_FTPPW = 23,
60+
ERROR_FTPPUTNAME = 24,
61+
ERROR_FTPPUTPATH = 25,
62+
ERROR_FTPPUT1 = 26,
63+
ERROR_FTPPUT2 = 27,
64+
ERROR_FTPPUT20 = 28,
5565
};
5666

5767

Sim800.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,15 @@ int SIM800::sendCmdAndWaitForResp(const char* cmd, const char *resp, unsigned ti
136136
return waitForResp(resp,timeout);
137137
}
138138

139+
int SIM800::sendCmdAndWaitForResp2(const char* cmd, const char *resp, unsigned timeout){
140+
char cmdBuff[120];
141+
char respBuff[32];
142+
strcpy_P(cmdBuff, cmd);
143+
strcpy_P(respBuff, resp);
144+
sendCmd(cmdBuff);
145+
return waitForResp(respBuff, timeout);
146+
}
147+
139148
void SIM800::serialDebug(void)
140149
{
141150
while(1) {

Sim800.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class SIM800
113113
* ERROR on error
114114
*/
115115
int sendCmdAndWaitForResp(const char* cmd, const char *resp, unsigned timeout);
116-
116+
int sendCmdAndWaitForResp2(const char* cmd, const char *resp, unsigned timeout);
117117

118118
/** used for serial debug, you can specify tx and rx pin and then communicate with GPRS module with common AT commands
119119
*/

0 commit comments

Comments
 (0)