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

Commit fb82506

Browse files
authored
v1.4.0 to save heap
1 parent 1ef95e5 commit fb82506

File tree

23 files changed

+772
-38
lines changed

23 files changed

+772
-38
lines changed

examples/Ethernet/Async_AdvancedWebServer/Async_AdvancedWebServer.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
#endif
4444

4545
#define _PORTENTA_H7_ATCP_LOGLEVEL_ 1
46-
#define _PORTENTA_H7_AWS_LOGLEVEL_ 1
46+
#define _PORTENTA_H7_AWS_LOGLEVEL_ 4
4747

4848
#define USE_ETHERNET_PORTENTA_H7 true
4949

@@ -177,7 +177,7 @@ void setup()
177177
digitalWrite(LED_BUILTIN, LED_OFF);
178178

179179
Serial.begin(115200);
180-
while (!Serial);
180+
while (!Serial && millis() < 5000);
181181

182182
delay(200);
183183

examples/Ethernet/Async_AdvancedWebServer_MemoryIssues_SendArduinoString/Async_AdvancedWebServer_MemoryIssues_SendArduinoString.ino

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/****************************************************************************************************************************
2-
Async_AdvancedWebServer_MemoryIssues_Send_CString.ino - Dead simple AsyncWebServer for STM32 LAN8720 or built-in LAN8742A Ethernet
2+
Async_AdvancedWebServer_MemoryIssues_SendArduinoString.ino - - Dead simple AsyncWebServer for Portenta_H7
33
44
For Portenta_H7 (STM32H7) with Vision-Shield Ethernet
55
@@ -53,6 +53,9 @@
5353

5454
#include <Portenta_H7_AsyncWebServer.h>
5555

56+
// In bytes
57+
#define STRING_SIZE 40000
58+
5659
// Enter a MAC address and IP address for your controller below.
5760
#define NUMBER_OF_MAC 20
5861

@@ -146,26 +149,49 @@ void handleNotFound(AsyncWebServerRequest *request)
146149
digitalWrite(LED_BUILTIN, LED_OFF);
147150
}
148151

149-
void PrintHeapData(String hIn){
150-
mbed_stats_heap_t heap_stats;
151-
152-
Serial.print("HEAP DATA - ");
153-
Serial.print(hIn);
152+
void PrintHeapData(String hIn)
153+
{
154+
static mbed_stats_heap_t heap_stats;
155+
static uint32_t maxHeapSize = 0;
154156

155157
mbed_stats_heap_get(&heap_stats);
156-
Serial.print(" Cur heap: ");
157-
Serial.print(heap_stats.current_size);
158-
Serial.print(" Res Size: ");
159-
Serial.print(heap_stats.reserved_size);
160-
Serial.print(" Max heap: ");
161-
Serial.println(heap_stats.max_size);
158+
159+
// Print and update only when different
160+
if (maxHeapSize != heap_stats.max_size)
161+
{
162+
maxHeapSize = heap_stats.max_size;
163+
164+
Serial.print("\nHEAP DATA - ");
165+
Serial.print(hIn);
166+
167+
Serial.print(" Cur heap: ");
168+
Serial.print(heap_stats.current_size);
169+
Serial.print(" Res Size: ");
170+
Serial.print(heap_stats.reserved_size);
171+
Serial.print(" Max heap: ");
172+
Serial.println(heap_stats.max_size);
173+
}
174+
}
175+
176+
void PrintStringSize(String & out)
177+
{
178+
static uint32_t count = 0;
179+
180+
// Print only when cStr length too large and corrupting memory or every (20 * 5) s
181+
if ( (out.length() >= STRING_SIZE) || (++count > 20) )
182+
{
183+
Serial.print("\nOut String Length=");
184+
Serial.println(out.length());
185+
186+
count = 0;
187+
}
162188
}
163189

164190
void drawGraph(AsyncWebServerRequest *request)
165191
{
166192
String out;
167193

168-
out.reserve(40000);
194+
out.reserve(STRING_SIZE);
169195
char temp[70];
170196

171197
out += "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"1810\" height=\"150\">\n";
@@ -180,19 +206,18 @@ void drawGraph(AsyncWebServerRequest *request)
180206
out += temp;
181207
y = y2;
182208
}
209+
183210
out += "</g>\n</svg>\n";
184211

185212
PrintHeapData("Pre Send");
186213

187-
Serial.print("Out String Length=");
188-
Serial.println(out.length());
214+
PrintStringSize(out);
189215

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

192218
PrintHeapData("Post Send");
193219
}
194220

195-
196221
void setup()
197222
{
198223
pinMode(LED_BUILTIN, OUTPUT);
@@ -264,7 +289,6 @@ void setup()
264289
Serial.print(F("HTTP EthernetWebServer is @ IP : "));
265290
Serial.println(Ethernet.localIP());
266291

267-
268292
PrintHeapData("Pre Create Arduino String");
269293

270294
}

0 commit comments

Comments
 (0)