diff --git a/Readme.md b/Readme.md index aa67d4f..c61234e 100644 --- a/Readme.md +++ b/Readme.md @@ -117,7 +117,4 @@ In order to perform a request, the library follows these steps: ## Development -- Lint -``` -cppcheck --enable=all . -``` \ No newline at end of file +- Lint: `cppcheck --enable=all .` \ No newline at end of file diff --git a/src/Ftp.cpp b/src/Ftp.cpp index c346b89..ba4844b 100644 --- a/src/Ftp.cpp +++ b/src/Ftp.cpp @@ -44,8 +44,8 @@ const char CLOSE_GPRS_CONTEXT[] PROGMEM = "AT+SAPBR=0,1\r\n"; const char REGISTRATION_STATUS[] PROGMEM = "AT+CREG?\r\n"; const char SLEEP_MODE_2[] PROGMEM = "AT+CSCLK=2\r\n"; -const char OK[] PROGMEM = "OK"; -const char OK_[] = "OK"; +const char AT_OK[] PROGMEM = "OK"; +const char AT_OK_[] = "OK"; const char AT_FTPPUT1_RESP[] PROGMEM = "1,1"; const char AT_FTPPUT2_RESP[] PROGMEM = "+FTPPUT: 2"; const char AT_FTPPUT20_RESP[] PROGMEM = "1,0"; @@ -68,30 +68,30 @@ Result FTP::putBegin(const char *apn, char tmp[24]; delay(10000); - if (sendCmdAndWaitForResp_P(AT_FTPCID, OK, 2000) == FALSE) + if (sendCmdAndWaitForResp_P(AT_FTPCID, AT_OK, 2000) == FALSE) return ERROR_FTPCID; strcpy_P(tmp, server); sprintf_P(buffer, AT_FTPSERV, tmp); - if (sendCmdAndWaitForResp(buffer, OK, 2000) == FALSE) + if (sendCmdAndWaitForResp(buffer, AT_OK_, 2000) == FALSE) return ERROR_FTPSERV; strcpy_P(tmp, usr); sprintf_P(buffer, AT_FTPUN, tmp); - if (sendCmdAndWaitForResp(buffer, OK_, 2000) == FALSE) + if (sendCmdAndWaitForResp(buffer, AT_OK_, 2000) == FALSE) return ERROR_FTPUN; strcpy_P(tmp, pass); sprintf_P(buffer, AT_FTPPW, tmp); - if (sendCmdAndWaitForResp(buffer, OK_, 2000) == FALSE) + if (sendCmdAndWaitForResp(buffer, AT_OK_, 2000) == FALSE) return ERROR_FTPPW; sprintf_P(buffer, AT_FTPPUTNAME, fileName); - if (sendCmdAndWaitForResp(buffer, OK_, 2000) == FALSE) + if (sendCmdAndWaitForResp(buffer, AT_OK_, 2000) == FALSE) return ERROR_FTPPUTNAME; sprintf_P(buffer, AT_FTPPUTPATH, path); - if (sendCmdAndWaitForResp(buffer, OK_, 2000) == FALSE) + if (sendCmdAndWaitForResp(buffer, AT_OK_, 2000) == FALSE) return ERROR_FTPPUTPATH; if (sendCmdAndWaitForResp_P(AT_FTPPUT1, AT_FTPPUT1_RESP, 10000) == FALSE) diff --git a/src/GPRS.cpp b/src/GPRS.cpp index e396ab4..b5416f9 100644 --- a/src/GPRS.cpp +++ b/src/GPRS.cpp @@ -41,8 +41,8 @@ const char REGISTRATION_STATUS[] PROGMEM = "AT+CREG?\r\n"; const char CONNECTED[] PROGMEM = "+CREG: 0,1"; const char ROAMING[] PROGMEM = "+CREG: 0,5"; const char BEARER_OPEN[] PROGMEM = "+SAPBR: 1,1"; -const char OK[] PROGMEM = "OK"; -const char OK_ = "OK"; +const char AT_OK[] PROGMEM = "OK"; +const char AT_OK_[] = "OK"; Result openGPRSContext(SIM800 *sim800, const char *apn) { @@ -56,8 +56,8 @@ Result openGPRSContext(SIM800 *sim800, const char *apn) sim800->sendCmdAndWaitForResp_P(REGISTRATION_STATUS, ROAMING, 2000) != TRUE) && attempts < MAX_ATTEMPTS) { - sim800->sendCmdAndWaitForResp_P(READ_VOLTAGE, OK, 1000); - sim800->sendCmdAndWaitForResp_P(SIGNAL_QUALITY, OK, 1000); + sim800->sendCmdAndWaitForResp_P(READ_VOLTAGE, AT_OK, 1000); + sim800->sendCmdAndWaitForResp_P(SIGNAL_QUALITY, AT_OK, 1000); attempts++; delay(1000 * attempts); if (attempts == MAX_ATTEMPTS) @@ -67,20 +67,20 @@ Result openGPRSContext(SIM800 *sim800, const char *apn) } } - if (sim800->sendCmdAndWaitForResp_P(BEARER_PROFILE_GPRS, OK, 2000) == FALSE) + if (sim800->sendCmdAndWaitForResp_P(BEARER_PROFILE_GPRS, AT_OK, 2000) == FALSE) result = ERROR_BEARER_PROFILE_GPRS; char httpApn[64]; char tmp[24]; strcpy_P(tmp, apn); sprintf_P(httpApn, BEARER_PROFILE_APN, tmp); - if (sim800->sendCmdAndWaitForResp(httpApn, OK_, 2000) == FALSE) + if (sim800->sendCmdAndWaitForResp(httpApn, AT_OK_, 2000) == FALSE) result = ERROR_BEARER_PROFILE_APN; while (sim800->sendCmdAndWaitForResp_P(QUERY_BEARER, BEARER_OPEN, 2000) == FALSE && attempts < MAX_ATTEMPTS) { attempts++; - if (sim800->sendCmdAndWaitForResp_P(OPEN_GPRS_CONTEXT, OK, 2000) == FALSE) + if (sim800->sendCmdAndWaitForResp_P(OPEN_GPRS_CONTEXT, AT_OK, 2000) == FALSE) { result = ERROR_OPEN_GPRS_CONTEXT; } @@ -97,7 +97,7 @@ Result closeGPRSContext(SIM800 *sim800) { Result result = SUCCESS; - if (sim800->sendCmdAndWaitForResp_P(CLOSE_GPRS_CONTEXT, OK, 2000) == FALSE) + if (sim800->sendCmdAndWaitForResp_P(CLOSE_GPRS_CONTEXT, AT_OK, 2000) == FALSE) result = ERROR_CLOSE_GPRS_CONTEXT; return result; diff --git a/src/Http.cpp b/src/Http.cpp index b4dae6c..43fc25d 100644 --- a/src/Http.cpp +++ b/src/Http.cpp @@ -27,6 +27,7 @@ #include "Http.h" #include "Parser.h" +#include "GPRS.h" const char HTTP_INIT[] PROGMEM = "AT+HTTPINIT\r\n"; const char HTTP_CID[] PROGMEM = "AT+HTTPPARA=\"CID\",1\r\n"; @@ -43,19 +44,18 @@ const char NORMAL_MODE[] PROGMEM = "AT+CFUN=1,1\r\n"; const char SIGNAL_QUALITY[] PROGMEM = "AT+CSQ\r\n"; const char READ_VOLTAGE[] PROGMEM = "AT+CBC\r\n"; -const char OK[] PROGMEM = "OK"; +const char AT_OK[] PROGMEM = "OK"; +const char AT_OK_[] = "OK"; const char DOWNLOAD[] PROGMEM = "DOWNLOAD"; const char HTTP_2XX[] PROGMEM = ",2XX,"; const char HTTPS_PREFIX[] PROGMEM = "https://"; -const char OK_[] = "OK"; -#include "GPRS.h" Result HTTP::connect(const char *apn) { Result result = openGPRSContext(this, apn); - if (sendCmdAndWaitForResp_P(HTTP_INIT, OK, 2000) == FALSE) + if (sendCmdAndWaitForResp_P(HTTP_INIT, AT_OK, 2000) == FALSE) result = ERROR_HTTP_INIT; return result; @@ -65,7 +65,7 @@ Result HTTP::disconnect() { Result result = closeGPRSContext(this); - if (sendCmdAndWaitForResp_P(HTTP_CLOSE, OK, 2000) == FALSE) + if (sendCmdAndWaitForResp_P(HTTP_CLOSE, AT_OK, 2000) == FALSE) result = ERROR_HTTP_CLOSE; return result; @@ -132,7 +132,7 @@ unsigned int HTTP::readVoltage() cleanBuffer(voltage, sizeof(voltage)); strcpy_P(buffer, READ_VOLTAGE); - sendCmd(buffer); + sendCmd(buffer, 500); if (readBuffer(buffer, sizeof(buffer)) == TRUE) { @@ -179,20 +179,20 @@ Result HTTP::setHTTPSession(const char *uri) Result result = SUCCESS; char buffer[128]; - if (sendCmdAndWaitForResp_P(HTTP_CID, OK, 2000) == FALSE) + if (sendCmdAndWaitForResp_P(HTTP_CID, AT_OK, 2000) == FALSE) result = ERROR_HTTP_CID; sprintf_P(buffer, HTTP_PARA, uri); - if (sendCmdAndWaitForResp(buffer, OK_, 2000) == FALSE) + if (sendCmdAndWaitForResp(buffer, AT_OK_, 2000) == FALSE) result = ERROR_HTTP_PARA; bool https = strncmp_P(uri, HTTPS_PREFIX, strlen_P(HTTPS_PREFIX)) == 0; - if (sendCmdAndWaitForResp_P(https ? HTTPS_ENABLE : HTTPS_DISABLE, OK, 2000) == FALSE) + if (sendCmdAndWaitForResp_P(https ? HTTPS_ENABLE : HTTPS_DISABLE, AT_OK, 2000) == FALSE) { result = https ? ERROR_HTTPS_ENABLE : ERROR_HTTPS_DISABLE; } - if (sendCmdAndWaitForResp_P(HTTP_CONTENT, OK, 2000) == FALSE) + if (sendCmdAndWaitForResp_P(HTTP_CONTENT, AT_OK, 2000) == FALSE) result = ERROR_HTTP_CONTENT; return result; diff --git a/src/Sim800.cpp b/src/Sim800.cpp index ec88888..ab2ac14 100644 --- a/src/Sim800.cpp +++ b/src/Sim800.cpp @@ -31,7 +31,7 @@ const char SLEEP_MODE_2[] PROGMEM = "AT+CSCLK=2\r\n"; const char SLEEP_MODE_1[] PROGMEM = "AT+CSCLK=1\r\n"; const char SLEEP_MODE_0[] PROGMEM = "AT+CSCLK=0\r\n"; -const char OK[] PROGMEM = "OK"; +const char AT_OK[] PROGMEM = "OK"; int SIM800::preInit(void) { @@ -95,11 +95,11 @@ void SIM800::cleanBuffer(char *buffer, int count) } } -void SIM800::sendCmd(const char *cmd) +void SIM800::sendCmd(const char *cmd, unsigned int delayBeforeSend) { serialSIM800.listen(); serialSIM800.flush(); - delay(500); + delay(delayBeforeSend); write(cmd); serialSIM800.flush(); } @@ -203,11 +203,11 @@ void SIM800::sleep(bool force) { if (force) { - sendCmdAndWaitForResp_P(SLEEP_MODE_1, OK, 2000); + sendCmdAndWaitForResp_P(SLEEP_MODE_1, AT_OK, 2000); } else { - sendCmdAndWaitForResp_P(SLEEP_MODE_2, OK, 2000); + sendCmdAndWaitForResp_P(SLEEP_MODE_2, AT_OK, 2000); } } diff --git a/src/Sim800.h b/src/Sim800.h index 18a9f38..f699526 100644 --- a/src/Sim800.h +++ b/src/Sim800.h @@ -37,7 +37,7 @@ #define DEFAULT_TIMEOUT 5000 // Comment or uncomment this to debug the library -#define DEBUG true +// #define DEBUG true /** SIM800 class. * Used for SIM800 communication. attention that SIM800 module communicate with MCU in serial protocol @@ -87,8 +87,9 @@ class SIM800 /** send AT command to SIM800 module * @param cmd command array which will be send to GPRS module + * @param delayBeforeSend integer indicating the sime to wait before sending a command */ - void sendCmd(const char *cmd); + void sendCmd(const char *cmd, unsigned int delayBeforeSend = 10); /**send "AT" to SIM800 module */