This repository has been archived by the owner on Feb 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
AsyncHTTP_HTTPSRequest_ESP.ino
402 lines (316 loc) · 13.2 KB
/
AsyncHTTP_HTTPSRequest_ESP.ino
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/****************************************************************************************************************************
AsyncHTTP_HTTPSRequest_ESP.ino - Dead simple AsyncHTTPSRequest for ESP8266, ESP32 and currently STM32 with built-in LAN8742A Ethernet
For ESP8266, ESP32 and STM32 with built-in LAN8742A Ethernet (Nucleo-144, DISCOVERY, etc)
AsyncHTTPSRequest_Generic is a library for the ESP8266, ESP32 and currently STM32 run built-in Ethernet WebServer
Based on and modified from AsyncHTTPRequest Library (https://github.com/boblemaire/AsyncHTTPRequest)
Built by Khoi Hoang https://github.com/khoih-prog/AsyncHTTPSRequest_Generic
Licensed under MIT license
Copyright (C) <2018> <Bob Lemaire, IoTaWatt, Inc.>
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
as published bythe Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*****************************************************************************************************************************/
//************************************************************************************************************
//
// There are scores of ways to use AsyncHTTPSRequest. The important thing to keep in mind is that
// it is asynchronous and just like in JavaScript, everything is event driven. You will have some
// reason to initiate an asynchronous HTTP request in your program, but then sending the request
// headers and payload, gathering the response headers and any payload, and processing
// of that response, can (and probably should) all be done asynchronously.
//
// In this example, a Ticker function is setup to fire every 300 seconds to initiate a request.
// Everything is handled in AsyncHTTPSRequest without blocking.
// The callback onReadyStateChange is made progressively and like most JS scripts, we look for
// readyState == 4 (complete) here. At that time the response is retrieved and printed.
//
// Note that there is no code in loop(). A code entered into loop would run oblivious to
// the ongoing HTTP requests. The Ticker could be removed and periodic calls to sendRequest()
// could be made in loop(), resulting in the same asynchronous handling.
//
// For demo purposes, debug is turned on for handling of the first request. These are the
// events that are being handled in AsyncHTTPSRequest. They all begin with Debug(nnn) where
// nnn is the elapsed time in milliseconds since the transaction was started.
//
//*************************************************************************************************************
#if !( defined(ESP8266) || defined(ESP32) )
#error This code is intended to run on the ESP8266 or ESP32 platform! Please check your Tools->Board setting.
#endif
#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.10.2"
#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1010002
#define ASYNC_HTTPS_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPSRequest_Generic v2.2.1"
#define ASYNC_HTTPS_REQUEST_GENERIC_VERSION_MIN 2002001
/////////////////////////////////////////////////////////
// Uncomment for certain HTTP site to optimize
//#define NOT_SEND_HEADER_AFTER_CONNECTED true
// Level from 0-4
#define ASYNC_HTTPS_DEBUG_PORT Serial
#define _ASYNC_TCP_SSL_LOGLEVEL_ 1
#define _ASYNC_HTTPS_LOGLEVEL_ 1
// 300s = 5 minutes to not flooding
#define HTTPS_REQUEST_INTERVAL 120
// 10s
#define HEARTBEAT_INTERVAL 10
int status; // the Wifi radio's status
const char* ssid = "your_ssid";
const char* password = "your_pass";
#if (ESP8266)
#include <ESP8266WiFi.h>
#elif (ESP32)
#include <WiFi.h>
#endif
// Use larger queue size if necessary for large data transfer. Default is 512 bytes if not defined here
//#define ASYNC_QUEUE_LENGTH 512
// Use larger priority if necessary. Default is 10 if not defined here. Must be > 4 or adjusted to 4
//#define CONFIG_ASYNC_TCP_PRIORITY (12)
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
// If use both AsyncHTTPRequest_Generic and AsyncHTTPSRequest_Generic, include AsyncHTTPRequest_Generic first or error
// because many definitions of AsyncHTTPSRequest_Generic rely on those of AsyncHTTPRequest_Generic
#include <AsyncHTTPRequest_Generic.h> // https://github.com/khoih-prog/AsyncHTTPRequest_Generic
#include <AsyncHTTPSRequest_Generic.h> // https://github.com/khoih-prog/AsyncHTTPSRequest_Generic
#include <Ticker.h>
#define NUM_DIFFERENT_SITES 3
const char* addreses[][NUM_DIFFERENT_SITES] =
{
{"https://worldtimeapi.org/api/timezone/America/Toronto.txt", "https://worldtimeapi.org/api/timezone/Europe/Prague.txt"},
{"http://worldtimeapi.org/api/timezone/Europe/London.txt", "http://worldtimeapi.org/api/timezone/America/Vancouver.txt"},
{"http://www.myexternalip.com/raw"}
};
#define NUM_ENTRIES_SITE_0 2
#define NUM_ENTRIES_SITE_1 2
#define NUM_ENTRIES_SITE_2 1
byte reqCount[] = { NUM_ENTRIES_SITE_0, NUM_ENTRIES_SITE_1, NUM_ENTRIES_SITE_2 };
bool readySend[] = { true, true, true };
typedef enum
{
HTTP_REQUEST = 0,
HTTPS_REQUEST = 1,
} HTTP_Type;
AsyncHTTPSRequest request0;
AsyncHTTPRequest request1;
AsyncHTTPRequest request2;
typedef struct _AsyncHTTPRequestData
{
void* request; // (void*) for AsyncHTTPRequest* or AsyncHTTPSRequest*
HTTP_Type httpType;
} AsyncHTTPRequestData;
AsyncHTTPRequestData myAsyncHTTPRequestData[] =
{
{ (void*) &request0, HTTPS_REQUEST },
{ (void*) &request1, HTTP_REQUEST },
{ (void*) &request2, HTTP_REQUEST }
};
// This is for HTTPS and must use AsyncHTTPSRequest
void requestCB0(void* optParm, AsyncHTTPSRequest* thisRequest, int readyState);
// This is for HTTP and must use AsyncHTTPRequest
void requestCB1(void* optParm, AsyncHTTPRequest* thisRequest, int readyState);
// This is for HTTP and must use AsyncHTTPRequest
void requestCB2(void* optParm, AsyncHTTPRequest* thisRequest, int readyState);
void sendRequest0();
void sendRequest1();
void sendRequest2();
typedef void (*requestCallback0)(void* optParm, AsyncHTTPSRequest* thisRequest, int readyState);
typedef void (*requestCallback1)(void* optParm, AsyncHTTPRequest* thisRequest, int readyState);
typedef void (*sendCallback)();
void* requestCB [] = { (void*) requestCB0, (void*) requestCB1, (void*) requestCB2 };
sendCallback sendRequestCB [] = { sendRequest0, sendRequest1, sendRequest2 };
Ticker ticker;
Ticker ticker1;
void heartBeatPrint()
{
static int num = 1;
if (WiFi.status() == WL_CONNECTED)
Serial.print(F("H")); // H means connected to WiFi
else
Serial.print(F("F")); // F means not connected to WiFi
if (num == 80)
{
Serial.println();
num = 1;
}
else if (num++ % 10 == 0)
{
Serial.print(F(" "));
}
}
void sendRequest(uint16_t index)
{
static bool requestOpenResult;
reqCount[index]--;
readySend[index] = false;
if ( myAsyncHTTPRequestData[index].httpType == HTTPS_REQUEST )
{
requestOpenResult = ((AsyncHTTPSRequest *) myAsyncHTTPRequestData[index].request)->open("GET",
addreses[index][reqCount[index]]);
if (requestOpenResult)
{
// Only send() if open() returns true, or crash
Serial.print("\nSending HTTPS request: ");
((AsyncHTTPSRequest *) myAsyncHTTPRequestData[index].request)->send();
}
else
{
Serial.print("\nCan't send bad HTTPS request : ");
}
}
else if ( myAsyncHTTPRequestData[index].httpType == HTTP_REQUEST )
{
requestOpenResult = ((AsyncHTTPRequest *) myAsyncHTTPRequestData[index].request)->open("GET",
addreses[index][reqCount[index]]);
if (requestOpenResult)
{
// Only send() if open() returns true, or crash
Serial.print("\nSending HTTP request: ");
((AsyncHTTPRequest *) myAsyncHTTPRequestData[index].request)->send();
}
else
{
Serial.print("\nCan't send bad HTTP request : ");
}
}
Serial.println(addreses[index][reqCount[index]]);
}
void sendRequest0()
{
sendRequest(0);
}
void sendRequest1()
{
sendRequest(1);
}
void sendRequest2()
{
sendRequest(2);
}
void sendRequests()
{
for (int index = 0; index < NUM_DIFFERENT_SITES; index++)
{
reqCount[index] = 2;
}
reqCount[0] = NUM_ENTRIES_SITE_0;
reqCount[1] = NUM_ENTRIES_SITE_1;
reqCount[2] = NUM_ENTRIES_SITE_2;
}
// This is for HTTPS and must use AsyncHTTPSRequest
void requestCB0(void *optParm, AsyncHTTPSRequest *thisRequest, int readyState)
{
(void) optParm;
if (readyState == readyStateDone)
{
AHTTPS_LOGDEBUG0(F("\n**************************************\n"));
AHTTPS_LOGDEBUG1(F("Response Code = "), thisRequest->responseHTTPString());
if (thisRequest->responseHTTPcode() == 200)
{
Serial.println(F("\n**************************************"));
Serial.println(thisRequest->responseText());
Serial.println(F("**************************************"));
}
thisRequest->setDebug(false);
readySend[0] = true;
}
}
// This is for HTTP and must use AsyncHTTPRequest
void requestCB1(void *optParm, AsyncHTTPRequest *thisRequest, int readyState)
{
(void) optParm;
if (readyState == readyStateDone)
{
AHTTPS_LOGDEBUG0(F("\n**************************************\n"));
AHTTPS_LOGDEBUG1(F("Response Code = "), thisRequest->responseHTTPString());
if (thisRequest->responseHTTPcode() == 200)
{
Serial.println(F("\n**************************************"));
Serial.println(thisRequest->responseText());
Serial.println(F("**************************************"));
}
thisRequest->setDebug(false);
readySend[1] = true;
}
}
// This is for HTTP and must use AsyncHTTPRequest
void requestCB2(void *optParm, AsyncHTTPRequest *thisRequest, int readyState)
{
(void) optParm;
if (readyState == readyStateDone)
{
AHTTPS_LOGDEBUG0(F("\n**************************************\n"));
AHTTPS_LOGDEBUG1(F("Response Code = "), thisRequest->responseHTTPString());
if (thisRequest->responseHTTPcode() == 200)
{
Serial.println(F("\n**************************************"));
Serial.println(thisRequest->responseText());
Serial.println(F("**************************************"));
}
thisRequest->setDebug(false);
readySend[2] = true;
}
}
void setup()
{
// put your setup code here, to run once:
Serial.begin(115200);
while (!Serial && millis() < 5000);
delay(200);
Serial.print("\nStarting AsyncHTTP_HTTPSRequest_ESP on ");
Serial.println(ARDUINO_BOARD);
#if defined(ESP32)
Serial.println(ASYNC_TCP_SSL_VERSION);
#else
//Serial.println(ESPASYNC_TCP_SSL_VERSION);
#endif
Serial.println(ASYNC_HTTPS_REQUEST_GENERIC_VERSION);
Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION);
#if defined(ASYNC_HTTPS_REQUEST_GENERIC_VERSION_MIN)
if (ASYNC_HTTPS_REQUEST_GENERIC_VERSION_INT < ASYNC_HTTPS_REQUEST_GENERIC_VERSION_MIN)
{
Serial.print(F("Warning. Must use this example on Version equal or later than : "));
Serial.println(ASYNC_HTTPS_REQUEST_GENERIC_VERSION_MIN_TARGET);
}
#endif
#if defined(ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN)
if (ASYNC_HTTP_REQUEST_GENERIC_VERSION_INT < ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN)
{
Serial.print(F("Warning. Must use this example on Version equal or later than : "));
Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET);
}
#endif
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("Connecting to WiFi SSID: " + String(ssid));
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.print(F("\nAsyncHTTPSRequest @ IP : "));
Serial.println(WiFi.localIP());
for (int index = 0; index < NUM_DIFFERENT_SITES; index++)
{
if ( myAsyncHTTPRequestData[index].httpType == HTTPS_REQUEST )
{
((AsyncHTTPSRequest *) myAsyncHTTPRequestData[index].request)->setDebug(false);
((AsyncHTTPSRequest *) myAsyncHTTPRequestData[index].request)->onReadyStateChange( (requestCallback0) requestCB[index]);
}
else if ( myAsyncHTTPRequestData[index].httpType == HTTP_REQUEST )
{
((AsyncHTTPRequest *) myAsyncHTTPRequestData[index].request)->setDebug(false);
((AsyncHTTPRequest *) myAsyncHTTPRequestData[index].request)->onReadyStateChange((requestCallback1) requestCB[index]);
}
}
ticker.attach(HTTPS_REQUEST_INTERVAL, sendRequests);
ticker1.attach(HEARTBEAT_INTERVAL, heartBeatPrint);
}
void loop()
{
for (int index = 0; index < NUM_DIFFERENT_SITES; index++)
{
if ((reqCount[index] > 0) && readySend[index])
{
sendRequestCB[index]();
// Don't reduce this or possible crash. TLS needs long time to work.
delay(100);
}
}
}