Skip to content

Commit f7eba0a

Browse files
committed
New improved version of the library which handles the GPRS context more properly.
1 parent a9dc60b commit f7eba0a

4 files changed

Lines changed: 53 additions & 49 deletions

File tree

Http.cpp

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
#define BEARER_PROFILE_GPRS "AT+SAPBR=3,1,\"Contype\",\"GPRS\"\r\n"
3232
#define BEARER_PROFILE_APN "AT+SAPBR=3,1,\"APN\",\"%s\"\r\n"
33+
#define QUERY_BEARER "AT+SAPBR=2,1\r\n"
3334
#define OPEN_GPRS_CONTEXT "AT+SAPBR=1,1\r\n"
3435
#define CLOSE_GPRS_CONTEXT "AT+SAPBR=0,1\r\n"
3536
#define HTTP_INIT "AT+HTTPINIT\r\n"
@@ -49,18 +50,22 @@
4950
#define DOWNLOAD "DOWNLOAD"
5051
#define HTTP_200 ",200,"
5152
#define CONNECTED "+CREG: 0,1"
53+
#define BEARER_OPEN "+SAPBR:1,1"
5254

5355

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

5658
Result result = SUCCESS;
5759

58-
int attempts = 0;
59-
int MAX_ATTEMPTS = 10;
60+
unsigned int attempts = 0;
61+
unsigned int MAX_ATTEMPTS = 10;
62+
63+
sendATTest();
6064

