Skip to content

Commit

Permalink
Use PROGREM to efficiently store strings
Browse files Browse the repository at this point in the history
  • Loading branch information
carrascoacd committed Oct 20, 2019
1 parent 07fa8ab commit acc5be1
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 23 deletions.
97 changes: 76 additions & 21 deletions Http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,22 @@
#define ROAMING "+CREG: 0,5"
#define BEARER_OPEN "+SAPBR: 1,1"

// FTP
const char AT_FTPCID[] PROGMEM = "AT+FTPCID=1\r\n";
const char AT_FTPSERV[] PROGMEM = "AT+FTPSERV=\"%s\"\r\n";
const char AT_FTPUN[] PROGMEM = "AT+FTPUN=\"%s\"\r\n";
const char AT_FTPPW[] PROGMEM = "AT+FTPPW=\"%s\"\r\n";
const char AT_FTPPUTNAME[] PROGMEM = "AT+FTPPUTNAME=\"%s\"\r\n";
const char AT_FTPPUTPATH[] PROGMEM = "AT+FTPPUTPATH=\"%s\"\r\n";
const char AT_FTPPUT1[] PROGMEM = "AT+FTPPUT=1\r\n";
const char AT_FTPPUT2[] PROGMEM = "AT+FTPPUT=2,%d\r\n";
const char AT_FTPPUT20[] PROGMEM = "AT+FTPPUT=2,0\r\n";

const char OK_1[] PROGMEM = "OK\r\n";
const char AT_FTPPUT1_RESP[] PROGMEM = "1,1";
const char AT_FTPPUT2_RESP[] PROGMEM = "+FTPPUT: 2";
const char AT_FTPPUT20_RESP[] PROGMEM = "1,0";

Result HTTP::configureBearer(const char *apn){

Result result = SUCCESS;
Expand Down Expand Up @@ -176,47 +192,86 @@ Result HTTP::putBegin(const char *fileName,
Result result;

char buffer[64];
char resp[12];

sendCmdAndWaitForResp("AT+FTPCID=1\r\n", OK, 2000);

sprintf(buffer, "AT+FTPSERV=\"%s\"\r\n", server);
sendCmdAndWaitForResp(buffer, OK, 2000);
strcpy_P(buffer, AT_FTPCID);
strcpy_P(resp, OK_1);
if(sendCmdAndWaitForResp(buffer, resp, 20000) == FALSE){
return ERROR_FTPCID;
}

//sendCmdAndWaitForResp("AT+FTPPORT=21\r\n", OK, 2000);
//sendCmdAndWaitForResp("AT+FTPSSL=2\r\n", OK, 2000);
sprintf_P(buffer, AT_FTPSERV, server);
strcpy_P(resp, OK_1);
if(sendCmdAndWaitForResp(buffer, resp, 20000) == FALSE){
return ERROR_FTPSERV;
}

sprintf(buffer, "AT+FTPUN=\"%s\"\r\n", usr);
sendCmdAndWaitForResp(buffer, OK, 20000);
sprintf_P(buffer, AT_FTPUN, usr);
strcpy_P(resp, OK_1);
if(sendCmdAndWaitForResp(buffer, resp, 20000) == FALSE){
return ERROR_FTPUN;
}

sprintf(buffer, "AT+FTPPW=\"%s\"\r\n", pass);
sendCmdAndWaitForResp(buffer, OK, 20000);
sprintf_P(buffer, AT_FTPPW, pass);
strcpy_P(resp, OK_1);
if(sendCmdAndWaitForResp(buffer, resp, 20000) == FALSE){
return ERROR_FTPPW;
}

sprintf(buffer, "AT+FTPPUTNAME=\"%s\"\r\n", fileName);
sendCmdAndWaitForResp(buffer, OK, 20000);
sprintf_P(buffer, AT_FTPPUTNAME, fileName);
strcpy_P(resp, OK_1);
if(sendCmdAndWaitForResp(buffer, resp, 20000) == FALSE){
return ERROR_FTPPUTNAME;
}

sprintf(buffer, "AT+FTPPUTPATH=\"%s\"\r\n", path);
sendCmdAndWaitForResp(buffer, OK, 20000);
sendCmdAndWaitForResp("AT+FTPPUT=1\r\n", "1,1", 20000);
sprintf_P(buffer, AT_FTPPUTPATH, path);
strcpy_P(resp, OK_1);
if(sendCmdAndWaitForResp(buffer, resp, 20000) == FALSE){
return ERROR_FTPPUTPATH;
}

strcpy_P(buffer, AT_FTPPUT1);
strcpy_P(resp, AT_FTPPUT1_RESP);
if(sendCmdAndWaitForResp(buffer, resp, 20000) == FALSE){
return ERROR_FTPPUT1;
}

return result;
}

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

char ftpUpload[32];
sprintf(ftpUpload, "AT+FTPPUT=2,%d\r\n", size);
sendCmdAndWaitForResp(ftpUpload, "+FTPPUT: 2", 20000);
char buffer[32];
char resp[32];

sprintf_P(buffer, AT_FTPPUT2, size);
strcpy_P(resp, AT_FTPPUT2_RESP);
if(sendCmdAndWaitForResp(buffer, resp, 2000) == FALSE){
return ERROR_FTPPUT2;
}
else {
write(data, size);

strcpy_P(resp, AT_FTPPUT1_RESP);
waitForResp(resp, 2000);
}

write(data, size);
waitForResp("1,1", 20000);
return result;
}

