Skip to content

Commit 90d5677

Browse files
committed
Improving the consistency and fail tolerance
1 parent d36848b commit 90d5677

5 files changed

Lines changed: 94 additions & 78 deletions

File tree

Http.cpp

Lines changed: 51 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -28,48 +28,61 @@
2828
#include "Http.h"
2929
#include <string.h>
3030

31-
const char *BEARER_PROFILE_GPRS = "AT+SAPBR=3,1,\"Contype\",\"GPRS\"\n";
32-
const char *BEARER_PROFILE_APN = "AT+SAPBR=3,1,\"APN\",\"%s\"\n";
33-
const char *OPEN_GPRS_CONTEXT = "AT+SAPBR=1,1\n";
34-
const char *CLOSE_GPRS_CONTEXT = "AT+SAPBR=0,1\n";
35-
const char *HTTP_INIT = "AT+HTTPINIT\n";
36-
const char *HTTP_CID = "AT+HTTPPARA=\"CID\",1\n";
37-
const char *HTTP_PARA = "AT+HTTPPARA=\"URL\",\"%s\"\n";
38-
const char *HTTP_GET = "AT+HTTPACTION=0\n";
39-
const char *HTTP_POST = "AT+HTTPACTION=1\n";
40-
const char *HTTP_DATA = "AT+HTTPDATA=%d,%d\n";
41-
const char *HTTP_READ = "AT+HTTPREAD\n";
42-
const char *HTTP_CLOSE = "AT+HTTPTERM\n";
43-
const char *HTTP_CONTENT = "AT+HTTPPARA=\"CONTENT\",\"application/json\"\n";
44-
const char *OK = "OK";
45-
const char *DOWNLOAD = "DOWNLOAD";
46-
const char *HTTP_200 = ",200,";
31+
#define BEARER_PROFILE_GPRS "AT+SAPBR=3,1,\"Contype\",\"GPRS\"\r\n"
32+
#define BEARER_PROFILE_APN "AT+SAPBR=3,1,\"APN\",\"%s\"\r\n"
33+
#define OPEN_GPRS_CONTEXT "AT+SAPBR=1,1\r\n"
34+
#define CLOSE_GPRS_CONTEXT "AT+SAPBR=0,1\r\n"
35+
#define HTTP_INIT "AT+HTTPINIT\r\n"
36+
#define HTTP_CID "AT+HTTPPARA=\"CID\",1\r\n"
37+
#define HTTP_PARA "AT+HTTPPARA=\"URL\",\"%s\"\r\n"
38+
#define HTTP_GET "AT+HTTPACTION=0\r\n"
39+
#define HTTP_POST "AT+HTTPACTION=1\n"
40+
#define HTTP_DATA "AT+HTTPDATA=%d,%d\r\n"
41+
#define HTTP_READ "AT+HTTPREAD\r\n"
42+
#define HTTP_CLOSE "AT+HTTPTERM\r\n"
43+
#define HTTP_CONTENT "AT+HTTPPARA=\"CONTENT\",\"application/json\"\r\n"
44+
#define NORMAL_MODE "AT+CFUN=1,1\r\n"
45+
#define REGISTRATION_STATUS "AT+CREG?\r\n"
46+
47+
#define OK "OK\r\n"
48+
#define DOWNLOAD "DOWNLOAD"
49+
#define HTTP_200 ",200,"
50+
#define CONNECTED "+CREG: 0,1"
51+
4752

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

5055
Result result = SUCCESS;
5156

52-
if (preInit() == FALSE){
53-
result = ERROR_INITIALIZATION;
54-
}
55-
else{
56-
if (sendCmdAndWaitForResp(BEARER_PROFILE_GPRS, OK, 2000) == FALSE)
57-
result = ERROR_BEARER_PROFILE_GPRS;
58-
59-
char httpApn[128];
60-
sprintf(httpApn, BEARER_PROFILE_APN, apn);
61-
if (sendCmdAndWaitForResp(httpApn, OK, 2000) == FALSE)
62-
result = ERROR_BEARER_PROFILE_APN;
57+
int attempts = 0;
58+
int MAX_ATTEMPTS = 10;
59+
while (sendCmdAndWaitForResp(REGISTRATION_STATUS, CONNECTED, 5000) != TRUE && attempts < MAX_ATTEMPTS){
60+
attempts ++;
61+
if (attempts == MAX_ATTEMPTS) {
62+
attempts = 0;
63+
preInit();
64+
}
6365
}
66+
67+
if (sendCmdAndWaitForResp(BEARER_PROFILE_GPRS, OK, 5000) == FALSE)
68+
result = ERROR_BEARER_PROFILE_GPRS;
69+
70+
char httpApn[128];
71+
sprintf(httpApn, BEARER_PROFILE_APN, apn);
72+
if (sendCmdAndWaitForResp(httpApn, OK, 5000) == FALSE)
73+
result = ERROR_BEARER_PROFILE_APN;
74+
6475
return result;
6576
}
6677

