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

Commit 0f7954e

Browse files
authored
v2.3.0 for ESP32 and LwIP ENC28J60 Ethernet
### Releases v2.3.0 1. Add support to ESP32 boards using `LwIP ENC28J60 Ethernet`
1 parent 3506fc7 commit 0f7954e

File tree

2 files changed

+288
-2
lines changed

2 files changed

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

examples/multiFileProject/multiFileProject.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
#error This code is intended to run on the ESP32 platform! Please check your Tools->Board setting.
1717
#endif
1818

19-
#define ASYNC_HTTPS_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPSRequest_Generic v2.2.1"
20-
#define ASYNC_HTTPS_REQUEST_GENERIC_VERSION_MIN 2002001
19+
#define ASYNC_HTTPS_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPSRequest_Generic v2.3.0"
20+
#define ASYNC_HTTPS_REQUEST_GENERIC_VERSION_MIN 2003000
2121

2222
/////////////////////////////////////////////////////////
2323

0 commit comments

Comments
 (0)