Skip to content

Commit

Permalink
Extract GPRS and common functions
Browse files Browse the repository at this point in the history
  • Loading branch information
carrascoacd committed Dec 9, 2019
1 parent 79a2a46 commit 072ea5c
Show file tree
Hide file tree
Showing 12 changed files with 330 additions and 52 deletions.
35 changes: 7 additions & 28 deletions Ftp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,54 +62,40 @@ Result FTP::putBegin(const char *apn,
const char *pass,
const char *path)
{
Result result = openGPRSContext(*this, apn);
Result result = openGPRSContext(this, apn);

char buffer[64];
char tmp[24];

delay(10000);
if (sendCmdAndWaitForResp_P(AT_FTPCID, OK, 2000) == FALSE)
{
return ERROR_FTPCID;
}

strcpy_P(tmp, server);
sprintf_P(buffer, AT_FTPSERV, tmp);
if (sendCmdAndWaitForResp(buffer, OK, 2000) == FALSE)
{
return ERROR_FTPSERV;
}

strcpy_P(tmp, usr);
sprintf_P(buffer, AT_FTPUN, tmp);
if (sendCmdAndWaitForResp(buffer, OK_, 2000) == FALSE)
{
return ERROR_FTPUN;
}

strcpy_P(tmp, pass);
sprintf_P(buffer, AT_FTPPW, tmp);
if (sendCmdAndWaitForResp(buffer, OK_, 2000) == FALSE)
{
return ERROR_FTPPW;
}

sprintf_P(buffer, AT_FTPPUTNAME, fileName);
if (sendCmdAndWaitForResp(buffer, OK_, 2000) == FALSE)
{
return ERROR_FTPPUTNAME;
}

sprintf_P(buffer, AT_FTPPUTPATH, path);
if (sendCmdAndWaitForResp(buffer, OK_, 2000) == FALSE)
{
return ERROR_FTPPUTPATH;
}

if (sendCmdAndWaitForResp_P(AT_FTPPUT1, AT_FTPPUT1_RESP, 10000) == FALSE)
{
return ERROR_FTPPUT1;
}

return result;
}
Expand All @@ -125,9 +111,7 @@ Result FTP::putWrite(const char *data, unsigned int size)
{
attempts++;
if (attempts == MAX_ATTEMPTS)
{
return ERROR_FTPPUT11;
}
}

return result;
Expand All @@ -137,42 +121,37 @@ Result FTP::putWriteStart(unsigned int size)
{
Result result = SUCCESS;

char buffer[32];
char resp[32];
char buffer[22];
char resp[11];

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

return result;
}

Result FTP::putWriteEnd(const char *data, unsigned int size)
{
Result result = SUCCESS;
char resp[8];
char resp[4];

write(data, size);

strcpy_P(resp, AT_FTPPUT1_RESP);
if (waitForResp(resp, 2000) == FALSE)
{
return ERROR_FTPPUT11;
}

return result;
}

Result FTP::putEnd()
{
Result result = closeGPRSContext(*this);
Result result = closeGPRSContext(this);

if (sendCmdAndWaitForResp_P(AT_FTPPUT20, AT_FTPPUT20_RESP, 2000) == FALSE)
{
return ERROR_FTPPUT20;
}

return result;
}
26 changes: 13 additions & 13 deletions GPRS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,43 +44,43 @@ const char BEARER_OPEN[] PROGMEM = "+SAPBR: 1,1";
const char OK[] PROGMEM = "OK";
const char OK_ PROGMEM = "OK";

Result openGPRSContext(SIM800& sim800, const char *apn)
Result openGPRSContext(SIM800 *sim800, const char *apn)
{
Result result = SUCCESS;
uint8_t attempts = 0;
uint8_t MAX_ATTEMPTS = 10;

sim800.sendATTest();
sim800->sendATTest();

while ((sim800.sendCmdAndWaitForResp_P(REGISTRATION_STATUS, CONNECTED, 2000) != TRUE &&
sim800.sendCmdAndWaitForResp_P(REGISTRATION_STATUS, ROAMING, 2000) != TRUE) &&
while ((sim800->sendCmdAndWaitForResp_P(REGISTRATION_STATUS, CONNECTED, 2000) != TRUE &&
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, OK, 1000);
sim800->sendCmdAndWaitForResp_P(SIGNAL_QUALITY, OK, 1000);
attempts++;
delay(1000 * attempts);
if (attempts == MAX_ATTEMPTS)
{
attempts = 0;
sim800.preInit();
sim800->preInit();
}
}

if (sim800.sendCmdAndWaitForResp_P(BEARER_PROFILE_GPRS, OK, 2000) == FALSE)
if (sim800->sendCmdAndWaitForResp_P(BEARER_PROFILE_GPRS, 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, OK_, 2000) == FALSE)
result = ERROR_BEARER_PROFILE_APN;

while (sim800.sendCmdAndWaitForResp_P(QUERY_BEARER, BEARER_OPEN, 2000) == FALSE && attempts < MAX_ATTEMPTS)
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, OK, 2000) == FALSE)
{
result = ERROR_OPEN_GPRS_CONTEXT;
}
Expand All @@ -93,11 +93,11 @@ Result openGPRSContext(SIM800& sim800, const char *apn)
return result;
}

