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

Commit c84d355

Browse files
authored
v1.12.0 for ESP32 and LwIP W5500 Ethernet
### Releases v1.12.0 1. Add support to ESP32 boards using `LwIP W5500 Ethernet` 2. Fix bug. Check [Fix "blank new line in chunk" bug #50](#50)
1 parent 3949066 commit c84d355

File tree

2 files changed

+578
-0
lines changed

2 files changed

+578
-0
lines changed
Lines changed: 308 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,308 @@
1+
/****************************************************************************************************************************
2+
AsyncHTTPMultiRequests_ESP32_W5500.ino - Dead simple AsyncHTTPRequest 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+
AsyncHTTPRequest_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/AsyncHTTPRequest_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+
// Level from 0-4
48+
#define ASYNC_HTTP_DEBUG_PORT Serial
49+
#define _ASYNC_HTTP_LOGLEVEL_ 1
50+
#define _ETHERNET_WEBSERVER_LOGLEVEL_ 1
51+
52+
// 300s = 5 minutes to not flooding
53+
#define HTTP_REQUEST_INTERVAL 60 //300
54+
55+
// 10s
56+
#define HEARTBEAT_INTERVAL 10
57+
58+
//////////////////////////////////////////////////////////
59+
60+
// Optional values to override default settings
61+
// Don't change unless you know what you're doing
62+
//#define ETH_SPI_HOST SPI3_HOST
63+
//#define SPI_CLOCK_MHZ 25
64+
65+
// Must connect INT to GPIOxx or not working
66+
//#define INT_GPIO 4
67+
68+
//#define MISO_GPIO 19
69+
//#define MOSI_GPIO 23
70+
//#define SCK_GPIO 18
71+
//#define CS_GPIO 5
72+
73+
//////////////////////////////////////////////////////////
74+
75+
#include <WebServer_ESP32_W5500.h> // https://github.com/khoih-prog/WebServer_ESP32_W5500
76+
77+
#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET "AsyncHTTPRequest_Generic v1.12.0"
78+
#define ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN 1012000
79+
80+
// Uncomment for certain HTTP site to optimize
81+
//#define NOT_SEND_HEADER_AFTER_CONNECTED true
82+
83+
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
84+
#include <AsyncHTTPRequest_Generic.h> // https://github.com/khoih-prog/AsyncHTTPRequest_Generic
85+
86+
#include <Ticker.h>
87+
88+
AsyncHTTPRequest request;
89+
Ticker ticker;
90+
Ticker ticker1;
91+
92+
/////////////////////////////////////////////
93+
94+
// Enter a MAC address and IP address for your controller below.
95+
#define NUMBER_OF_MAC 20
96+
97+
byte mac[][NUMBER_OF_MAC] =
98+
{
99+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x01 },
100+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x02 },
101+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x03 },
102+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x04 },
103+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x05 },
104+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x06 },
105+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x07 },
106+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x08 },
107+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x09 },
108+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0A },
109+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0B },
110+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0C },
111+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0D },
112+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x0E },
113+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x0F },
114+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x10 },
115+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x11 },
116+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x12 },
117+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x13 },
118+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0xBE, 0x14 },
119+
};
120+
121+
// Select the IP address according to your local network
122+
IPAddress myIP(192, 168, 2, 232);
123+
IPAddress myGW(192, 168, 2, 1);
124+
IPAddress mySN(255, 255, 255, 0);
125+
126+
// Google DNS Server IP
127+
IPAddress myDNS(8, 8, 8, 8);
128+
129+
/////////////////////////////////////////////
130+
131+
void heartBeatPrint(void)
132+
{
133+
static int num = 1;
134+
135+
if (ESP32_W5500_isConnected())
136+
Serial.print(F("H")); // H means connected to WiFi
137+
else
138+
Serial.print(F("F")); // F means not connected to WiFi
139+
140+
if (num == 80)
141+
{
142+
Serial.println();
143+
num = 1;
144+
}
145+
else if (num++ % 10 == 0)
146+
{
147+
Serial.print(F(" "));
148+
}
149+
}
150+
151+
// To replace with your real APP_API
152+
#define APP_API "SECRECT_APP_API"
153+
154+
String requestPart1 = "http://api.openweathermap.org/data/2.5/onecall?lat=-24.32&lon=-46.9983";
155+
String requestAPPID = "&appid=" + String(APP_API);
156+
157+
// exclude fields: current,minutely,hourly,daily,alerts
158+
String requestCurrent = requestPart1 + "&exclude=minutely,hourly,daily,alerts" + requestAPPID;
159+
String requestMinutely = requestPart1 + "&exclude=current,hourly,daily,alerts" + requestAPPID;
160+
String requestHourly = requestPart1 + "&exclude=current,minutely,daily,alerts" + requestAPPID;
161+
String requestDaily = requestPart1 + "&exclude=current,minutely,hourly,alerts" + requestAPPID;
162+
String requestAlert = requestPart1 + "&exclude=current,minutely,hourly,daily" + requestAPPID;
163+
164+
#define NUM_REQUESTS 5
165+
166+
const char* requestName[ NUM_REQUESTS ] = { "Current", "Minutely", "Hourly", "Daily", "Alert" };
167+
168+
const char* requestAll[ NUM_REQUESTS ] = { requestCurrent.c_str(), requestMinutely.c_str(), requestHourly.c_str(), requestDaily.c_str(), requestAlert.c_str() };
169+
170+
uint8_t requestIndex = 0;
171+
172+
void sendRequest()
173+
{
174+
static bool requestOpenResult;
175+
176+
if (request.readyState() == readyStateUnsent || request.readyState() == readyStateDone)
177+
{
178+
requestOpenResult = request.open("GET", requestAll[requestIndex] );
179+
180+
if (requestOpenResult)
181+
{
182+
// Only send() if open() returns true, or crash
183+
request.send();
184+
}
185+
else
186+
{
187+
Serial.println("Can't send bad request");
188+
}
189+
}
190+
else
191+
{
192+
Serial.println("Can't send request");
193+
}
194+
}
195+
196+
void requestCB(void* optParm, AsyncHTTPRequest* request, int readyState)
197+
{
198+
(void) optParm;
199+
200+
if (readyState == readyStateDone)
201+
{
202+
AHTTP_LOGDEBUG(F("\n**************************************"));
203+
AHTTP_LOGDEBUG1(F("Response Code = "), request->responseHTTPString());
204+
205+
if (request->responseHTTPcode() == 200)
206+
{
207+
Serial.print(F("\n***************"));
208+
Serial.print(requestName[ requestIndex ]);
209+
Serial.println(F("***************"));
210+
Serial.println(request->responseText());
211+
Serial.println(F("**************************************"));
212+
}
213+
214+
#if 1
215+
216+
// Bypass hourly
217+
if (requestIndex == 1)
218+
requestIndex = 3;
219+
else
220+
requestIndex = (requestIndex + 1) % NUM_REQUESTS;
221+
222+
#else
223+
// hourly too long, not display anyway. Not enough heap.
224+
requestIndex = (requestIndex + 1) % NUM_REQUESTS;
225+
#endif
226+
227+
request->setDebug(false);
228+
}
229+
}
230+
231+
232+
void setup()
233+
{
234+
// put your setup code here, to run once:
235+
Serial.begin(115200);
236+
237+
while (!Serial && millis() < 5000);
238+
239+
delay(200);
240+
241+
Serial.print("\nStart AsyncHTTPMultiRequests_ESP32_W5500 on ");
242+
Serial.print(ARDUINO_BOARD);
243+
Serial.print(" with ");
244+
Serial.println(SHIELD_TYPE);
245+
Serial.println(WEBSERVER_ESP32_W5500_VERSION);
246+
Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION);
247+
248+
Serial.setDebugOutput(true);
249+
250+
#if defined(ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN)
251+
252+
if (ASYNC_HTTP_REQUEST_GENERIC_VERSION_INT < ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN)
253+
{
254+
Serial.print("Warning. Must use this example on Version equal or later than : ");
255+
Serial.println(ASYNC_HTTP_REQUEST_GENERIC_VERSION_MIN_TARGET);
256+
}
257+
258+
#endif
259+
260+
AHTTP_LOGWARN(F("Default SPI pinout:"));
261+
AHTTP_LOGWARN1(F("SPI_HOST:"), ETH_SPI_HOST);
262+
AHTTP_LOGWARN1(F("MOSI:"), MOSI_GPIO);
263+
AHTTP_LOGWARN1(F("MISO:"), MISO_GPIO);
264+
AHTTP_LOGWARN1(F("SCK:"), SCK_GPIO);
265+
AHTTP_LOGWARN1(F("CS:"), CS_GPIO);
266+
AHTTP_LOGWARN1(F("INT:"), INT_GPIO);
267+
AHTTP_LOGWARN1(F("SPI Clock (MHz):"), SPI_CLOCK_MHZ);
268+
AHTTP_LOGWARN(F("========================="));
269+
270+
///////////////////////////////////
271+
272+
// To be called before ETH.begin()
273+
ESP32_W5500_onEvent();
274+
275+
// start the ethernet connection and the server:
276+
// Use DHCP dynamic IP and random mac
277+
uint16_t index = millis() % NUMBER_OF_MAC;
278+
279+
//bool begin(int MISO_GPIO, int MOSI_GPIO, int SCLK_GPIO, int CS_GPIO, int INT_GPIO, int SPI_CLOCK_MHZ,
280+
// int SPI_HOST, uint8_t *W5500_Mac = W5500_Default_Mac);
281+
//ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST );
282+
ETH.begin( MISO_GPIO, MOSI_GPIO, SCK_GPIO, CS_GPIO, INT_GPIO, SPI_CLOCK_MHZ, ETH_SPI_HOST, mac[index] );
283+
284+
// Static IP, leave without this line to get IP via DHCP
285+
//bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
286+
//ETH.config(myIP, myGW, mySN, myDNS);
287+
288+
ESP32_W5500_waitForConnect();
289+
290+
///////////////////////////////////
291+
292+
Serial.print(F("AsyncHTTPRequest @ IP : "));
293+
Serial.println(ETH.localIP());
294+
295+
request.setDebug(false);
296+
297+
request.onReadyStateChange(requestCB);
298+
ticker.attach(HTTP_REQUEST_INTERVAL, sendRequest);
299+
300+
ticker1.attach(HEARTBEAT_INTERVAL, heartBeatPrint);
301+
302+
// Send first request now
303+
sendRequest();
304+
}
305+
306+
void loop()
307+
{
308+
}

0 commit comments

Comments
 (0)