Skip to content
This repository was archived by the owner on Feb 4, 2023. It is now read-only.

Commit 2fb7c67

Browse files
authored
v2.1.3 not to reconnect after connected
### Releases v2.1.3 1. Not try to reconnect to the same host:port after connected. Check [setReuse feature #12](#12)
1 parent 6b028a5 commit 2fb7c67

File tree

1 file changed

+389
-0
lines changed

1 file changed

+389
-0
lines changed
Lines changed: 389 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,389 @@
1+
/****************************************************************************************************************************
2+
AsyncHTTP_HTTPSRequest_ESP.ino - Dead simple AsyncHTTPSRequest for ESP8266, ESP32 and currently STM32 with built-in LAN8742A Ethernet
3+
4+
For ESP8266, ESP32 and STM32 with built-in LAN8742A Ethernet (Nucleo-144, DISCOVERY, etc)
5+
6+
AsyncHTTPSRequest_Generic is a library for the ESP8266, ESP32 and currently STM32 run built-in Ethernet WebServer
7+
8+
Based on and modified from AsyncHTTPRequest Library (https://github.com/boblemaire/AsyncHTTPRequest)
9+
10+
Built by Khoi Hoang https://github.com/khoih-prog/AsyncHTTPSRequest_Generic
11+
Licensed under MIT license
12+
13+
Copyright (C) <2018> <Bob Lemaire, IoTaWatt, Inc.>
14+
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
15+
as published bythe Free Software Foundation, either version 3 of the License, or (at your option) any later version.
16+
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18+
You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
*****************************************************************************************************************************/
20+
//************************************************************************************************************
21+
//
22+
// There are scores of ways to use AsyncHTTPSRequest. The important thing to keep in mind is that
23+
// it is asynchronous and just like in JavaScript, everything is event driven. You will have some
24+
// reason to initiate an asynchronous HTTP request in your program, but then sending the request
25+
// headers and payload, gathering the response headers and any payload, and processing
26+
// of that response, can (and probably should) all be done asynchronously.
27+
//
28+
// In this example, a Ticker function is setup to fire every 300 seconds to initiate a request.
29+
// Everything is handled in AsyncHTTPSRequest without blocking.
30+
// The callback onReadyStateChange is made progressively and like most JS scripts, we look for
31+
// readyState == 4 (complete) here. At that time the response is retrieved and printed.
32+
//
33+
// Note that there is no code in loop(). A code entered into loop would run oblivious to
34+
// the ongoing HTTP requests. The Ticker could be removed and periodic calls to sendRequest()
35+
// could be made in loop(), resulting in the same asynchronous handling.
36+
//
37+
// For demo purposes, debug is turned on for handling of the first request. These are the
38+
// events that are being handled in AsyncHTTPSRequest. They all begin with Debug(nnn) where
39+
// nnn is the elapsed time in milliseconds since the transaction was started.
40+
//
41+
//*************************************************************************************************************
42+
43+
#if !( defined(ESP8266) || defined(ESP32) )
44+
#error This code is intended to run on the ESP8266 or ESP32 platform! Please check your Tools->Board setting.
45+
#endif
46+
47+
#define ASYNC_HTTPS_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPSRequest_Generic v2.1.3"
48+
#define ASYNC_HTTPS_REQUEST_GENERIC_VERSION_MIN 2001003
49+
50+
#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.9.1"
51+
#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1009001
52+
53+
// Level from 0-4
54+
#define ASYNC_HTTPS_DEBUG_PORT Serial
55+
56+
#define _ASYNC_TCP_SSL_LOGLEVEL_ 1
57+
#define _ASYNC_HTTPS_LOGLEVEL_ 2
58+
59+
// 300s = 5 minutes to not flooding
60+
#define HTTPS_REQUEST_INTERVAL 120
61+
62+
// 10s
63+
#define HEARTBEAT_INTERVAL 10
64+
65+
int status; // the Wifi radio's status
66+
67+
const char* ssid = "your_ssid";
68+
const char* password = "your_pass";
69+
70+
#if (ESP8266)
71+
#include <ESP8266WiFi.h>
72+
#elif (ESP32)
73+
#include <WiFi.h>
74+
#endif
75+
76+
// Use larger queue size if necessary for large data transfer. Default is 512 bytes if not defined here
77+
//#define ASYNC_QUEUE_LENGTH 512
78+
79+
// Use larger priority if necessary. Default is 10 if not defined here. Must be > 4 or adjusted to 4
80+
//#define CONFIG_ASYNC_TCP_PRIORITY (12)
81+
82+
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
83+
// If use both AsyncHTTPRequest_Generic and AsyncHTTPSRequest_Generic, include AsyncHTTPRequest_Generic first or error
84+
// because many definitions of AsyncHTTPSRequest_Generic rely on those of AsyncHTTPRequest_Generic
85+
#include <AsyncHTTPRequest_Generic.h> // https://github.com/khoih-prog/AsyncHTTPRequest_Generic
86+
#include <AsyncHTTPSRequest_Generic.h> // https://github.com/khoih-prog/AsyncHTTPSRequest_Generic
87+
88+
#include <Ticker.h>
89+
90+
#define NUM_DIFFERENT_SITES 3
91+
92+
const char* addreses[][NUM_DIFFERENT_SITES] =
93+
{
94+
{"https://worldtimeapi.org/api/timezone/America/Toronto.txt", "https://worldtimeapi.org/api/timezone/Europe/Prague.txt"},
95+
{"http://worldtimeapi.org/api/timezone/Europe/London.txt", "http://worldtimeapi.org/api/timezone/America/Vancouver.txt"},
96+
{"http://www.myexternalip.com/raw"}
97+
};
98+
99+
#define NUM_ENTRIES_SITE_0 2
100+
#define NUM_ENTRIES_SITE_1 2
101+
#define NUM_ENTRIES_SITE_2 1
102+
103+
byte reqCount[] = { NUM_ENTRIES_SITE_0, NUM_ENTRIES_SITE_1, NUM_ENTRIES_SITE_2 };
104+
bool readySend[] = { true, true, true };
105+
106+
typedef enum
107+
{
108+
HTTP_REQUEST = 0,
109+
HTTPS_REQUEST = 1,
110+
} HTTP_Type;
111+
112+
AsyncHTTPSRequest request0;
113+
AsyncHTTPRequest request1;
114+
AsyncHTTPRequest request2;
115+
116+
typedef struct _AsyncHTTPRequestData
117+
{
118+
void* request; // (void*) for AsyncHTTPRequest* or AsyncHTTPSRequest*
119+
HTTP_Type httpType;
120+
} AsyncHTTPRequestData;
121+
122+
AsyncHTTPRequestData myAsyncHTTPRequestData[] =
123+
{
124+
{ (void*) &request0, HTTPS_REQUEST },
125+
{ (void*) &request1, HTTP_REQUEST },
126+
{ (void*) &request2, HTTP_REQUEST }
127+
};
128+
129+
// This is for HTTPS and must use AsyncHTTPSRequest
130+
void requestCB0(void* optParm, AsyncHTTPSRequest* thisRequest, int readyState);
131+
132+
// This is for HTTP and must use AsyncHTTPRequest
133+
void requestCB1(void* optParm, AsyncHTTPRequest* thisRequest, int readyState);
134+
135+
// This is for HTTP and must use AsyncHTTPRequest
136+
void requestCB2(void* optParm, AsyncHTTPRequest* thisRequest, int readyState);
137+
138+
void sendRequest0();
139+
void sendRequest1();
140+
void sendRequest2();
141+
142+
typedef void (*requestCallback0)(void* optParm, AsyncHTTPSRequest* thisRequest, int readyState);
143+
typedef void (*requestCallback1)(void* optParm, AsyncHTTPRequest* thisRequest, int readyState);
144+
145+
typedef void (*sendCallback)();
146+
147+
void* requestCB [] = { (void*) requestCB0, (void*) requestCB1, (void*) requestCB2 };
148+
sendCallback sendRequestCB [] = { sendRequest0, sendRequest1, sendRequest2 };
149+
150+
Ticker ticker;
151+
Ticker ticker1;
152+
153+
void heartBeatPrint()
154+
{
155+
static int num = 1;
156+
157+
if (WiFi.status() == WL_CONNECTED)
158+
Serial.print(F("H")); // H means connected to WiFi
159+
else
160+
Serial.print(F("F")); // F means not connected to WiFi
161+
162+
if (num == 80)
163+
{
164+
Serial.println();
165+
num = 1;
166+
}
167+
else if (num++ % 10 == 0)
168+
{
169+
Serial.print(F(" "));
170+
}
171+
}
172+
173+
void sendRequest(uint16_t index)
174+
{
175+
static bool requestOpenResult;
176+
177+
reqCount[index]--;
178+
readySend[index] = false;
179+
180+
if ( myAsyncHTTPRequestData[index].httpType == HTTPS_REQUEST )
181+
{
182+
requestOpenResult = ((AsyncHTTPSRequest *) myAsyncHTTPRequestData[index].request)->open("GET", addreses[index][reqCount[index]]);
183+
184+
if (requestOpenResult)
185+
{
186+
// Only send() if open() returns true, or crash
187+
Serial.print("\nSending HTTPS request: ");
188+
((AsyncHTTPSRequest *) myAsyncHTTPRequestData[index].request)->send();
189+
}
190+
else
191+
{
192+
Serial.print("\nCan't send bad HTTPS request : ");
193+
}
194+
}
195+
else if ( myAsyncHTTPRequestData[index].httpType == HTTP_REQUEST )
196+
{
197+
requestOpenResult = ((AsyncHTTPRequest *) myAsyncHTTPRequestData[index].request)->open("GET", addreses[index][reqCount[index]]);
198+
199+
if (requestOpenResult)
200+
{
201+
// Only send() if open() returns true, or crash
202+
Serial.print("\nSending HTTP request: ");
203+
((AsyncHTTPRequest *) myAsyncHTTPRequestData[index].request)->send();
204+
}
205+
else
206+
{
207+
Serial.print("\nCan't send bad HTTP request : ");
208+
}
209+
}
210+
211+
Serial.println(addreses[index][reqCount[index]]);
212+
}
213+
214+
void sendRequest0()
215+
{
216+
sendRequest(0);
217+
}
218+
219+
void sendRequest1()
220+
{
221+
sendRequest(1);
222+
}
223+
224+
void sendRequest2()
225+
{
226+
sendRequest(2);
227+
}
228+
229+
void sendRequests()
230+
{
231+
for (int index = 0; index < NUM_DIFFERENT_SITES; index++)
232+
{
233+
reqCount[index] = 2;
234+
}
235+
236+
reqCount[0] = NUM_ENTRIES_SITE_0;
237+
reqCount[1] = NUM_ENTRIES_SITE_1;
238+
reqCount[2] = NUM_ENTRIES_SITE_2;
239+
}
240+
241+
// This is for HTTPS and must use AsyncHTTPSRequest
242+
void requestCB0(void *optParm, AsyncHTTPSRequest *thisRequest, int readyState)
243+
{
244+
(void) optParm;
245+
246+
if (readyState == readyStateDone)
247+
{
248+
AHTTPS_LOGWARN0(F("\n**************************************\n"));
249+
AHTTPS_LOGWARN1(F("Response Code = "), thisRequest->responseHTTPString());
250+
251+
if (thisRequest->responseHTTPcode() == 200)
252+
{
253+
Serial.println(F("\n**************************************"));
254+
Serial.println(thisRequest->responseText());
255+
Serial.println(F("**************************************"));
256+
}
257+
258+
thisRequest->setDebug(false);
259+
readySend[0] = true;
260+
}
261+
}
262+
263+
// This is for HTTP and must use AsyncHTTPRequest
264+
void requestCB1(void *optParm, AsyncHTTPRequest *thisRequest, int readyState)
265+
{
266+
(void) optParm;
267+
268+
if (readyState == readyStateDone)
269+
{
270+
AHTTP_LOGWARN0(F("\n**************************************\n"));
271+
AHTTP_LOGWARN1(F("Response Code = "), thisRequest->responseHTTPString());
272+
273+
if (thisRequest->responseHTTPcode() == 200)
274+
{
275+
Serial.println(F("\n**************************************"));
276+
Serial.println(thisRequest->responseText());
277+
Serial.println(F("**************************************"));
278+
}
279+
280+
thisRequest->setDebug(false);
281+
readySend[1] = true;
282+
}
283+
}
284+
285+
// This is for HTTP and must use AsyncHTTPRequest
286+
void requestCB2(void *optParm, AsyncHTTPRequest *thisRequest, int readyState)
287+
{
288+
(void) optParm;
289+
290+
if (readyState == readyStateDone)
291+
{
292+
AHTTP_LOGWARN0(F("\n**************************************\n"));
293+
AHTTP_LOGWARN1(F("Response Code = "), thisRequest->responseHTTPString());
294+
295+
if (thisRequest->responseHTTPcode() == 200)
296+
{
297+
Serial.println(F("\n**************************************"));
298+
Serial.println(thisRequest->responseText());
299+
Serial.println(F("**************************************"));
300+
}
301+
302+
thisRequest->setDebug(false);
303+
readySend[2] = true;
304+
}
305+
}
306+
307+
308+
void setup()
309+
{
310+
// put your setup code here, to run once:
311+
Serial.begin(115200);
312+
while (!Serial && millis() < 5000);
313+
314+
delay(200);
315+
316+
Serial.print("\nStarting AsyncHTTP_HTTPSRequest_ESP on "); Serial.println(ARDUINO_BOARD);
317+
318+
#if defined(ESP32)
319+
Serial.println(ASYNC_TCP_SSL_VERSION);
320+
#else
321+
//Serial.println(ESPASYNC_TCP_SSL_VERSION);
322+
#endif
323+
324+
Serial.println(ASYNC_HTTPS_REQUEST_GENERIC_VERSION);
325+
Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION);
326+
327+
#if defined(ASYNC_HTTPS_REQUEST_GENERIC_VERSION_MIN)
328+
if (ASYNC_HTTPS_REQUEST_GENERIC_VERSION_INT < ASYNC_HTTPS_REQUEST_GENERIC_VERSION_MIN)
329+
{
330+
Serial.print(F("Warning. Must use this example on Version equal or later than : "));
331+
Serial.println(ASYNC_HTTPS_REQUEST_GENERIC_VERSION_MIN_TARGET);
332+
}
333+
#endif
334+
335+
#if defined(ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN)
336+
if (ASYNC_HTTP_REQUEST_GENERIC_VERSION_INT < ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN)
337+
{
338+
Serial.print(F("Warning. Must use this example on Version equal or later than : "));
339+
Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET);
340+
}
341+
#endif
342+
343+
WiFi.mode(WIFI_STA);
344+
345+
WiFi.begin(ssid, password);
346+
347+
Serial.println("Connecting to WiFi SSID: " + String(ssid));
348+
349+
while (WiFi.status() != WL_CONNECTED)
350+
{
351+
delay(500);
352+
Serial.print(".");
353+
}
354+
355+
Serial.print(F("\nAsyncHTTPSRequest @ IP : "));
356+
Serial.println(WiFi.localIP());
357+
358+
for (int index = 0; index < NUM_DIFFERENT_SITES; index++)
359+
{
360+
if ( myAsyncHTTPRequestData[index].httpType == HTTPS_REQUEST )
361+
{
362+
((AsyncHTTPSRequest *) myAsyncHTTPRequestData[index].request)->setDebug(false);
363+
((AsyncHTTPSRequest *) myAsyncHTTPRequestData[index].request)->onReadyStateChange( (requestCallback0) requestCB[index]);
364+
}
365+
else if ( myAsyncHTTPRequestData[index].httpType == HTTP_REQUEST )
366+
{
367+
((AsyncHTTPRequest *) myAsyncHTTPRequestData[index].request)->setDebug(false);
368+
((AsyncHTTPRequest *) myAsyncHTTPRequestData[index].request)->onReadyStateChange((requestCallback1) requestCB[index]);
369+
}
370+
}
371+
372+
ticker.attach(HTTPS_REQUEST_INTERVAL, sendRequests);
373+
374+
ticker1.attach(HEARTBEAT_INTERVAL, heartBeatPrint);
375+
}
376+
377+
void loop()
378+
{
379+
for (int index = 0; index < NUM_DIFFERENT_SITES; index++)
380+
{
381+
if ((reqCount[index] > 0) && readySend[index])
382+
{
383+
sendRequestCB[index]();
384+
385+
// Don't reduce this or possible crash. TLS needs long time to work.
386+
delay(100);
387+
}
388+
}
389+
}

0 commit comments

Comments
 (0)