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

Commit 6b2f9fc

Browse files
authored
v1.6.1 to add demo sending in chunks
#### Release v1.6.1 1. Add examples [Async_AdvancedWebServer_SendChunked](https://github.com/khoih-prog/AsyncWebServer_STM32/tree/main/examples/Async_AdvancedWebServer_SendChunked) and [AsyncWebServer_SendChunked](https://github.com/khoih-prog/AsyncWebServer_STM32/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 6333a81 commit 6b2f9fc

File tree

4 files changed

+714
-0
lines changed

4 files changed

+714
-0
lines changed
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
/****************************************************************************************************************************
2+
AsyncWebServer_SendChunked.ino
3+
4+
For STM32 with LAN8720 (STM32F4/F7) or built-in LAN8742A Ethernet (Nucleo-144, DISCOVERY, etc)
5+
6+
AsyncWebServer_STM32 is a library for the STM32 with LAN8720 or built-in LAN8742A Ethernet WebServer
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_STM32
10+
Licensed under GPLv3 license
11+
*****************************************************************************************************************************/
12+
/*
13+
Currently support
14+
1) STM32 boards with built-in Ethernet (to use USE_BUILTIN_ETHERNET = true) such as :
15+
- Nucleo-144 (F429ZI, F767ZI)
16+
- Discovery (STM32F746G-DISCOVERY)
17+
- STM32 boards (STM32F/L/H/G/WB/MP1) with 32K+ Flash, with Built-in Ethernet,
18+
- See How To Use Built-in Ethernet at (https://github.com/khoih-prog/EthernetWebServer_STM32/issues/1)
19+
2) STM32F/L/H/G/WB/MP1 boards (with 64+K Flash) running ENC28J60 shields (to use USE_BUILTIN_ETHERNET = false)
20+
3) STM32F/L/H/G/WB/MP1 boards (with 64+K Flash) running W5x00 shields
21+
4) STM32F4 and STM32F7 boards (with 64+K Flash) running LAN8720 shields
22+
*/
23+
24+
#include "defines.h"
25+
26+
// In bytes
27+
#define STRING_SIZE 50000
28+
29+
AsyncWebServer server(80);
30+
31+
int reqCount = 0; // number of requests received
32+
33+
const int led = 13;
34+
35+
#define BUFFER_SIZE 512
36+
char temp[BUFFER_SIZE];
37+
38+
void createPage(String &pageInput)
39+
{
40+
int sec = millis() / 1000;
41+
int min = sec / 60;
42+
int hr = min / 60;
43+
int day = hr / 24;
44+
45+
snprintf(temp, BUFFER_SIZE - 1,
46+
"<html>\
47+
<head>\
48+
<meta http-equiv='refresh' content='5'/>\
49+
<title>AsyncWebServer-%s</title>\
50+
<style>\
51+
body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
52+
</style>\
53+
</head>\
54+
<body>\
55+
<h2>AsyncWebServer_SendChunked!</h2>\
56+
<h3>running on %s</h3>\
57+
<p>Uptime: %d d %02d:%02d:%02d</p>\
58+
</body>\
59+
</html>", BOARD_NAME, BOARD_NAME, day, hr % 24, min % 60, sec % 60);
60+
61+
pageInput = temp;
62+
}
63+
64+
void handleNotFound(AsyncWebServerRequest *request)
65+
{
66+
String message = "File Not Found\n\n";
67+
68+
message += "URI: ";
69+
message += request->url();
70+
message += "\nMethod: ";
71+
message += (request->method() == HTTP_GET) ? "GET" : "POST";
72+
message += "\nArguments: ";
73+
message += request->args();
74+
message += "\n";
75+
76+
for (uint8_t i = 0; i < request->args(); i++)
77+
{
78+
message += " " + request->argName(i) + ": " + request->arg(i) + "\n";
79+
}
80+
81+
request->send(404, "text/plain", message);
82+
}
83+
84+
String out;
85+
86+
void handleRoot(AsyncWebServerRequest *request)
87+
{
88+
// clear the String to start over
89+
out = String();
90+
91+
createPage(out);
92+
93+
out += "<html><body>\r\n<table><tr><th>INDEX</th><th>DATA</th></tr>";
94+
95+
for (uint16_t lineIndex = 0; lineIndex < 500; lineIndex++)
96+
{
97+
out += "<tr><td>";
98+
out += String(lineIndex);
99+
out += "</td><td>";
100+
out += "AsyncWebServer_SendChunked_ABCDEFGHIJKLMNOPQRSTUVWXYZ</td></tr>";
101+
}
102+
103+
out += "</table></body></html>\r\n";
104+
105+
LOGDEBUG1("Total length to send in chunks =", out.length());
106+
107+
AsyncWebServerResponse *response = request->beginChunkedResponse("text/html", [](uint8_t *buffer, size_t maxLen, size_t filledLength) -> size_t
108+
{
109+
size_t len = min(maxLen, out.length() - filledLength);
110+
memcpy(buffer, out.c_str() + filledLength, len);
111+
112+
LOGDEBUG1("Bytes sent in chunk =", len);
113+
114+
return len;
115+
});
116+
117+
request->send(response);
118+
}
119+
120+
121+
void setup()
122+
{
123+
out.reserve(STRING_SIZE);
124+
125+
pinMode(led, OUTPUT);
126+
digitalWrite(led, 0);
127+
128+
Serial.begin(115200);
129+
130+
while (!Serial && millis() < 5000);
131+
132+
delay(200);
133+
134+
Serial.print("\nStart AsyncWebServer_SendChunked on ");
135+
Serial.print(BOARD_NAME);
136+
Serial.print(" with ");
137+
Serial.println(SHIELD_TYPE);
138+
Serial.println(ASYNC_WEBSERVER_STM32_VERSION);
139+
140+
#if (_ASYNCWEBSERVER_STM32_LOGLEVEL_ > 2)
141+
Serial.print("STM32 Core version v");
142+
Serial.print(STM32_CORE_VERSION_MAJOR);
143+
Serial.print(".");
144+
Serial.print(STM32_CORE_VERSION_MINOR);
145+
Serial.print(".");
146+
Serial.println(STM32_CORE_VERSION_PATCH);
147+
#endif
148+
149+
// start the ethernet connection and the server
150+
// Use random mac
151+
uint16_t index = millis() % NUMBER_OF_MAC;
152+
153+
// Use Static IP
154+
//Ethernet.begin(mac[index], ip);
155+
// Use DHCP dynamic IP and random mac
156+
Ethernet.begin(mac[index]);
157+
158+
///////////////////////////////////
159+
160+
server.on("/", HTTP_GET, [](AsyncWebServerRequest * request)
161+
{
162+
handleRoot(request);
163+
});
164+
165+
server.on("/inline", [](AsyncWebServerRequest * request)
166+
{
167+
request->send(200, "text/plain", "This works as well");
168+
});
169+
170+
server.onNotFound(handleNotFound);
171+
172+
server.begin();
173+
174+
Serial.print(F("AsyncWebServer is @ IP : "));
175+
Serial.println(Ethernet.localIP());
176+
}
177+
178+
void heartBeatPrint()
179+
{
180+
static int num = 1;
181+
182+
Serial.print(F("."));
183+
184+
if (num == 80)
185+
{
186+
Serial.println();
187+
num = 1;
188+
}
189+
else if (num++ % 10 == 0)
190+
{
191+
Serial.print(F(" "));
192+
}
193+
}
194+
195+
void check_status()
196+
{
197+
static unsigned long checkstatus_timeout = 0;
198+
199+
#define STATUS_CHECK_INTERVAL 10000L
200+
201+
// Send status report every STATUS_REPORT_INTERVAL (60) seconds: we don't need to send updates frequently if there is no status change.
202+
if ((millis() > checkstatus_timeout) || (checkstatus_timeout == 0))
203+
{
204+
heartBeatPrint();
205+
checkstatus_timeout = millis() + STATUS_CHECK_INTERVAL;
206+
}
207+
}
208+
209+
void loop()
210+
{
211+
check_status();
212+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/****************************************************************************************************************************
2+
defines.h
3+
4+
For STM32 with LAN8720 (STM32F4/F7) or built-in LAN8742A Ethernet (Nucleo-144, DISCOVERY, etc)
5+
6+
AsyncWebServer_STM32 is a library for the STM32 with LAN8720 or built-in LAN8742A Ethernet WebServer
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_STM32
10+
Licensed under GPLv3 license
11+
***************************************************************************************************************************************/
12+
13+
#ifndef defines_h
14+
#define defines_h
15+
16+
/*
17+
Currently support
18+
1) STM32 boards with built-in Ethernet (to use USE_BUILTIN_ETHERNET = true) such as :
19+
- Nucleo-144 (F429ZI, F767ZI)
20+
- Discovery (STM32F746G-DISCOVERY)
21+
- STM32 boards (STM32F/L/H/G/WB/MP1) with 32K+ Flash, with Built-in Ethernet,
22+
- See How To Use Built-in Ethernet at (https://github.com/khoih-prog/EthernetWebServer_STM32/issues/1)
23+
2) STM32F/L/H/G/WB/MP1 boards (with 64+K Flash) running ENC28J60 shields (to use USE_BUILTIN_ETHERNET = false)
24+
3) STM32F/L/H/G/WB/MP1 boards (with 64+K Flash) running W5x00 shields
25+
4) STM32F4 and STM32F7 boards (with 64+K Flash) running LAN8720 shields
26+
*/
27+
28+
#if !( defined(STM32F0) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) ||defined(STM32F4) || defined(STM32F7) || \
29+
defined(STM32L0) || defined(STM32L1) || defined(STM32L4) || defined(STM32H7) ||defined(STM32G0) || defined(STM32G4) || \
30+
defined(STM32WB) || defined(STM32MP1) )
31+
#error This code is designed to run on STM32F/L/H/G/WB/MP1 platform! Please check your Tools->Board setting.
32+
#endif
33+
34+
#define _ASYNCWEBSERVER_STM32_LOGLEVEL_ 4
35+
36+
#if defined(STM32F0)
37+
#warning STM32F0 board selected
38+
#define BOARD_TYPE "STM32F0"
39+
#elif defined(STM32F1)
40+
#warning STM32F1 board selected
41+
#define BOARD_TYPE "STM32F1"
42+
#elif defined(STM32F2)
43+
#warning STM32F2 board selected
44+
#define BOARD_TYPE "STM32F2"
45+
#elif defined(STM32F3)
46+
#warning STM32F3 board selected
47+
#define BOARD_TYPE "STM32F3"
48+
#elif defined(STM32F4)
49+
#warning STM32F4 board selected
50+
#define BOARD_TYPE "STM32F4"
51+
#elif defined(STM32F7)
52+
#warning STM32F7 board selected
53+
#define BOARD_TYPE "STM32F7"
54+
#elif defined(STM32L0)
55+
#warning STM32L0 board selected
56+
#define BOARD_TYPE "STM32L0"
57+
#elif defined(STM32L1)
58+
#warning STM32L1 board selected
59+
#define BOARD_TYPE "STM32L1"
60+
#elif defined(STM32L4)
61+
#warning STM32L4 board selected
62+
#define BOARD_TYPE "STM32L4"
63+
#elif defined(STM32H7)
64+
#warning STM32H7 board selected
65+
#define BOARD_TYPE "STM32H7"
66+
#elif defined(STM32G0)
67+
#warning STM32G0 board selected
68+
#define BOARD_TYPE "STM32G0"
69+
#elif defined(STM32G4)
70+
#warning STM32G4 board selected
71+
#define BOARD_TYPE "STM32G4"
72+
#elif defined(STM32WB)
73+
#warning STM32WB board selected
74+
#define BOARD_TYPE "STM32WB"
75+
#elif defined(STM32MP1)
76+
#warning STM32MP1 board selected
77+
#define BOARD_TYPE "STM32MP1"
78+
#else
79+
#warning STM32 unknown board selected
80+
#define BOARD_TYPE "STM32 Unknown"
81+
#endif
82+
83+
#ifndef BOARD_NAME
84+
#define BOARD_NAME BOARD_TYPE
85+
#endif
86+
87+
#define SHIELD_TYPE "LAN8742A built-in Ethernet"
88+
89+
#include <LwIP.h>
90+
#include <STM32Ethernet.h>
91+
#include <AsyncWebServer_STM32.h>
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, 0x32, 0x01 },
99+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x02 },
100+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x03 },
101+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x04 },
102+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x05 },
103+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x06 },
104+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x07 },
105+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x08 },
106+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x09 },
107+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0A },
108+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0B },
109+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0C },
110+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0D },
111+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0E },
112+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0F },
113+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x10 },
114+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x11 },
115+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x12 },
116+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x13 },
117+
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x14 },
118+
};
119+
// Select the IP address according to your local network
120+
IPAddress ip(192, 168, 2, 232);
121+
122+
#endif //defines_h

0 commit comments

Comments
 (0)