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

Commit

Permalink
v1.5.0 to save heap when sending large data
Browse files Browse the repository at this point in the history
#### Releases v1.5.0

1. Support using `CString` to save heap to send `very large data`. Check [request->send(200, textPlainStr, jsonChartDataCharStr); - Without using String Class - to save heap #8](khoih-prog/Portenta_H7_AsyncWebServer#8) and [All memmove() removed - string no longer destroyed #11](khoih-prog/Portenta_H7_AsyncWebServer#11)
2. Add multiple examples to demo the new feature
  • Loading branch information
khoih-prog committed Oct 6, 2022
1 parent b867b25 commit 9f7e380
Show file tree
Hide file tree
Showing 36 changed files with 2,496 additions and 411 deletions.
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ Arduino IDE version: 1.8.19
ESP8266_NODEMCU_ESP12E using ESP8266_W5500 Ethernet
ESP8266 core v3.0.2
OS: Ubuntu 20.04 LTS
Linux xy-Inspiron-3593 5.13.0-39-generic #44~20.04.1-Ubuntu SMP Thu Mar 24 16:43:35 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
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
Context:
I encountered a crash while trying to use the Timer Interrupt.
I encountered a crash while using this library
Steps to reproduce:
1. ...
Expand Down
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@
## Table of contents

* [Changelog](#changelog)
* [Releases v1.5.0](#releases-v150)
* [Releases v1.4.1](#releases-v141)

---
---

## Changelog

#### Releases v1.5.0

1. Support using `CString` to save heap to send `very large data`. Check [request->send(200, textPlainStr, jsonChartDataCharStr); - Without using String Class - to save heap #8](https://github.com/khoih-prog/Portenta_H7_AsyncWebServer/pull/8) and [All memmove() removed - string no longer destroyed #11](https://github.com/khoih-prog/Portenta_H7_AsyncWebServer/pull/11)
2. Add multiple examples to demo the new feature

#### Releases v1.4.1

1. Initial coding to port [ESPAsyncWebServer](https://github.com/me-no-dev/ESPAsyncWebServer) to ESP8266 boards using W5x00 / ENC28J60 Ethernet.
Expand Down
3 changes: 2 additions & 1 deletion examples/Async_AdvancedWebServer/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@

#include <SPI.h>

#define CSPIN 16 // 5
// Using GPIO4, GPIO16, or GPIO5
#define CSPIN 4 //16 // 5

#if USING_W5500
#include "W5500lwIP.h"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,303 @@
/****************************************************************************************************************************
Async_AdvancedWebServer_MemoryIssues_SendArduinoString.ino
For ESP8266 using W5x00/ENC8266 Ethernet
AsyncWebServer_Ethernet is a library for the Ethernet with lwIP_5100, lwIP_5500 or lwIP_enc28j60 library
Based on and modified from ESPAsyncWebServer (https://github.com/me-no-dev/ESPAsyncWebServer)
Built by Khoi Hoang https://github.com/khoih-prog/AsyncWebServer_Ethernet
Licensed under GPLv3 license
Copyright (c) 2015, Majenko Technologies
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
Neither the name of Majenko Technologies nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************************************************************/

#include "defines.h"

#include <AsyncWebServer_Ethernet.h>

// In bytes
//#define STRING_SIZE 40000
#define STRING_SIZE 12000

AsyncWebServer server(80);

int reqCount = 0; // number of requests received

#define BUFFER_SIZE 512
char temp[BUFFER_SIZE];

void handleRoot(AsyncWebServerRequest *request)
{
digitalWrite(LED_BUILTIN, LED_ON);

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_Ethernet!</h2>\
<h3>running on %s</h3>\
<p>Uptime: %d d %02d:%02d:%02d</p>\
<img src=\"/test.svg\" />\
</body>\
</html>", BOARD_NAME, SHIELD_TYPE, day, hr % 24, min % 60, sec % 60);

request->send(200, "text/html", temp);

digitalWrite(LED_BUILTIN, LED_OFF);
}

void handleNotFound(AsyncWebServerRequest *request)
{
digitalWrite(LED_BUILTIN, LED_ON);
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);
digitalWrite(LED_BUILTIN, LED_OFF);
}

void PrintHeapData(String hIn)
{
// cores/esp8266/Esp.cpp
static uint32_t maxFreeHeap = 0xFFFFFFFF;

// Get once at the beginning for comparison only
static uint32_t totalHeap = ESP.getFreeHeap();

uint32_t freeHeap = ESP.getFreeHeap();

// Print and update only when larger heap
if (maxFreeHeap > freeHeap)
{
maxFreeHeap = freeHeap;

Serial.print("\nHEAP DATA - ");
Serial.print(hIn);

Serial.print(" Free heap: ");
Serial.print(freeHeap);
Serial.print(" Used heap: ");
Serial.println(totalHeap - freeHeap);
}
}

void PrintStringSize(String & out)
{
static uint32_t count = 0;

// Print only when cStr length too large and corrupting memory or every (20 * 5) s
if ( (out.length() >= STRING_SIZE) || (++count > 20) )
{
Serial.print("\nOut String Length=");
Serial.println(out.length());

count = 0;
}
}

void drawGraph(AsyncWebServerRequest *request)
{
String out;

out.reserve(STRING_SIZE);
char temp[70];

out += "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"1810\" height=\"150\">\n";
out += "<rect width=\"1510\" height=\"150\" fill=\"rgb(250, 230, 210)\" stroke-width=\"2\" stroke=\"rgb(0, 0, 0)\" />\n";
out += "<g stroke=\"blue\">\n";
int y = rand() % 130;

for (int x = 10; x < 1500; x += 10)
{
int y2 = rand() % 130;
sprintf(temp, "<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" stroke-width=\"2\" />\n", x, 140 - y, x + 10, 140 - y2);
out += temp;
y = y2;
}

out += "</g>\n</svg>\n";

PrintHeapData("Pre Send");

PrintStringSize(out);

request->send(200, "image/svg+xml", out);

PrintHeapData("Post Send");
}

void initEthernet()
{
SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV4);
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);

#if !USING_DHCP
eth.config(localIP, gateway, netMask, gateway);
#endif

eth.setDefault();

if (!eth.begin())
{
Serial.println("No Ethernet hardware ... Stop here");

while (true)
{
delay(1000);
}
}
else
{
Serial.print("Connecting to network : ");

while (!eth.connected())
{
Serial.print(".");
delay(1000);
}
}

Serial.println();

#if USING_DHCP
Serial.print("Ethernet DHCP IP address: ");
#else
Serial.print("Ethernet Static IP address: ");
#endif

Serial.println(eth.localIP());
}

void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LED_OFF);

Serial.begin(115200);
while (!Serial && millis() < 5000);

delay(200);

Serial.print("\nStart Async_AdvancedWebServer_MemoryIssues_SendArduinoString on "); Serial.print(BOARD_NAME);
Serial.print(" with "); Serial.println(SHIELD_TYPE);
Serial.println(ASYNC_WEBSERVER_ETHERNET_VERSION);

PrintHeapData("Start =>");

initEthernet();

///////////////////////////////////

server.on("/", HTTP_GET, [](AsyncWebServerRequest * request)
{
handleRoot(request);
});

server.on("/test.svg", HTTP_GET, [](AsyncWebServerRequest * request)
{
drawGraph(request);
});

server.on("/inline", [](AsyncWebServerRequest * request)
{
request->send(200, "text/plain", "This works as well");
});

server.onNotFound(handleNotFound);

server.begin();

Serial.print(F("HTTP EthernetWebServer is @ IP : "));
Serial.println(eth.localIP());

PrintHeapData("Pre Create Arduino String");

}

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 9f7e380

Please sign in to comment.