revert: restore pre-4b59b89 web server (drop chunked API + low-memory auto-reboot)

The chunked API handlers and the maxblock<6000 auto-restart from 4b59b89
broke the web UI on-device; restore the working String-based handlers and
plain loop while keeping the scrollable forecast log.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-21 08:48:25 +07:00
co-authored by Claude Opus 4.8
parent f8294d4aed
commit 2bf49c14ac
2 changed files with 19 additions and 31 deletions
-9
View File
@@ -88,15 +88,6 @@ void loop() {
tSample = now; tSample = now;
sampleNow(); sampleNow();
displayRender(g_state); 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) { if (now - tHistory >= HISTORY_INTERVAL_MS) {
+19 -22
View File
@@ -27,44 +27,41 @@ static void handleCurrent() {
doc["haveTrend"] = g_state.haveTrend; doc["haveTrend"] = g_state.haveTrend;
doc["forecast"] = g_state.haveTrend ? g_state.forecast.text : "Collecting data..."; doc["forecast"] = g_state.haveTrend ? g_state.forecast.text : "Collecting data...";
doc["category"] = g_state.haveTrend ? categoryShort(g_state.forecast.category) : "..."; doc["category"] = g_state.haveTrend ? categoryShort(g_state.forecast.category) : "...";
doc["heap"] = ESP.getFreeHeap(); // for monitoring memory health
String out; String out;
serializeJson(doc, out); serializeJson(doc, out);
server.send(200, "application/json", out); server.send(200, "application/json", out);
} }
// Streamed (chunked) so we never build a large String/JsonDocument on the heap.
static void handleHistory() { static void handleHistory() {
server.setContentLength(CONTENT_LENGTH_UNKNOWN); JsonDocument doc;
server.send(200, "application/json", ""); JsonArray arr = doc.to<JsonArray>();
server.sendContent("[");
char buf[80];
int n = historyCount(); int n = historyCount();
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
Sample s = historyGet(i); Sample s = historyGet(i);
snprintf(buf, sizeof(buf), "%s{\"t\":%lu,\"msl\":%.1f,\"temp\":%.1f}", JsonObject o = arr.add<JsonObject>();
i ? "," : "", (unsigned long)s.epoch, s.mslHpa, s.tempC); o["t"] = s.epoch;
server.sendContent(buf); o["msl"] = s.mslHpa;
o["temp"] = s.tempC;
} }
server.sendContent("]"); String out;
serializeJson(doc, out);
server.send(200, "application/json", out);
} }
// Streamed (chunked). Forecast phrases contain no quotes/backslashes, so they
// are safe to embed directly in JSON.
static void handleForecasts() { static void handleForecasts() {
server.setContentLength(CONTENT_LENGTH_UNKNOWN); JsonDocument doc;
server.send(200, "application/json", ""); JsonArray arr = doc.to<JsonArray>();
server.sendContent("[");
char buf[128];
int n = flogCount(); int n = flogCount();
for (int i = n - 1, first = 1; i >= 0; i--, first = 0) { // newest first for (int i = n - 1; i >= 0; i--) { // newest first
FcastEntry e = flogGet(i); FcastEntry e = flogGet(i);
snprintf(buf, sizeof(buf), "%s{\"t\":%lu,\"cat\":\"%s\",\"text\":\"%s\"}", JsonObject o = arr.add<JsonObject>();
first ? "" : ",", (unsigned long)e.epoch, o["t"] = e.epoch;
categoryShort((WxCategory)e.cat), forecastTextForLetter(e.letter)); o["cat"] = categoryShort((WxCategory)e.cat);
server.sendContent(buf); o["text"] = forecastTextForLetter(e.letter);
} }
server.sendContent("]"); String out;
serializeJson(doc, out);
server.send(200, "application/json", out);
} }
static void handleGetSettings() { static void handleGetSettings() {