Result closeGPRSContext(SIM800& sim800)
Result closeGPRSContext(SIM800 *sim800)
{
Result result = SUCCESS;

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

return result;
Expand Down
4 changes: 2 additions & 2 deletions GPRS.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#include "Result.h"
#include "Sim800.h"

Result openGPRSContext(SIM800& sim800, const char *apn);
Result closeGPRSContext(SIM800& sim800);
Result openGPRSContext(SIM800 *sim800, const char *apn);
Result closeGPRSContext(SIM800 *sim800);

#endif
7 changes: 4 additions & 3 deletions Http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const char HTTPS_DISABLE[] PROGMEM = "AT+HTTPSSL=0\r\n";
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 DOWNLOAD[] PROGMEM = "DOWNLOAD";
const char HTTP_2XX[] PROGMEM = ",2XX,";
Expand All @@ -52,7 +53,7 @@ const char OK_[] = "OK";

Result HTTP::connect(const char *apn)
{
Result result = openGPRSContext(*this, apn);
Result result = openGPRSContext(this, apn);

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

Result HTTP::disconnect()
{
Result result = closeGPRSContext(*this);;
Result result = closeGPRSContext(this);

if (sendCmdAndWaitForResp_P(HTTP_CLOSE, OK, 2000) == FALSE)
result = ERROR_HTTP_CLOSE;
Expand Down Expand Up @@ -109,7 +110,7 @@ Result HTTP::get(const char *uri, char *response)
Result result = setHTTPSession(uri);
char buffer[16];
char resp[16];

if (sendCmdAndWaitForResp_P(HTTP_GET, HTTP_2XX, 2000) == TRUE)
{
strcpy_P(buffer, HTTP_READ);
Expand Down
8 changes: 4 additions & 4 deletions Sim800.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ int SIM800::waitForResp(const char *resp, unsigned int timeout)
char c = serialSIM800.read();
if (debugMode)
Serial.print(c);

sum = (c == resp[sum] || resp[sum] == 'X') ? sum + 1 : 0;
if (sum == len)
break;
Expand Down Expand Up @@ -160,7 +161,7 @@ int SIM800::sendCmdAndWaitForResp_P(const char *cmd, const char *resp, unsigned
char respBuff[32];
strcpy_P(cmdBuff, cmd);
strcpy_P(respBuff, resp);

return sendCmdAndWaitForResp(cmdBuff, respBuff, timeout);
}

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

void SIM800::sleep(bool force)
{
char buffer[16];
if (force)
{
sendCmdAndWaitForResp_P(SLEEP_MODE_1, OK, 2000);
}
else
{
sendCmdAndWaitForResp_P(buffer, OK, 2000);
sendCmdAndWaitForResp_P(SLEEP_MODE_2, OK, 2000);
}
}

void SIM800::wakeUp()
{
preInit();
preInit();
}
10 changes: 8 additions & 2 deletions Sim800.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#ifndef __SIM800_H__
#define __SIM800_H__

#include "Arduino.h"
#include <Arduino.h>
#include <SoftwareSerial.h>

#define TRUE 1
Expand All @@ -39,6 +39,7 @@
/** SIM800 class.
* Used for SIM800 communication. attention that SIM800 module communicate with MCU in serial protocol
*/

class SIM800
{

Expand All @@ -47,8 +48,13 @@ class SIM800
* @param baudRate baud rate of uart communication
* @param rxPin uart receive pin to communicate with SIM800
* @param txPin uart transmit pin to communicate with SIM800
* @param debug indicates if print the AT command sequence
*/
SIM800(unsigned int baudRate, unsigned int rxPin, unsigned int txPin, unsigned int rstPin, bool debug) : serialSIM800(txPin, rxPin)
SIM800(unsigned int baudRate,
unsigned int rxPin,
unsigned int txPin,
unsigned int rstPin,
bool debug) : serialSIM800(txPin, rxPin)
{
serialSIM800.begin(baudRate);
debugMode = debug;
Expand Down
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 072ea5c

Please sign in to comment.