This repository has been archived by the owner on Feb 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v1.4.2 to add demo sending in chunks
### Releases v1.4.2 1. Add examples [Async_AdvancedWebServer_SendChunked](https://github.com/khoih-prog/Portenta_H7_AsyncWebServer/tree/main/examples/Async_AdvancedWebServer_SendChunked) and [AsyncWebServer_SendChunked](https://github.com/khoih-prog/Portenta_H7_AsyncWebServer/tree/main/examples/AsyncWebServer_SendChunked) to demo how to use `beginChunkedResponse()` to send large `html` in chunks 2. Use `allman astyle` and add `utils` 3. Update `Packages_Patches`
- Loading branch information
1 parent
6dec28d
commit c9d6805
Showing
72 changed files
with
5,812 additions
and
4,386 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
254 changes: 254 additions & 0 deletions
254
examples/Ethernet/AsyncWebServer_SendChunked/AsyncWebServer_SendChunked.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,254 @@ | ||
/**************************************************************************************************************************** | ||
AsyncWebServer_SendChunked.ino | ||
For Portenta_H7 (STM32H7) with Vision-Shield Ethernet or Murata WiFi | ||
Portenta_H7_AsyncWebServer is a library for the Portenta_H7 with Vision-Shield Ethernet or Murata WiFi | ||
Based on and modified from ESPAsyncWebServer (https://github.com/me-no-dev/ESPAsyncWebServer) | ||
Built by Khoi Hoang https://github.com/khoih-prog/Portenta_H7_AsyncWebServer | ||
Licensed under GPLv3 license | ||
*****************************************************************************************************************************/ | ||
|
||
#if !( defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4) ) | ||
#error For Portenta_H7 only | ||
#endif | ||
|
||
#define _PORTENTA_H7_ATCP_LOGLEVEL_ 1 | ||
#define _PORTENTA_H7_AWS_LOGLEVEL_ 4 | ||
|
||
#define USE_ETHERNET_PORTENTA_H7 true | ||
|
||
#include <Portenta_Ethernet.h> | ||
#include <Ethernet.h> | ||
#warning Using Portenta_Ethernet lib for Portenta_H7. | ||
|
||
#include <Portenta_H7_AsyncWebServer.h> | ||
|
||
// In bytes | ||
#define STRING_SIZE 50000 | ||
|
||
// Enter a MAC address and IP address for your controller below. | ||
#define NUMBER_OF_MAC 20 | ||
|
||
byte mac[][NUMBER_OF_MAC] = | ||
{ | ||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x01 }, | ||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x02 }, | ||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x03 }, | ||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x04 }, | ||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x05 }, | ||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x06 }, | ||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x07 }, | ||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x08 }, | ||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x09 }, | ||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0A }, | ||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0B }, | ||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0C }, | ||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0D }, | ||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0E }, | ||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x0F }, | ||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x10 }, | ||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x11 }, | ||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x12 }, | ||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x13 }, | ||
{ 0xDE, 0xAD, 0xBE, 0xEF, 0x32, 0x14 }, | ||
}; | ||
// Select the IP address according to your local network | ||
IPAddress ip(192, 168, 2, 232); | ||
|
||
AsyncWebServer server(80); | ||
|
||
int reqCount = 0; // number of requests received | ||
|
||
#define BUFFER_SIZE 512 | ||
char temp[BUFFER_SIZE]; | ||
|
||
void createPage(String &pageInput) | ||
{ | ||
int sec = millis() / 1000; | ||
int min = sec / 60; | ||
int hr = min / 60; | ||
int day = hr / 24; | ||
|
||
snprintf(temp, BUFFER_SIZE - 1, | ||
"<html>\ | ||
<head>\ | ||
<meta http-equiv='refresh' content='5'/>\ | ||
<title>AsyncWebServer-%s</title>\ | ||
<style>\ | ||
body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\ | ||
</style>\ | ||
</head>\ | ||
<body>\ | ||
<h2>AsyncWebServer_SendChunked_Portenta_H7!</h2>\ | ||
<h3>running on %s</h3>\ | ||
<p>Uptime: %d d %02d:%02d:%02d</p>\ | ||
</body>\ | ||
</html>", BOARD_NAME, BOARD_NAME, day, hr % 24, min % 60, sec % 60); | ||
|
||
pageInput = temp; | ||
} | ||
|
||
void handleNotFound(AsyncWebServerRequest *request) | ||
{ | ||
String message = "File Not Found\n\n"; | ||
|
||
message += "URI: "; | ||
message += request->url(); | ||
message += "\nMethod: "; | ||
message += (request->method() == HTTP_GET) ? "GET" : "POST"; | ||
message += "\nArguments: "; | ||
message += request->args(); | ||
message += "\n"; | ||
|
||
for (uint8_t i = 0; i < request->args(); i++) | ||
{ | ||
message += " " + request->argName(i) + ": " + request->arg(i) + "\n"; | ||
} | ||
|
||
request->send(404, "text/plain", message); | ||
} | ||
|
||
String out; | ||
|
||
void handleRoot(AsyncWebServerRequest *request) | ||
{ | ||
char temp[70]; | ||
|
||
// clear the String to start over | ||
out = String(); | ||
|
||
createPage(out); | ||
|
||
out += "<html><body>\r\n<table><tr><th>INDEX</th><th>DATA</th></tr>"; | ||
|
||
for (uint16_t lineIndex = 0; lineIndex < 500; lineIndex++) | ||
{ | ||
out += "<tr><td>"; | ||
out += String(lineIndex); | ||
out += "</td><td>"; | ||
out += "Portenta_H7_AsyncWebServer_SendChunked_ABCDEFGHIJKLMNOPQRSTUVWXYZ</td></tr>"; | ||
} | ||
|
||
out += "</table></body></html>\r\n"; | ||
|
||
AWS_LOGDEBUG1("Total length to send in chunks =", out.length()); | ||
|
||
AsyncWebServerResponse *response = request->beginChunkedResponse("text/html", [](uint8_t *buffer, size_t maxLen, size_t filledLength) -> size_t | ||
{ | ||
size_t len = min(maxLen, out.length() - filledLength); | ||
memcpy(buffer, out.c_str() + filledLength, len); | ||
|
||
AWS_LOGDEBUG1("Bytes sent in chunk =", len); | ||
|
||
return len; | ||
}); | ||
|
||
request->send(response); | ||
} | ||
|
||
void setup() | ||
{ | ||
out.reserve(STRING_SIZE); | ||
|
||
Serial.begin(115200); | ||
|
||
while (!Serial && millis() < 5000); | ||
|
||
delay(200); | ||
|
||
Serial.print("\nStart AsyncWebServer_SendChunked on "); | ||
Serial.print(BOARD_NAME); | ||
Serial.print(" with "); | ||
Serial.println(SHIELD_TYPE); | ||
Serial.println(PORTENTA_H7_ASYNC_TCP_VERSION); | ||
Serial.println(PORTENTA_H7_ASYNC_WEBSERVER_VERSION); | ||
|
||
/////////////////////////////////// | ||
|
||
// start the ethernet connection and the server | ||
// Use random mac | ||
uint16_t index = millis() % NUMBER_OF_MAC; | ||
|
||
// Use Static IP | ||
//Ethernet.begin(mac[index], ip); | ||
// Use DHCP dynamic IP and random mac | ||
Ethernet.begin(mac[index]); | ||
|
||
if (Ethernet.hardwareStatus() == EthernetNoHardware) | ||
{ | ||
Serial.println("No Ethernet found. Stay here forever"); | ||
|
||
while (true) | ||
{ | ||
delay(1); // do nothing, no point running without Ethernet hardware | ||
} | ||
} | ||
|
||
if (Ethernet.linkStatus() == LinkOFF) | ||
{ | ||
Serial.println("Not connected Ethernet cable"); | ||
} | ||
|
||
Serial.print(F("Using mac index = ")); | ||
Serial.println(index); | ||
|
||
Serial.print(F("Connected! IP address: ")); | ||
Serial.println(Ethernet.localIP()); | ||
|
||
/////////////////////////////////// | ||
|
||
server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) | ||
{ | ||
handleRoot(request); | ||
}); | ||
|
||
server.on("/inline", [](AsyncWebServerRequest * request) | ||
{ | ||
request->send(200, "text/plain", "This works as well"); | ||
}); | ||
|
||
server.onNotFound(handleNotFound); | ||
|
||
server.begin(); | ||
|
||
Serial.print(F("AsyncWebServer is @ IP : ")); | ||
Serial.println(Ethernet.localIP()); | ||
} | ||
|
||
void heartBeatPrint() | ||
{ | ||
static int num = 1; | ||
|
||
Serial.print(F(".")); | ||
|
||
if (num == 80) | ||
{ | ||
Serial.println(); | ||
num = 1; | ||
} | ||
else if (num++ % 10 == 0) | ||
{ | ||
Serial.print(F(" ")); | ||
} | ||
} | ||
|
||
void check_status() | ||
{ | ||
static unsigned long checkstatus_timeout = 0; | ||
|
||
#define STATUS_CHECK_INTERVAL 10000L | ||
|
||
// Send status report every STATUS_REPORT_INTERVAL (60) seconds: we don't need to send updates frequently if there is no status change. | ||
if ((millis() > checkstatus_timeout) || (checkstatus_timeout == 0)) | ||
{ | ||
heartBeatPrint(); | ||
checkstatus_timeout = millis() + STATUS_CHECK_INTERVAL; | ||
} | ||
} | ||
|
||
void loop() | ||
{ | ||
check_status(); | ||
} |
Oops, something went wrong.