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

Commit 9647523

Browse files
authored
v1.11.0 for ESP32 and LwIP ENC28J60 Ethernet
### Releases v1.11.0 1. Add support to ESP32 boards using LwIP ENC28J60 Ethernet 2. Use `allman astyle` and add `utils`. Restyle the library
1 parent 4b56b00 commit 9647523

File tree

3 files changed

+577
-3
lines changed

3 files changed

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

0 commit comments

Comments
 (0)