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

Commit

Permalink
v1.4.2 to add demo sending in chunks
Browse files Browse the repository at this point in the history
### 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
khoih-prog committed Nov 11, 2022
1 parent 6dec28d commit c9d6805
Show file tree
Hide file tree
Showing 72 changed files with 5,812 additions and 4,386 deletions.
31 changes: 27 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ If you don't find anything, please [open a new issue](https://github.com/khoih-p
Please ensure to specify the following:

* Arduino IDE version (e.g. 1.8.19) or Platform.io version
* `ArduinoCore-mbed` Core Version (e.g. `ArduinoCore-mbed` mbed_portenta core v3.3.0)
* `ArduinoCore-mbed` Core Version (e.g. `ArduinoCore-mbed` mbed_portenta core v3.4.1)
* `Portenta_H7` Board type (e.g. Portenta_H7 Rev2 ABX00042, etc.)
* Contextual information (e.g. what you were trying to achieve)
* Simplest possible steps to reproduce
Expand All @@ -28,28 +28,51 @@ Please ensure to specify the following:

```
Arduino IDE version: 1.8.19
`ArduinoCore-mbed` mbed_portenta core v3.3.0
`ArduinoCore-mbed` mbed_portenta core v3.4.1
Portenta_H7 Rev2 ABX00042
OS: Ubuntu 20.04 LTS
Linux xy-Inspiron-3593 5.15.0-48-generic #54~20.04.1-Ubuntu SMP Thu Sep 1 16:17:26 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
Linux xy-Inspiron-3593 5.15.0-52-generic #58~20.04.1-Ubuntu SMP Thu Oct 13 13:09:46 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
Context:
I encountered a crash while using this library
Steps to reproduce:
1. ...
2. ...
3. ...
4. ...
```

### Additional context

Add any other context about the problem here.

---

### Sending Feature Requests

Feel free to post feature requests. It's helpful if you can explain exactly why the feature would be useful.

There are usually some outstanding feature requests in the [existing issues list](https://github.com/khoih-prog/Portenta_H7_AsyncWebServer/issues?q=is%3Aopen+is%3Aissue+label%3Aenhancement), feel free to add comments to them.

---

### Sending Pull Requests

Pull Requests with changes and fixes are also welcome!

Please use the `astyle` to reformat the updated library code as follows (demo for Ubuntu Linux)

1. Change directory to the library GitHub

```
xy@xy-Inspiron-3593:~$ cd Arduino/xy/Portenta_H7_AsyncWebServer_GitHub/
xy@xy-Inspiron-3593:~/Arduino/xy/Portenta_H7_AsyncWebServer_GitHub$
```

2. Issue astyle command

```
xy@xy-Inspiron-3593:~/Arduino/xy/Portenta_H7_AsyncWebServer_GitHub$ bash utils/restyle.sh
```


12 changes: 12 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@
[![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](#Contributing)
[![GitHub issues](https://img.shields.io/github/issues/khoih-prog/Portenta_H7_AsyncWebServer.svg)](http://github.com/khoih-prog/Portenta_H7_AsyncWebServer/issues)

<a href="https://www.buymeacoffee.com/khoihprog6" title="Donate to my libraries using BuyMeACoffee"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Donate to my libraries using BuyMeACoffee" style="height: 50px !important;width: 181px !important;" ></a>
<a href="https://www.buymeacoffee.com/khoihprog6" title="Donate to my libraries using BuyMeACoffee"><img src="https://img.shields.io/badge/buy%20me%20a%20coffee-donate-orange.svg?logo=buy-me-a-coffee&logoColor=FFDD00" style="height: 20px !important;width: 200px !important;" ></a>
<a href="https://profile-counter.glitch.me/khoih-prog/count.svg" title="Total khoih-prog Visitor count"><img src="https://profile-counter.glitch.me/khoih-prog/count.svg" style="height: 30px;width: 200px;"></a>
<a href="https://profile-counter.glitch.me/khoih-prog-Portenta_H7_AsyncWebServer/count.svg" title="Visitor count"><img src="https://profile-counter.glitch.me/khoih-prog-Portenta_H7_AsyncWebServer/count.svg" style="height: 30px;width: 200px;"></a>

---
---

## Table of Contents

* [Changelog](#changelog)
* [Releases v1.4.2](#Releases-v142)
* [Releases v1.4.1](#Releases-v141)
* [Releases v1.4.0](#Releases-v140)
* [Releases v1.3.0](#Releases-v130)
Expand All @@ -25,6 +31,12 @@

## Changelog

### 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`

### Releases v1.4.1

1. Don't need `memmove()`, CString no longer destroyed. Check [All memmove() removed - string no longer destroyed #11](https://github.com/khoih-prog/Portenta_H7_AsyncWebServer/pull/11)
Expand Down
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();
}
Loading

0 comments on commit c9d6805

Please sign in to comment.