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

Commit b61291e

Browse files
authored
v2.6.0 for LwIP W6100
### Releases v2.6.0 1. Add support to `ESP32` and `ESP32S2/S3/C3` boards using `LwIP W6100 Ethernet`
1 parent 79c3076 commit b61291e

File tree

2 files changed

+586
-0
lines changed

2 files changed

+586
-0
lines changed
Lines changed: 300 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,300 @@
1+
/****************************************************************************************************************************
2+
AsyncHTTPSRequest_ESP32_W6100.ino - Dead simple AsyncHTTPSRequest for ESP8266, ESP32 and currently STM32 with built-in LAN8742A Ethernet
3+
4+
For ESP32 using LwIP W6100 / W6100 / ENC28J60 / LAN8720 Ethernet
5+
6+
AsyncHTTPSRequest_ESP32_Ethernet is a library for ESP32 using LwIP W6100 / W6100 / ENC28J60 / LAN8720 Ethernet
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_ESP32_Ethernet
11+
Licensed under GPLv3 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 AsyncHTTPSRequest_ESP32_Ethernet library is currently supporting only ESP32 using LwIP Ethernet
45+
#endif
46+
47+
#define ASYNC_HTTPS_REQUEST_ESP32_ETHERNET_VERSION_MIN_TARGET "AsyncHTTPSRequest_ESP32_Ethernet v2.5.0"
48+
#define ASYNC_HTTPS_REQUEST_ESP32_ETHERNET_VERSION_MIN 2005000
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+
// For ESP32-S3
82+
// Optional values to override default settings
83+
//#define ETH_SPI_HOST SPI2_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 13
90+
//#define MOSI_GPIO 11
91+
//#define SCK_GPIO 12
92+
//#define CS_GPIO 10
93+
94+
// For ESP32_C3
95+
// Optional values to override default settings
96+
// Don't change unless you know what you're doing
97+
//#define ETH_SPI_HOST SPI2_HOST
98+
//#define SPI_CLOCK_MHZ 25
99+
100+
// Must connect INT to GPIOxx or not working
101+
//#define INT_GPIO 10
102+
103+
//#define MISO_GPIO 5
104+
//#define MOSI_GPIO 6
105+
//#define SCK_GPIO 4
106+
//#define CS_GPIO 7
107+
108+
//////////////////////////////////////////////////////////
109+
110+
#include <WebServer_ESP32_SC_W6100.h> // https://github.com/khoih-prog/WebServer_ESP32_SC_W6100
111+
112+
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
113+
#include <AsyncHTTPSRequest_ESP32_Ethernet.h> // https://github.com/khoih-prog/AsyncHTTPSRequest_ESP32_Ethernet
114+
115+
#include <Ticker.h>
116+
117+
AsyncHTTPSRequest request;
118+
Ticker ticker;
119+
Ticker ticker1;
120+
121+
/////////////////////////////////////////////
122+
123+
// Enter a MAC address and IP address for your controller below.
124+
#define NUMBER_OF_MAC 20
125+
126+
byte mac[][NUMBER_OF_MAC] =
127+
{
128+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 },
129+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 },
130+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 },
131+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 },
132+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 },
133+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 },
134+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 },
135+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 },
136+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 },
137+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A },
138+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B },
139+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C },
140+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D },
141+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E },
142+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F },
143+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 },
144+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 },
145+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 },
146+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 },
147+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 },
148+
};
149+
150+
// Select the IP address according to your local network
151+
IPAddress myIP(192, 168, 2, 232);
152+
IPAddress myGW(192, 168, 2, 1);
153+
IPAddress mySN(255, 255, 255, 0);
154+
155+
// Google DNS Server IP
156+
IPAddress myDNS(8, 8, 8, 8);
157+
158+
/////////////////////////////////////////////
159+
160+
void heartBeatPrint()
161+
{
162+
static int num = 1;
163+
164+
if (ESP32_W6100_isConnected())
165+
Serial.print(F("H")); // H means connected
166+
else
167+
Serial.print(F("F")); // F means not connected
168+
169+
if (num == 80)
170+
{
171+
Serial.println();
172+
num = 1;
173+
}
174+
else if (num++ % 10 == 0)
175+
{
176+
Serial.print(F(" "));
177+
}
178+
}
179+
180+
void sendRequest()
181+
{
182+
static bool requestOpenResult;
183+
184+
if (request.readyState() == readyStateUnsent || request.readyState() == readyStateDone)
185+
{
186+
//requestOpenResult = request.open("GET", "https://worldtimeapi.org/api/timezone/Europe/London.txt");
187+
requestOpenResult = request.open("GET", "https://worldtimeapi.org/api/timezone/America/Toronto.txt");
188+
189+
if (requestOpenResult)
190+
{
191+
// Only send() if open() returns true, or crash
192+
request.send();
193+
}
194+
else
195+
{
196+
Serial.println(F("Can't send bad request"));
197+
}
198+
}
199+
else
200+
{
201+
Serial.println(F("Can't send request"));
202+
}
203+
}
204+
205+
void requestCB(void *optParm, AsyncHTTPSRequest *request, int readyState)
206+
{
207+
(void) optParm;
208+
209+
if (readyState == readyStateDone)
210+
{
211+
AHTTPS_LOGDEBUG0(F("\n**************************************\n"));
212+
AHTTPS_LOGDEBUG1(F("Response Code = "), request->responseHTTPString());
213+
214+
if (request->responseHTTPcode() == 200)
215+
{
216+
Serial.println(F("\n**************************************"));
217+
Serial.println(request->responseText());
218+
Serial.println(F("**************************************"));
219+
}
220+
221+
request->setDebug(false);
222+
}
223+
}
224+
225+
void setup()
226+
{
227+
// put your setup code here, to run once:
228+
Serial.begin(115200);
229+
230+
while (!Serial && millis() < 5000);
231+
232+
delay(500);
233+
234+
Serial.print(F("\nStarting AsyncHTTPSRequest_ESP32_SC_W6100 using "));
235+
Serial.print(ARDUINO_BOARD);
236+
Serial.print(F(" with "));
237+
Serial.println(SHIELD_TYPE);
238+
Serial.println(WEBSERVER_ESP32_SC_W6100_VERSION);
239+
Serial.println(ASYNC_TCP_SSL_VERSION);
240+
Serial.println(ASYNC_HTTPS_REQUEST_ESP32_ETHERNET_VERSION);
241+
242+
Serial.setDebugOutput(true);
243+
244+
#if defined(ASYNC_HTTPS_REQUEST_ESP32_ETHERNET_VERSION_MIN)
245+
246+
if (ASYNC_HTTPS_REQUEST_ESP32_ETHERNET_VERSION_INT < ASYNC_HTTPS_REQUEST_ESP32_ETHERNET_VERSION_MIN)
247+
{
248+
Serial.print(F("Warning. Must use this example on Version equal or later than : "));
249+
Serial.println(ASYNC_HTTPS_REQUEST_ESP32_ETHERNET_VERSION_MIN_TARGET);
250+
}
251+
252+
#endif
253+
254+
AHTTPS_LOGWARN(F("Default SPI pinout:"));
255+
AHTTPS_LOGWARN1(F("SPI_HOST:"), ETH_SPI_HOST);
256+
AHTTPS_LOGWARN1(F("MOSI:"), MOSI_GPIO);
257+
AHTTPS_LOGWARN1(F("MISO:"), MISO_GPIO);
258+
AHTTPS_LOGWARN1(F("SCK:"), SCK_GPIO);
259+
AHTTPS_LOGWARN1(F("CS:"), CS_GPIO);
260+
AHTTPS_LOGWARN1(F("INT:"), INT_GPIO);
261+
AHTTPS_LOGWARN1(F("SPI Clock (MHz):"), SPI_CLOCK_MHZ);
262+
AHTTPS_LOGWARN(F("========================="));
263+
264+
///////////////////////////////////
265+
266+
// To be called before ETH.begin()
267+
ESP32_W6100_onEvent();
268+
269+
// start the ethernet connection and the server:
270+
// Use DHCP dynamic IP and random mac
271+
//bool begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPI_CLOCK_MHZ,
272+
// int SPI_HOST, uint8_t *W6100_Mac = W6100_Default_Mac);
273+
ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST );
274+
//ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST, mac[millis() % NUMBER_OF_MAC] );
275+
276+
// Static IP, leave without this line to get IP via DHCP
277+
//bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
278+
//ETH.config(myIP, myGW, mySN, myDNS);
279+
280+
ESP32_W6100_waitForConnect();
281+
282+
///////////////////////////////////
283+
284+
Serial.print(F("\nHTTP WebClient is @ IP : "));
285+
Serial.println(ETH.localIP());
286+
287+
request.setDebug(false);
288+
289+
request.onReadyStateChange(requestCB);
290+
ticker.attach(HTTPS_REQUEST_INTERVAL, sendRequest);
291+
292+
ticker1.attach(HEARTBEAT_INTERVAL, heartBeatPrint);
293+
294+
// Send first request now
295+
sendRequest();
296+
}
297+
298+
void loop()
299+
{
300+
}

0 commit comments

Comments
 (0)