Skip to content

Commit baa0937

Browse files
authored
Merge pull request #4 from sandro-csimas/https_support
Adding support for HTTPS urls
2 parents 5b3d7d4 + c50c46d commit baa0937

3 files changed

Lines changed: 26 additions & 15 deletions

File tree

Http.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
#define HTTP_READ "AT+HTTPREAD\r\n"
4343
#define HTTP_CLOSE "AT+HTTPTERM\r\n"
4444
#define HTTP_CONTENT "AT+HTTPPARA=\"CONTENT\",\"application/json\"\r\n"
45+
#define HTTPS_ENABLE "AT+HTTPSSL=1\r\n"
46+
#define HTTPS_DISABLE "AT+HTTPSSL=0\r\n"
4547
#define NORMAL_MODE "AT+CFUN=1,1\r\n"
4648
#define REGISTRATION_STATUS "AT+CREG?\r\n"
4749
#define SIGNAL_QUALITY "AT+CSQ\r\n"
@@ -50,10 +52,10 @@
5052
#define OK "OK\r\n"
5153
#define DOWNLOAD "DOWNLOAD"
5254
#define HTTP_200 ",200,"
55+
#define HTTPS_PREFIX "https://"
5356
#define CONNECTED "+CREG: 0,1"
5457
#define BEARER_OPEN "+SAPBR: 1,1"
5558

56-
5759
Result HTTP::configureBearer(const char *apn){
5860

5961
Result result = SUCCESS;
@@ -62,7 +64,7 @@ Result HTTP::configureBearer(const char *apn){
6264
unsigned int MAX_ATTEMPTS = 10;
6365

6466
sendATTest();
65-
67+
6668
while (sendCmdAndWaitForResp(REGISTRATION_STATUS, CONNECTED, 2000) != TRUE && attempts < MAX_ATTEMPTS){
6769
sendCmdAndWaitForResp(SIGNAL_QUALITY, CONNECTED, 1000);
6870
attempts ++;
@@ -75,12 +77,12 @@ Result HTTP::configureBearer(const char *apn){
7577

7678
if (sendCmdAndWaitForResp(BEARER_PROFILE_GPRS, OK, 2000) == FALSE)
7779
result = ERROR_BEARER_PROFILE_GPRS;
78-
80+
7981
char httpApn[64];
8082
sprintf(httpApn, BEARER_PROFILE_APN, apn);
8183
if (sendCmdAndWaitForResp(httpApn, OK, 2000) == FALSE)
8284
result = ERROR_BEARER_PROFILE_APN;
83-
85+
8486
return result;
8587
}
8688

@@ -143,7 +145,7 @@ Result HTTP::post(const char *uri, const char *body, char *response) {
143145
Result HTTP::get(const char *uri, char *response) {
144146

145147
Result result = setHTTPSession(uri);
146-
148+
147149
if (sendCmdAndWaitForResp(HTTP_GET, HTTP_200, 2000) == TRUE) {
148150
sendCmd(HTTP_READ);
149151
result = SUCCESS;
@@ -176,15 +178,20 @@ Result HTTP::setHTTPSession(const char *uri){
176178
if (sendCmdAndWaitForResp(httpPara, OK, 2000) == FALSE)
177179
result = ERROR_HTTP_PARA;
178180

181+
bool https = strncmp(HTTPS_PREFIX, uri, strlen(HTTPS_PREFIX)) == 0;
182+
if (sendCmdAndWaitForResp(https ? HTTPS_ENABLE : HTTPS_DISABLE, OK, 2000) == FALSE) {
183+
result = https ? ERROR_HTTPS_ENABLE : ERROR_HTTPS_DISABLE;
184+
}
185+
179186
if (sendCmdAndWaitForResp(HTTP_CONTENT, OK, 2000) == FALSE)
180187
result = ERROR_HTTP_CONTENT;
181188

182189
return result;
183190
}
184191

185192
void HTTP::readResponse(char *response){
186-
187-
char buffer[128];
193+
194+
char buffer[128];
188195
cleanBuffer(buffer, sizeof(buffer));
189196
cleanBuffer(response, sizeof(response));
190197

@@ -214,7 +221,7 @@ void HTTP::parseJSONResponse(const char *buffer, unsigned int bufferSize, char *
214221
}
215222
--j;
216223
}
217-
224+
218225
for(int k = 0; k < (end_index - start_index) + 2; ++k){
219226
response[k] = buffer[start_index + k];
220227
response[k + 1] = '\0';

Http.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ enum Result {
4747
ERROR_HTTP_CONTENT = 15,
4848
ERROR_NORMAL_MODE = 16,
4949
ERROR_LOW_CONSUMPTION_MODE = 17,
50+
ERROR_HTTPS_ENABLE = 18,
51+
ERROR_HTTPS_DISABLE = 19
5052
};
5153

5254

Readme.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
# HTTP client for SIM800L
3-
A smart HTTP client based on Seeeduino that implements the AT HTTP commands to perform GET and POST requests to a JSON API.
3+
A smart HTTP client based on Seeeduino that implements the AT HTTP commands to perform GET and POST requests to a JSON API.
44

55
## Support
66
* Your board have to support the standard SoftwareSerial library. It doesn't work with HardwareSerial based boards for the moment.
@@ -66,22 +66,24 @@ In order to perform a request, the library follows these steps:
6666
##### HTTP GET:
6767

6868
- AT+HTTPINIT -> wait for OK
69-
- AT+HTTPPARA="CID",1 -> wait for OK
70-
- AT+HTTPPARA="URL","smartgarden.herokuapp.com" -> wait for OK
71-
- AT+HTTPACTION=0 -> wait for ,200,
69+
- AT+HTTPPARA="CID",1 -> wait for OK
70+
- AT+HTTPPARA="URL","smartgarden.herokuapp.com"-> wait for OK
71+
- AT+HTTPSSL=0 -> wait for OK (1 when URL starts with "https://")
72+
- AT+HTTPACTION=0 -> wait for 200
7273
- AT+HTTPREAD -> read buffer and parse it
7374
- AT+HTTPTERM -> wait for OK
7475
- AT+SAPBR=0,1
7576

7677
##### HTTP POST:
7778
- AT+HTTPINIT -> wait for OK
78-
- AT+HTTPPARA="CID",1 -> wait for OK
79-
- AT+HTTPPARA="URL","smartgarden.herokuapp.com" -> wait for OK
80-
79+
- AT+HTTPPARA="CID",1 -> wait for OK
80+
- AT+HTTPPARA="URL","smartgarden.herokuapp.com" -> wait for OK
81+
8182
For example, if we have this body: {"location_id": 238, "fill_percent": 90}
8283

8384
- AT+HTTPPARA="CONTENT","application/json"
8485
- AT+HTTPDATA=strlen(body),10000 -> wait for DOWNLOAD, then write the body and wait 10000
86+
- AT+HTTPSSL=0 -> wait for OK (1 when URL starts with "https://")
8587
- AT+HTTPACTION=1 -> wait for ,200,
8688
- AT+HTTPREAD -> read buffer and parse it
8789
- AT+HTTPTERM -> wait for OK

0 commit comments

Comments
 (0)