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

Commit f991b5e

Browse files
authored
v1.5.1 to add demo sending in chunks
#### Releases v1.5.1 1. Add examples [Async_AdvancedWebServer_SendChunked](https://github.com/khoih-prog/AsyncWebServer_Ethernet/tree/main/examples/Async_AdvancedWebServer_SendChunked) and [AsyncWebServer_SendChunked](https://github.com/khoih-prog/AsyncWebServer_Ethernet/tree/main/examples/AsyncWebServer_SendChunked) to demo how to use `beginChunkedResponse()` to send large `html` in chunks 2. Use `allman astyle` and add `utils`
1 parent b9bb5df commit f991b5e

File tree

4 files changed

+648
-0
lines changed

4 files changed

+648
-0
lines changed
Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
/****************************************************************************************************************************
2+
AsyncWebServer_SendChunked.ino
3+
4+
For ESP8266 using W5x00/ENC8266 Ethernet
5+
6+
AsyncWebServer_Ethernet is a library for the Ethernet with lwIP_5100, lwIP_5500 or lwIP_enc28j60 library
7+
8+
Based on and modified from ESPAsyncWebServer (https://github.com/me-no-dev/ESPAsyncWebServer)
9+
Built by Khoi Hoang https://github.com/khoih-prog/AsyncWebServer_Ethernet
10+
Licensed under GPLv3 license
11+
*****************************************************************************************************************************/
12+
13+
#include "defines.h"
14+
15+
#include <AsyncWebServer_Ethernet.h>
16+
17+
// In bytes
18+
#define STRING_SIZE 50000
19+
20+
AsyncWebServer server(80);
21+
22+
int reqCount = 0; // number of requests received
23+
24+
#define BUFFER_SIZE 512
25+
char temp[BUFFER_SIZE];
26+
27+
void createPage(String &pageInput)
28+
{
29+
int sec = millis() / 1000;
30+
int min = sec / 60;
31+
int hr = min / 60;
32+
int day = hr / 24;
33+
34+
snprintf(temp, BUFFER_SIZE - 1,
35+
"<html>\
36+
<head>\
37+
<meta http-equiv='refresh' content='5'/>\
38+
<title>AsyncWebServer-%s</title>\
39+
<style>\
40+
body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
41+
</style>\
42+
</head>\
43+
<body>\
44+
<h2>AsyncWebServer_SendChunked_WT32_ETH01!</h2>\
45+
<h3>running on %s</h3>\
46+
<p>Uptime: %d d %02d:%02d:%02d</p>\
47+
</body>\
48+
</html>", BOARD_NAME, BOARD_NAME, day, hr % 24, min % 60, sec % 60);
49+
50+
pageInput = temp;
51+
}
52+
53+
void handleNotFound(AsyncWebServerRequest *request)
54+
{
55+
String message = "File Not Found\n\n";
56+
57+
message += "URI: ";
58+
message += request->url();
59+
message += "\nMethod: ";
60+
message += (request->method() == HTTP_GET) ? "GET" : "POST";
61+
message += "\nArguments: ";
62+
message += request->args();
63+
message += "\n";
64+
65+
for (uint8_t i = 0; i < request->args(); i++)
66+
{
67+
message += " " + request->argName(i) + ": " + request->arg(i) + "\n";
68+
}
69+
70+
request->send(404, "text/plain", message);
71+
}
72+
73+
String out;
74+
75+
void handleRoot(AsyncWebServerRequest *request)
76+
{
77+
// clear the String to start over
78+
out = String();
79+
80+
createPage(out);
81+
82+
out += "<html><body>\r\n<table><tr><th>INDEX</th><th>DATA</th></tr>";
83+
84+
for (uint16_t lineIndex = 0; lineIndex < 500; lineIndex++)
85+
{
86+
out += "<tr><td>";
87+
out += String(lineIndex);
88+
out += "</td><td>";
89+
out += "AsyncWebServer_SendChunked_ABCDEFGHIJKLMNOPQRSTUVWXYZ</td></tr>";
90+
}
91+
92+
out += "</table></body></html>\r\n";
93+
94+
LOGDEBUG1("Total length to send in chunks =", out.length());
95+
96+
AsyncWebServerResponse *response = request->beginChunkedResponse("text/html", [](uint8_t *buffer, size_t maxLen, size_t filledLength) -> size_t
97+
{
98+
size_t len = min(maxLen, out.length() - filledLength);
99+
memcpy(buffer, out.c_str() + filledLength, len);
100+
101+
LOGDEBUG1("Bytes sent in chunk =", len);
102+
103+
return len;
104+
});
105+
106+
request->send(response);
107+
}
108+
109+
void initEthernet()
110+
{
111+
SPI.begin();
112+
SPI.setClockDivider(SPI_CLOCK_DIV4);
113+
SPI.setBitOrder(MSBFIRST);
114+
SPI.setDataMode(SPI_MODE0);
115+
116+
#if !USING_DHCP
117+
eth.config(localIP, gateway, netMask, gateway);
118+
#endif
119+
120+
eth.setDefault();
121+
122+
if (!eth.begin())
123+
{
124+
Serial.println("No Ethernet hardware ... Stop here");
125+
126+
while (true)
127+
{
128+
delay(1000);
129+
}
130+
}
131+
else
132+
{
133+
Serial.print("Connecting to network : ");
134+
135+
while (!eth.connected())
136+
{
137+
Serial.print(".");
138+
delay(1000);
139+
}
140+
}
141+
142+
Serial.println();
143+
144+
#if USING_DHCP
145+
Serial.print("Ethernet DHCP IP address: ");
146+
#else
147+
Serial.print("Ethernet Static IP address: ");
148+
#endif
149+
150+
Serial.println(eth.localIP());
151+
}
152+
153+
void setup()
154+
{
155+
out.reserve(STRING_SIZE);
156+
157+
pinMode(LED_BUILTIN, OUTPUT);
158+
digitalWrite(LED_BUILTIN, LED_OFF);
159+
160+
Serial.begin(115200);
161+
162+
while (!Serial && millis() < 5000);
163+
164+
delay(200);
165+
166+
Serial.print("\nStart AsyncWebServer_SendChunked on ");
167+
Serial.print(BOARD_NAME);
168+
Serial.print(" with ");
169+
Serial.println(SHIELD_TYPE);
170+
Serial.println(ASYNC_WEBSERVER_ETHERNET_VERSION);
171+
172+
initEthernet();
173+
174+
///////////////////////////////////
175+
176+
server.on("/", HTTP_GET, [](AsyncWebServerRequest * request)
177+
{
178+
handleRoot(request);
179+
});
180+
181+
server.on("/inline", [](AsyncWebServerRequest * request)
182+
{
183+
request->send(200, "text/plain", "This works as well");
184+
});
185+
186+
server.onNotFound(handleNotFound);
187+
188+
server.begin();
189+
190+
Serial.print(F("AsyncWebServer is @ IP : "));
191+
Serial.println(eth.localIP());
192+
}
193+
194+
void heartBeatPrint()
195+
{
196+
static int num = 1;
197+
198+
Serial.print(F("."));
199+
200+
if (num == 80)
201+
{
202+
Serial.println();
203+
num = 1;
204+
}
205+
else if (num++ % 10 == 0)
206+
{
207+
Serial.print(F(" "));
208+
}
209+
}
210+
211+
void check_status()
212+
{
213+
static unsigned long checkstatus_timeout = 0;
214+
215+
#define STATUS_CHECK_INTERVAL 10000L
216+
217+
// Send status report every STATUS_REPORT_INTERVAL (60) seconds: we don't need to send updates frequently if there is no status change.
218+
if ((millis() > checkstatus_timeout) || (checkstatus_timeout == 0))
219+
{
220+
heartBeatPrint();
221+
checkstatus_timeout = millis() + STATUS_CHECK_INTERVAL;
222+
}
223+
}
224+
225+
void loop()
226+
{
227+
check_status();
228+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/****************************************************************************************************************************
2+
defines.h
3+
4+
For ESP8266 using W5x00/ENC8266 Ethernet
5+
6+
AsyncWebServer_Ethernet is a library for the Ethernet with lwIP_5100, lwIP_5500 or lwIP_enc28j60 library
7+
8+
Based on and modified from ESPAsyncWebServer (https://github.com/me-no-dev/ESPAsyncWebServer)
9+
Built by Khoi Hoang https://github.com/khoih-prog/AsyncWebServer_Ethernet
10+
Licensed under GPLv3 license
11+
***************************************************************************************************************************************/
12+
13+
#ifndef defines_h
14+
#define defines_h
15+
16+
#if defined(ESP8266)
17+
#define LED_ON LOW
18+
#define LED_OFF HIGH
19+
#else
20+
#error Only ESP8266
21+
#endif
22+
23+
#define _AWS_ETHERNET_LOGLEVEL_ 4
24+
25+
//////////////////////////////////////////////////////////
26+
27+
#define USING_W5500 true
28+
#define USING_W5100 false
29+
#define USING_ENC28J60 false
30+
31+
#include <SPI.h>
32+
33+
// Using GPIO4, GPIO16, or GPIO5
34+
#define CSPIN 4 //16 // 5
35+
36+
#if USING_W5500
37+
#include "W5500lwIP.h"
38+
#define SHIELD_TYPE "ESP8266_W5500 Ethernet"
39+
40+
Wiznet5500lwIP eth(CSPIN);
41+
42+
#elif USING_W5100
43+
#include <W5100lwIP.h>
44+
#define SHIELD_TYPE "ESP8266_W5100 Ethernet"
45+
46+
Wiznet5100lwIP eth(CSPIN);
47+
48+
#elif USING_ENC28J60
49+
#include <ENC28J60lwIP.h>
50+
#define SHIELD_TYPE "ESP8266_ENC28J60 Ethernet"
51+
52+
ENC28J60lwIP eth(CSPIN);
53+
#else
54+
// default if none selected
55+
#include "W5500lwIP.h"
56+
57+
Wiznet5500lwIP eth(CSPIN);
58+
#endif
59+
60+
#include <WiFiClient.h> // WiFiClient (-> TCPClient)
61+
62+
using TCPClient = WiFiClient;
63+
64+
//////////////////////////////////////////////////////////
65+
66+
#define USING_DHCP true
67+
68+
#if !USING_DHCP
69+
IPAddress localIP(192, 168, 2, 222);
70+
IPAddress gateway(192, 168, 2, 1);
71+
IPAddress netMask(255, 255, 255, 0);
72+
#endif
73+
74+
#endif //defines_h

0 commit comments

Comments
 (0)