Result HTTP::putEnd(){
Result result;
serialSIM800.flush();
sendCmdAndWaitForResp("AT+FTPPUT=2,0\r\n", "1,0", 20000);

char buffer[32];
char resp[12];

strcpy_P(buffer, AT_FTPPUT20);
strcpy_P(resp, AT_FTPPUT20_RESP);
if(sendCmdAndWaitForResp(AT_FTPPUT20, AT_FTPPUT20_RESP, 2000) == FALSE){
return ERROR_FTPPUT20;
}
return result;
}

Expand Down
12 changes: 11 additions & 1 deletion Http.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,17 @@ enum Result {
ERROR_NORMAL_MODE = 16,
ERROR_LOW_CONSUMPTION_MODE = 17,
ERROR_HTTPS_ENABLE = 18,
ERROR_HTTPS_DISABLE = 19
ERROR_HTTPS_DISABLE = 19,
// FTP
ERROR_FTPCID = 20,
ERROR_FTPSERV = 21,
ERROR_FTPUN = 22,
ERROR_FTPPW = 23,
ERROR_FTPPUTNAME = 24,
ERROR_FTPPUTPATH = 25,
ERROR_FTPPUT1 = 26,
ERROR_FTPPUT2 = 27,
ERROR_FTPPUT20 = 28,
};


Expand Down
9 changes: 9 additions & 0 deletions Sim800.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,15 @@ int SIM800::sendCmdAndWaitForResp(const char* cmd, const char *resp, unsigned ti
return waitForResp(resp,timeout);
}

int SIM800::sendCmdAndWaitForResp2(const char* cmd, const char *resp, unsigned timeout){
char cmdBuff[120];
char respBuff[32];
strcpy_P(cmdBuff, cmd);
strcpy_P(respBuff, resp);
sendCmd(cmdBuff);
return waitForResp(respBuff, timeout);
}

void SIM800::serialDebug(void)
{
while(1) {
Expand Down
2 changes: 1 addition & 1 deletion Sim800.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class SIM800
* ERROR on error
*/
int sendCmdAndWaitForResp(const char* cmd, const char *resp, unsigned timeout);

int sendCmdAndWaitForResp2(const char* cmd, const char *resp, unsigned timeout);

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

0 comments on commit acc5be1

Please sign in to comment.