forked from carrascoacd/ArduinoSIM800L
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttp.cpp
211 lines (175 loc) · 5.47 KB
/
Http.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
* Http.cpp
* A HTTP library for the SIM800L board
*
* Copyright 2021 Antonio Carrasco
*
* The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#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";
const char HTTP_PARA[] PROGMEM = "AT+HTTPPARA=\"URL\",\"%s\"\r\n";
const char HTTP_GET[] PROGMEM = "AT+HTTPACTION=0\r\n";
const char HTTP_POST[] PROGMEM = "AT+HTTPACTION=1\n";
const char HTTP_DATA[] PROGMEM = "AT+HTTPDATA=%d,%d\r\n";
const char HTTP_READ[] PROGMEM = "AT+HTTPREAD\r\n";
const char HTTP_CLOSE[] PROGMEM = "AT+HTTPTERM\r\n";
const char HTTP_CONTENT[] PROGMEM = "AT+HTTPPARA=\"CONTENT\",\"application/json\"\r\n";
const char HTTPS_ENABLE[] PROGMEM = "AT+HTTPSSL=1\r\n";
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 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://";
Result HTTP::connect(const char *apn)
{
Result result = openGPRSContext(this, apn);
if (sendCmdAndWaitForResp_P(HTTP_INIT, AT_OK, 8000) == FALSE)
result = ERROR_HTTP_INIT;
return result;
}
Result HTTP::disconnect()
{
Result result = closeGPRSContext(this);
if (sendCmdAndWaitForResp_P(HTTP_CLOSE, AT_OK, 5000) == FALSE)
result = ERROR_HTTP_CLOSE;
return result;
}
Result HTTP::post(const char *uri, const char *body, char *response)
{
Result result;
setHTTPSession(uri);
char buffer[32];
char resp[16];
unsigned int delayToDownload = 10000;
sprintf_P(buffer, HTTP_DATA, strlen(body), delayToDownload);
strcpy_P(resp, DOWNLOAD);
sendCmdAndWaitForResp(buffer, resp, 7000);
purgeSerial();
sendCmd(body);
if (sendCmdAndWaitForResp_P(HTTP_POST, HTTP_2XX, delayToDownload) == TRUE)
{
strcpy_P(buffer, HTTP_READ);
sendCmd(buffer);
readResponse(response);
result = SUCCESS;
}
else
{
result = ERROR_HTTP_POST;
}
return result;
}
Result HTTP::get(const char *uri, char *response)
{
Result result;
setHTTPSession(uri);
if (sendCmdAndWaitForResp_P(HTTP_GET, HTTP_2XX, 7000) == TRUE)
{
char buffer[16];
strcpy_P(buffer, HTTP_READ);
sendCmd(buffer);
result = SUCCESS;
readResponse(response);
}
else
{
result = ERROR_HTTP_GET;
}
return result;
}
unsigned int HTTP::readVoltage()
{
char buffer[32];
char voltage[8];
cleanBuffer(buffer, sizeof(buffer));
cleanBuffer(voltage, sizeof(voltage));
strcpy_P(buffer, READ_VOLTAGE);
sendCmd(buffer, 500);
if (readBuffer(buffer, sizeof(buffer)) == TRUE)
{
parseATResponse(buffer, 4, 7, voltage);
}
return atoi(voltage);
}
unsigned int HTTP::readVoltagePercentage()
{
char buffer[32];
char voltage[8];
cleanBuffer(buffer, sizeof(buffer));
cleanBuffer(voltage, sizeof(voltage));
strcpy_P(buffer, READ_VOLTAGE);
sendCmd(buffer);
if (readBuffer(buffer, sizeof(buffer)) == TRUE)
{
parseATResponse(buffer, 2, 4, voltage);
}
return atoi(voltage);
}
unsigned int HTTP::readSignalStrength()
{
char buffer[32];
char signals[8];
cleanBuffer(buffer, sizeof(buffer));
cleanBuffer(signals, sizeof(signals));
strcpy_P(buffer, SIGNAL_QUALITY);
sendCmd(buffer);
if (readBuffer(buffer, sizeof(buffer)) == TRUE)
{
parseATResponse(buffer, 2, 2, signals);
}
return atoi(signals);
}
Result HTTP::setHTTPSession(const char *uri)
{
Result result = SUCCESS;
char buffer[128];
if (sendCmdAndWaitForResp_P(HTTP_CID, AT_OK, 2000) == FALSE)
result = ERROR_HTTP_CID;
sprintf_P(buffer, HTTP_PARA, uri);
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, AT_OK, 2000) == FALSE)
{
result = https ? ERROR_HTTPS_ENABLE : ERROR_HTTPS_DISABLE;
}
if (sendCmdAndWaitForResp_P(HTTP_CONTENT, AT_OK, 2000) == FALSE)
result = ERROR_HTTP_CONTENT;
return result;
}
void HTTP::readResponse(char *response)
{
char buffer[128];
cleanBuffer(buffer, sizeof(buffer));
cleanBuffer(response, sizeof(response));
if (readBuffer(buffer, sizeof(buffer)) == TRUE)
{
parseJSONResponse(buffer, sizeof(buffer), response);
}
}