6778
Result HTTP::connect() {
6879

6980
Result result = SUCCESS;
70-
if (sendCmdAndWaitForResp(OPEN_GPRS_CONTEXT, OK, 2000) == FALSE)
81+
82+
if (sendCmdAndWaitForResp(OPEN_GPRS_CONTEXT, OK, 5000) == FALSE)
7183
result = ERROR_OPEN_GPRS_CONTEXT;
72-
if (sendCmdAndWaitForResp(HTTP_INIT, OK, 2000) == FALSE)
84+
85+
if (sendCmdAndWaitForResp(HTTP_INIT, OK, 5000) == FALSE)
7386
result = ERROR_HTTP_INIT;
7487

7588
return result;
@@ -78,10 +91,11 @@ Result HTTP::connect() {
7891
Result HTTP::disconnect() {
7992

8093
Result result = SUCCESS;
81-
if (sendCmdAndWaitForResp(HTTP_CLOSE, OK, 2000) == FALSE)
82-
result = ERROR_HTTP_CLOSE;
94+
8395
if (sendCmdAndWaitForResp(CLOSE_GPRS_CONTEXT, OK, 2000) == FALSE)
8496
result = ERROR_CLOSE_GPRS_CONTEXT;
97+
if (sendCmdAndWaitForResp(HTTP_CLOSE, OK, 2000) == FALSE)
98+
result = ERROR_HTTP_CLOSE;
8599

86100
return result;
87101
}
@@ -90,9 +104,6 @@ Result HTTP::post(const char *uri, const char *body, char *response) {
90104

91105
Result result = setHTTPSession(uri);
92106

93-
if (sendCmdAndWaitForResp(HTTP_CONTENT, OK, 5000) == FALSE)
94-
result = ERROR_HTTP_CONTENT;
95-
96107
char httpData[128];
97108
int delayToDownload = 5000;
98109
sprintf(httpData, HTTP_DATA, strlen(body), delayToDownload);
@@ -101,6 +112,7 @@ Result HTTP::post(const char *uri, const char *body, char *response) {
101112
}
102113

103114
sendCmd(body);
115+
purgeSerial();
104116
delay(delayToDownload);
105117

106118
if (sendCmdAndWaitForResp(HTTP_POST, HTTP_200, 5000) == TRUE) {
@@ -111,6 +123,8 @@ Result HTTP::post(const char *uri, const char *body, char *response) {
111123
else {
112124
result = ERROR_HTTP_POST;
113125
}
126+
127+
return result;
114128
}
115129

116130
Result HTTP::get(const char *uri, char *response) {
@@ -141,6 +155,9 @@ Result HTTP::setHTTPSession(const char *uri){
141155
if (sendCmdAndWaitForResp(httpPara, OK, 2000) == FALSE)
142156
result = ERROR_HTTP_PARA;
143157

158+
if (sendCmdAndWaitForResp(HTTP_CONTENT, OK, 2000) == FALSE)
159+
result = ERROR_HTTP_CONTENT;
160+
144161
return result;
145162
}
146163

Http.h

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,24 @@
2929

3030
/* Result codes */
3131
enum Result {
32-
SUCCESS,
33-
ERROR_INITIALIZATION,
34-
ERROR_BEARER_PROFILE_GPRS,
35-
ERROR_BEARER_PROFILE_APN,
36-
ERROR_OPEN_GPRS_CONTEXT,
37-
ERROR_QUERY_GPRS_CONTEXT,
38-
ERROR_CLOSE_GPRS_CONTEXT,
39-
ERROR_HTTP_INIT,
40-
ERROR_HTTP_CID,
41-
ERROR_HTTP_PARA,
42-
ERROR_HTTP_GET,
43-
ERROR_HTTP_READ,
44-
ERROR_HTTP_CLOSE,
45-
ERROR_HTTP_POST,
46-
ERROR_HTTP_DATA,
47-
ERROR_HTTP_CONTENT
32+
SUCCESS = 0,
33+
ERROR_INITIALIZATION = 1,
34+
ERROR_BEARER_PROFILE_GPRS = 2,
35+
ERROR_BEARER_PROFILE_APN = 3,
36+
ERROR_OPEN_GPRS_CONTEXT = 4,
37+
ERROR_QUERY_GPRS_CONTEXT = 5,
38+
ERROR_CLOSE_GPRS_CONTEXT = 6,
39+
ERROR_HTTP_INIT = 7,
40+
ERROR_HTTP_CID = 8,
41+
ERROR_HTTP_PARA = 9,
42+
ERROR_HTTP_GET = 10,
43+
ERROR_HTTP_READ = 11,
44+
ERROR_HTTP_CLOSE = 12,
45+
ERROR_HTTP_POST = 13,
46+
ERROR_HTTP_DATA = 14,
47+
ERROR_HTTP_CONTENT = 15,
48+
ERROR_NORMAL_MODE = 16,
49+
ERROR_LOW_CONSUMPTION_MODE = 17,
4850
};
4951

5052

Readme.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# SIM800L
22
A smart library based on Seeeduino that implements the AT HTTP commands to perform GET and POST requests to a JSON API.
3-
In order to make a requests the library follows these steps:
3+
In order to perform a request, the library follows these steps:
44

55
##### Configure Bearer:
66

7+
- AT+CREG? -> try until 0,1 (connected to the network)
78
- AT+SAPBR=3,1,"Contype","GPRS" -> wait for OK
89
- AT+SAPBR=3,1,"APN","movistar.es" -> wait for OK
910
- AT+SAPBR=1,1 -> wait for OK
@@ -31,3 +32,4 @@ In order to make a requests the library follows these steps:
3132
- AT+HTTPREAD -> read buffer and parse it
3233
- AT+HTTPTERM -> wait for OK
3334
- AT+SAPBR=0,1
35+

Sim800.cpp

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,18 @@
3030

3131
int SIM800::preInit(void)
3232
{
33-
pinMode(SIM800_POWER_STATUS,INPUT);
34-
delay(10);
35-
if(LOW == digitalRead(SIM800_POWER_STATUS))
36-
{
37-
if(sendATTest() == FALSE)
38-
{
39-
delay(800);
40-
digitalWrite(powerPin,HIGH);
41-
delay(200);
42-
digitalWrite(powerPin,LOW);
43-
delay(2000);
44-
digitalWrite(powerPin,HIGH);
45-
delay(3000);
46-
}
47-
while(sendATTest() == FALSE);
48-
return TRUE;
49-
}
50-
else
51-
{
52-
return FALSE;
53-
}
33+
pinMode(SIM800_RESET_PIN, OUTPUT);
34+
35+
digitalWrite(SIM800_RESET_PIN, HIGH);
36+
delay(200);
37+
digitalWrite(SIM800_RESET_PIN, LOW);
38+
delay(2000);
39+
digitalWrite(SIM800_RESET_PIN, HIGH);
40+
delay(3000);
41+
42+
purgeSerial();
43+
44+
return TRUE;
5445
}
5546

5647
int SIM800::checkReadable(void)
@@ -112,6 +103,7 @@ int SIM800::waitForResp(const char *resp, unsigned int timeout)
112103
while(1) {
113104
if(serialSIM800.available()) {
114105
char c = serialSIM800.read();
106+
Serial.print(c);
115107
sum = (c==resp[sum]) ? sum+1 : 0;
116108
if(sum == len)break;
117109
}
@@ -150,4 +142,9 @@ void SIM800::serialDebug(void)
150142
serialSIM800.write(Serial.read());
151143
}
152144
}
145+
}
146+
147+
void SIM800::purgeSerial()
148+
{
149+
while (serialSIM800.available()) serialSIM800.read();
153150
}

Sim800.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@
3737

3838
#define SIM800_TX_PIN 8
3939
#define SIM800_RX_PIN 7
40-
#define SIM800_POWER_PIN 9
41-
#define SIM800_POWER_STATUS 12
40+
#define SIM800_RESET_PIN 12
4241

4342
#define UART_DEBUG
4443

@@ -65,8 +64,6 @@ class SIM800
6564
* @param baudRate baud rate of uart communication
6665
*/
6766
SIM800(int baudRate):serialSIM800(SIM800_TX_PIN,SIM800_RX_PIN){
68-
powerPin = SIM800_POWER_PIN;
69-
pinMode(powerPin,OUTPUT);
7067
serialSIM800.begin(baudRate);
7168
};
7269

@@ -132,11 +129,12 @@ class SIM800
132129
*/
133130
void serialDebug(void);
134131

135-
int powerPin;
136-
SoftwareSerial serialSIM800;
132+
void purgeSerial();
137133

138134
private:
139135

136+
SoftwareSerial serialSIM800;
137+
140138
};
141139

142140
#endif

0 commit comments

Comments
 (0)