fix: stream large API responses to stop heap-exhaustion truncation

Serving /api/history (and /api/forecasts) built a ~12 KB String +
JsonDocument every 15 s; over days this fragmented the ESP8266 heap until
the TCP stack could no longer send a full response, causing the web page
to fail with ERR_CONTENT_LENGTH_MISMATCH. Stream both array endpoints via
chunked transfer (small stack buffer, no big allocations). Add free-heap
to /api/current and Serial, plus a low-memory safety restart (history is
persisted first).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-21 06:57:09 +07:00
co-authored by Claude Opus 4.8
parent 1e923249f6
commit 4b59b8936b
2 changed files with 31 additions and 19 deletions
+9
View File
@@ -88,6 +88,15 @@ void loop() {
tSample = now;
sampleNow();
displayRender(g_state);
Serial.printf("heap=%u maxblock=%u\n", ESP.getFreeHeap(), ESP.getMaxFreeBlockSize());
// Safety net: if the largest contiguous block gets too small, the TCP stack
// can no longer send responses. Persist and reboot to defragment.
if (ESP.getMaxFreeBlockSize() < 6000) {
Serial.println(F("Low memory -> saving history and restarting"));
historySave();
delay(50);
ESP.restart();
}
}
if (now - tHistory >= HISTORY_INTERVAL_MS) {