6165
while (sendCmdAndWaitForResp(REGISTRATION_STATUS, CONNECTED, 2000) != TRUE && attempts < MAX_ATTEMPTS){
6266
sendCmdAndWaitForResp(SIGNAL_QUALITY, CONNECTED, 1000);
6367
attempts ++;
68+
delay(1000 * attempts);
6469
if (attempts == MAX_ATTEMPTS) {
6570
attempts = 0;
6671
preInit();
@@ -70,7 +75,7 @@ Result HTTP::configureBearer(const char *apn){
7075
if (sendCmdAndWaitForResp(BEARER_PROFILE_GPRS, OK, 2000) == FALSE)
7176
result = ERROR_BEARER_PROFILE_GPRS;
7277

73-
char httpApn[128];
78+
char httpApn[64];
7479
sprintf(httpApn, BEARER_PROFILE_APN, apn);
7580
if (sendCmdAndWaitForResp(httpApn, OK, 2000) == FALSE)
7681
result = ERROR_BEARER_PROFILE_APN;
@@ -82,8 +87,10 @@ Result HTTP::connect() {
8287

8388
Result result = SUCCESS;
8489

85-
if (sendCmdAndWaitForResp(OPEN_GPRS_CONTEXT, OK, 2000) == FALSE){
86-
result = ERROR_OPEN_GPRS_CONTEXT;
90+
if (sendCmdAndWaitForResp(QUERY_BEARER, BEARER_OPEN, 2000) == FALSE){
91+
if (sendCmdAndWaitForResp(OPEN_GPRS_CONTEXT, OK, 6000) == FALSE){
92+
result = ERROR_OPEN_GPRS_CONTEXT;
93+
}
8794
}
8895

8996
if (sendCmdAndWaitForResp(HTTP_INIT, OK, 2000) == FALSE)
@@ -109,24 +116,26 @@ Result HTTP::post(const char *uri, const char *body, char *response) {
109116
Result result = setHTTPSession(uri);
110117

111118
char httpData[32];
112-
int delayToDownload = 10000;
113-
sprintf(httpData, HTTP_DATA, strlen(body), 2000);
119+
unsigned int delayToDownload = 10000;
120+
sprintf(httpData, HTTP_DATA, strlen(body), 10000);
114121
if (sendCmdAndWaitForResp(httpData, DOWNLOAD, 2000) == FALSE){
115122
result = ERROR_HTTP_DATA;
116123
}
117124

118125
purgeSerial();
119-
printData(body);
120-
delay(delayToDownload);
126+
delay(500);
127+
sendCmd(body);
128+
delay(1000);
129+
121130
if (sendCmdAndWaitForResp(HTTP_POST, HTTP_200, delayToDownload) == TRUE) {
122131
sendCmd(HTTP_READ);
123-
result = SUCCESS;
124132
readResponse(response);
133+
result = SUCCESS;
125134
}
126135
else {
127136
result = ERROR_HTTP_POST;
128137
}
129-
138+
130139
return result;
131140
}
132141

HttpExample/HttpExample.ino

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
#include <ArduinoJson.h>
22
#include <Http.h>
33

4-
HTTP http;
5-
Result result;
6-
74
unsigned long lastRunTime = 0;
85
unsigned long waitForRunTime = 0;
6+
HTTP http;
97

108
// the setup routine runs once when you press reset:
119
void setup() {
@@ -20,52 +18,60 @@ void loop() {
2018
}
2119

2220
// functions
23-
void print(const char *message, long result = -1){
24-
if (result != -1){
25-
char buffer[100];
26-
sprintf(buffer, message, result);
27-
Serial.println(buffer);
21+
void print(const __FlashStringHelper *message, int code = -1){
22+
if (code != -1){
23+
Serial.print(message);
24+
Serial.println(code);
2825
}
2926
else {
3027
Serial.println(message);
3128
}
3229
}
3330

3431
bool shouldTrackTimeEntry(){
32+
// This calculation uses the max value the unsigned long can store as key. Remember when a negative number
33+
// is assigned or the maximun is exceeded, then the module is applied to that value.
3534
unsigned long elapsedTime = millis() - lastRunTime;
36-
print("Elapsed time: %ld", elapsedTime);
35+
print(F("Elapsed time: "), elapsedTime);
3736
return elapsedTime >= waitForRunTime;
3837
}
3938

4039
void trackTimeEntry(){
4140

42-
print("Cofigure bearer: %d", http.configureBearer("movistar.es"));
43-
print("HTTP connect: %d", http.connect());
44-
45-
char response[256];
46-
47-
result = http.get("smartgarden.herokuapp.com/api", response);
48-
print("HTTP GET: %d", result);
41+
char response[32];
42+
char body[90];
43+
Result result;
44+
45+
print(F("Cofigure bearer: "), http.configureBearer("movistar.es"));
46+
result = http.connect();
47+
print(F("HTTP connect: "), result);
48+
49+
sprintf(body, "{\"name\": \"%s\"}", "Arduino");
50+
result = http.post("your.domain/api/devices", body, response);
51+
print(F("HTTP POST: "), result);
4952
if (result == SUCCESS) {
5053
Serial.println(response);
51-
StaticJsonBuffer<200> jsonBuffer;
54+
StaticJsonBuffer<32> jsonBuffer;
5255
JsonObject& root = jsonBuffer.parseObject(response);
5356
lastRunTime = millis();
5457
waitForRunTime = root["waitForRunTime"];
5558

56-
print("Last run time: %ld", lastRunTime);
57-
print("Next post in: %ld", waitForRunTime);
59+
print(F("Last run time: "), lastRunTime);
60+
print(F("Next post in: "), waitForRunTime);
5861
}
59-
60-
result = http.post("smartgarden.herokuapp.com/api", "{\"name\":\"Antonio\"}", response);
61-
print("HTTP POST: %d", result);
62+
63+
result = http.get("your.domain/api/timing", response);
64+
print(F("HTTP GET: "), result);
6265
if (result == SUCCESS) {
6366
Serial.println(response);
64-
StaticJsonBuffer<200> jsonBuffer;
67+
StaticJsonBuffer<32> jsonBuffer;
6568
JsonObject& root = jsonBuffer.parseObject(response);
66-
Serial.print("name: ");
67-
Serial.println((const char *)root["name"]);
69+
lastRunTime = millis();
70+
waitForRunTime = root["waitForRunTime"];
71+
72+
print(F("Last run time: "), lastRunTime);
73+
print(F("Next post in: "), waitForRunTime);
6874
}
69-
70-
print("HTTP disconnect: %d", http.disconnect());
75+
76+
print(F("HTTP disconnect: "), http.disconnect());
7177
}

Sim800.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,6 @@ void SIM800::sendCmd(const char* cmd)
8787
serialSIM800.write(cmd);
8888
}
8989

90-
void SIM800::printData(const char* data)
91-
{
92-
Serial.println(data);
93-
serialSIM800.print(data);
94-
}
95-
9690
int SIM800::sendATTest(void)
9791
{
9892
int ret = sendCmdAndWaitForResp("AT\r\n","OK",DEFAULT_TIMEOUT);

Sim800.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,6 @@ class SIM800
9797
*/
9898
void sendCmd(const char* cmd);
9999

100-
/** print data for HTTP POST to SIM800 module
101-
* @param data array which will be send to GPRS module
102-
*/
103-
void printData(const char* data);
104-
105100
/**send "AT" to SIM800 module
106101
*/
107102
int sendATTest(void);

0 commit comments

Comments
 (0)