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

Commit 6168613

Browse files
authored
v2.4.0 for ESP32 and LwIP W5500 Ethernet
### Releases v2.4.0 1. Add support to ESP32 boards using `LwIP W5500 Ethernet` 2. Fix bug. Check [Fix "blank new line in chunk" bug #50](khoih-prog/AsyncHTTPRequest_Generic#50)
1 parent 8a3260f commit 6168613

File tree

1 file changed

+288
-0
lines changed

1 file changed

+288
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,288 @@
1+
/****************************************************************************************************************************
2+
AsyncHTTPSRequest_ESP32_W5500.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 AsyncHTTPRequest. 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 AsyncHTTPRequest 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 AsyncHTTPRequest. 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(ESP32) )
44+
#error This code is intended to run on the ESP32 platform! Please check your Tools->Board setting.
45+
#endif
46+
47+
#define ASYNC_HTTPS_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPSRequest_Generic v2.4.0"
48+
#define ASYNC_HTTPS_REQUEST_GENERIC_VERSION_MIN 2004000
49+
50+
/////////////////////////////////////////////////////////
51+
52+
// Uncomment for certain HTTP site to optimize
53+
//#define NOT_SEND_HEADER_AFTER_CONNECTED true
54+
55+
// Use larger queue size if necessary for large data transfer. Default is 512 bytes if not defined here
56+
//#define ASYNC_QUEUE_LENGTH 512
57+
58+
// Use larger priority if necessary. Default is 10 if not defined here. Must be > 4 or adjusted to 4
59+
//#define CONFIG_ASYNC_TCP_PRIORITY (12)
60+
61+
/////////////////////////////////////////////////////////
62+
63+
// Level from 0-4
64+
#define ASYNC_HTTPS_DEBUG_PORT Serial
65+
66+
#define _ASYNC_TCP_SSL_LOGLEVEL_ 1
67+
#define _ASYNC_HTTPS_LOGLEVEL_ 2
68+
#define _ETHERNET_WEBSERVER_LOGLEVEL_ 1
69+
70+
// 300s = 5 minutes to not flooding
71+
#define HTTPS_REQUEST_INTERVAL 60 //300
72+
73+
// 10s
74+
#define HEARTBEAT_INTERVAL 10
75+
76+
// Uncomment to use ESP32 core v1.0.6-
77+
//#define USING_CORE_ESP32_CORE_V200_PLUS false
78+
79+
//////////////////////////////////////////////////////////
80+
81+
// Optional values to override default settings
82+
// Don't change unless you know what you're doing
83+
//#define ETH_SPI_HOST SPI3_HOST
84+
//#define SPI_CLOCK_MHZ 25
85+
86+
// Must connect INT to GPIOxx or not working
87+
//#define INT_GPIO 4
88+
89+
//#define MISO_GPIO 19
90+
//#define MOSI_GPIO 23
91+
//#define SCK_GPIO 18
92+
//#define CS_GPIO 5
93+
94+
//////////////////////////////////////////////////////////
95+
96+
#include <WebServer_ESP32_W5500.h> // https://github.com/khoih-prog/WebServer_ESP32_W5500
97+
98+
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
99+
#include <AsyncHTTPSRequest_Generic.h> // https://github.com/khoih-prog/AsyncHTTPSRequest_Generic
100+
101+
#include <Ticker.h>
102+
103+
AsyncHTTPSRequest request;
104+
Ticker ticker;
105+
Ticker ticker1;
106+
107+
/////////////////////////////////////////////
108+
109+
// Enter a MAC address and IP address for your controller below.
110+
#define NUMBER_OF_MAC 20
111+
112+
byte mac[][NUMBER_OF_MAC] =
113+
{
114+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 },
115+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 },
116+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 },
117+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 },
118+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 },
119+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 },
120+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 },
121+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 },
122+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 },
123+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A },
124+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B },
125+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C },
126+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D },
127+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E },
128+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F },
129+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 },
130+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 },
131+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 },
132+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 },
133+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 },
134+
};
135+
136+
// Select the IP address according to your local network
137+
IPAddress myIP(192, 168, 2, 232);
138+
IPAddress myGW(192, 168, 2, 1);
139+
IPAddress mySN(255, 255, 255, 0);
140+
141+
// Google DNS Server IP
142+
IPAddress myDNS(8, 8, 8, 8);
143+
144+
/////////////////////////////////////////////
145+
146+
void heartBeatPrint()
147+
{
148+
static int num = 1;
149+
150+
if (ESP32_W5500_isConnected())
151+
Serial.print(F("H")); // H means connected
152+
else
153+
Serial.print(F("F")); // F means not connected
154+
155+
if (num == 80)
156+
{
157+
Serial.println();
158+
num = 1;
159+
}
160+
else if (num++ % 10 == 0)
161+
{
162+
Serial.print(F(" "));
163+
}
164+
}
165+
166+
void sendRequest()
167+
{
168+
static bool requestOpenResult;
169+
170+
if (request.readyState() == readyStateUnsent || request.readyState() == readyStateDone)
171+
{
172+
//requestOpenResult = request.open("GET", "https://worldtimeapi.org/api/timezone/Europe/London.txt");
173+
requestOpenResult = request.open("GET", "https://worldtimeapi.org/api/timezone/America/Toronto.txt");
174+
175+
if (requestOpenResult)
176+
{
177+
// Only send() if open() returns true, or crash
178+
request.send();
179+
}
180+
else
181+
{
182+
Serial.println(F("Can't send bad request"));
183+
}
184+
}
185+
else
186+
{
187+
Serial.println(F("Can't send request"));
188+
}
189+
}
190+
191+
void requestCB(void *optParm, AsyncHTTPSRequest *request, int readyState)
192+
{
193+
(void) optParm;
194+
195+
if (readyState == readyStateDone)
196+
{
197+
AHTTPS_LOGDEBUG0(F("\n**************************************\n"));
198+
AHTTPS_LOGDEBUG1(F("Response Code = "), request->responseHTTPString());
199+
200+
if (request->responseHTTPcode() == 200)
201+
{
202+
Serial.println(F("\n**************************************"));
203+
Serial.println(request->responseText());
204+
Serial.println(F("**************************************"));
205+
}
206+
207+
request->setDebug(false);
208+
}
209+
}
210+
211+
void setup()
212+
{
213+
// put your setup code here, to run once:
214+
Serial.begin(115200);
215+
216+
while (!Serial && millis() < 5000);
217+
218+
delay(200);
219+
220+
Serial.print(F("\nStarting AsyncHTTPSRequest_ESP32_W5500 using "));
221+
Serial.print(ARDUINO_BOARD);
222+
Serial.print(F(" with "));
223+
Serial.println(SHIELD_TYPE);
224+
Serial.println(WEBSERVER_ESP32_W5500_VERSION);
225+
Serial.println(ASYNC_TCP_SSL_VERSION);
226+
Serial.println(ASYNC_HTTPS_REQUEST_GENERIC_VERSION);
227+
228+
Serial.setDebugOutput(true);
229+
230+
#if defined(ASYNC_HTTPS_REQUEST_GENERIC_VERSION_MIN)
231+
232+
if (ASYNC_HTTPS_REQUEST_GENERIC_VERSION_INT < ASYNC_HTTPS_REQUEST_GENERIC_VERSION_MIN)
233+
{
234+
Serial.print(F("Warning. Must use this example on Version equal or later than : "));
235+
Serial.println(ASYNC_HTTPS_REQUEST_GENERIC_VERSION_MIN_TARGET);
236+
}
237+
238+
#endif
239+
240+
AHTTPS_LOGWARN(F("Default SPI pinout:"));
241+
AHTTPS_LOGWARN1(F("SPI_HOST:"), ETH_SPI_HOST);
242+
AHTTPS_LOGWARN1(F("MOSI:"), MOSI_GPIO);
243+
AHTTPS_LOGWARN1(F("MISO:"), MISO_GPIO);
244+
AHTTPS_LOGWARN1(F("SCK:"), SCK_GPIO);
245+
AHTTPS_LOGWARN1(F("CS:"), CS_GPIO);
246+
AHTTPS_LOGWARN1(F("INT:"), INT_GPIO);
247+
AHTTPS_LOGWARN1(F("SPI Clock (MHz):"), SPI_CLOCK_MHZ);
248+
AHTTPS_LOGWARN(F("========================="));
249+
250+
///////////////////////////////////
251+
252+
// To be called before ETH.begin()
253+
ESP32_W5500_onEvent();
254+
255+
// start the ethernet connection and the server:
256+
// Use DHCP dynamic IP and random mac
257+
uint16_t index = millis() % NUMBER_OF_MAC;
258+
259+
//bool begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPI_CLOCK_MHZ,
260+
// int SPI_HOST, uint8_t *W5500_Mac = W5500_Default_Mac);
261+
//ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST );
262+
ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST, mac[index] );
263+
264+
// Static IP, leave without this line to get IP via DHCP
265+
//bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
266+
//ETH.config(myIP, myGW, mySN, myDNS);
267+
268+
ESP32_W5500_waitForConnect();
269+
270+
///////////////////////////////////
271+
272+
Serial.print(F("\nHTTP WebClient is @ IP : "));
273+
Serial.println(ETH.localIP());
274+
275+
request.setDebug(false);
276+
277+
request.onReadyStateChange(requestCB);
278+
ticker.attach(HTTPS_REQUEST_INTERVAL, sendRequest);
279+
280+
ticker1.attach(HEARTBEAT_INTERVAL, heartBeatPrint);
281+
282+
// Send first request now
283+
sendRequest();
284+
}
285+
286+
void loop()
287+
{
288+
}

0 commit comments

Comments
 (0)