Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Firmware/v4.5_AiO_WEB/CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,12 @@ The addresses must not move: settings must survive a firmware update.
padding, to the next block; `diag.ram.stackFree` 46 KB -> 10 KB). New code that is not time-critical (web page
answers, JSON, set-up, text) gets `FLASHMEM`; keep the steering, GNSS, IMU and anything that runs during a flash
write (FlasherX, `FlashTxx.c` RAMFUNC) in RAM. The GitHub check fails under 32 KB "free for local variables".
14. **XIAO web answers never hold up its loop** (13 Nov 2026): the XIAO program serves its page through
`LinkWebServer`, a `WebServer` whose writes take what the connection takes and keep `teensy.poll()` going while
the browser catches up (the core's own write waited up to 10 s on a weak WiFi link: no hello, "XIAO silent for
10 s" in the Teensy log). Send every answer with `server.send` / `send_P` / `sendContent`, never
`server.client().write()`; a handler that waits for something else calls `teensy.poll()` itself. The page has
an ETag (build date, `Cache-Control: no-cache`, 304 when unchanged), as the Teensy page.

## Version and Publishing

Expand Down
4 changes: 4 additions & 0 deletions Firmware/v4.5_AiO_WEB/v4.5_XIAO_ESP32/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Version 4.5, the same number as the Teensy firmware it goes with (v4.5_AiO_WEB).

| Date | What changed |
|---|---|
| 13 Nov 2026 | Loading the page no longer stops the XIAO on a weak WiFi link (the Teensy saw "XIAO silent for 10 s"), and the browser keeps the page: a new visit only asks whether it changed ([What it does](#what-it-does)). |
| 11 Nov 2026 | The page checks that it is the page of the program running: after an update an old page a browser kept (old buttons, new data) fetches the new one by itself ([What it does](#what-it-does)). |
| 10 Nov 2026 | Tabs and the light/dark button 44 px high with 8 px corners; the WiFi tab's password to confirm in a card of its own, no longer a field across the page ([What it does](#what-it-does)). Messages give the size of an updated program, not 0 bytes. |
| 9 Nov 2026 | The page's questions (save and restart, install, the module's firmware, subnet) in its own box: the browser's pop-ups show nothing inside an AgOpenWeb web card, so those buttons did nothing there ([What it does](#what-it-does)). |
Expand All @@ -49,6 +50,9 @@ Version 4.5, the same number as the Teensy firmware it goes with (v4.5_AiO_WEB).
- the button at the top (☀ / ☾) switches it, and the phone remembers the choice;
- without a choice, the phone's setting (dark or light mode) decides;
- when the XIAO restarts (its uptime goes back, or another build runs), the page loads itself again, on the same tab.
- it goes out without holding up the XIAO: on a weak WiFi link the 53 kB page can take a few seconds, but the link to the Teensy (hello, data) keeps going meanwhile. Before 13 Nov 2026 the XIAO waited for the browser, and the Teensy log showed "XIAO silent for 10 s";
- the browser keeps the page and asks the XIAO each time whether it changed: the same build answers "not modified" (a few hundred bytes), so only the first visit after an update sends the whole page;
- a spare connection a browser opens and leaves silent is dropped after 1 s when another one is waiting (it used to hold up the next request for 5 s).

| Tab | What it shows |
|---|---|
Expand Down
79 changes: 77 additions & 2 deletions Firmware/v4.5_AiO_WEB/v4.5_XIAO_ESP32/v4.5_XIAO_ESP32.ino
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
#include "esp_system.h"
#include "esp_task_wdt.h"
#include "esp_app_format.h" // header of a program file (.bin): which chip it is for
#include "lwip/sockets.h" // send() with MSG_DONTWAIT: answers that never hold up loop()
#include <errno.h>
#include "xVersion.h"
#include "xLink.h"
#include "xPage.h"
Expand All @@ -67,7 +69,56 @@

HardwareSerial& TeensyPort = Serial1;
TeensyLink<HardwareSerial> teensy(TeensyPort);
WebServer server(80);

// ---------------------------------------------------------------- web server that never holds up the Teensy link
// 13 Nov 2026. The core's WebServer writes an answer in one go and returns only when the browser has taken all of
// it. The page is 53 kB: on a weak WiFi link (Starlink, -72 dBm) that took about 10 s, and all that time loop()
// was away from the Teensy - no hello ("XIAO silent for 10 s" in the Teensy log), its data piling up unread.
// This one gives the connection what it takes now and, while the browser catches up, keeps the link to the
// Teensy going (the hello, its data), as the uploads already do. The Teensy page sends its page in pieces too.
#define SEND_WAIT_MS 20000 // a browser that takes nothing for this long is given up (= UPLOAD_WAIT_MS)
#define SPARE_IDLE_MS 1000 // a connection that asked nothing for this long gives way to the next one
class LinkWebServer : public WebServer
{
public:
explicit LinkWebServer(int port) : WebServer(port) {}

// Browsers open a spare connection in advance and may leave it silent. The core waits up to 5 s for it to
// ask something (HTTP_MAX_DATA_WAIT) and serves no one else meanwhile: when another connection is waiting,
// the silent one is dropped after SPARE_IDLE_MS (by the core's own clean-up). The browser opens a new one.
void handleClient() override
{
if (_currentStatus == HC_WAIT_READ && millis() - _statusChange > SPARE_IDLE_MS && !_currentClient.available() && _server.hasClient())
_statusChange = millis() - HTTP_MAX_DATA_WAIT - 1;
WebServer::handleClient();
}

protected:
size_t _currentClientWrite(const char* b, size_t l) override { return writeKeepingLink(b, l); }
size_t _currentClientWrite_P(PGM_P b, size_t l) override { return writeKeepingLink(b, l); }

private:
size_t writeKeepingLink(const char* b, size_t l)
{
const int fd = _currentClient.fd();
if (fd < 0) return 0;
size_t done = 0;
uint32_t takenMs = millis();
while (done < l)
{
const int r = ::send(fd, b + done, l - done, MSG_DONTWAIT); // what the connection's buffer takes now (~5.7 kB)
if (r > 0) { done += r; takenMs = millis(); continue; }
if (r < 0 && errno != EAGAIN && errno != EWOULDBLOCK) break; // the browser has gone
if (millis() - takenMs >= SEND_WAIT_MS) break; // it took nothing for 20 s: given up
teensy.poll(millis()); // meanwhile: the hello, the Teensy's data
feedLoopWDT();
delay(1); // and the WiFi gets the core
}
return done;
}
};

LinkWebServer server(80);
DNSServer dns;
Preferences prefs;

Expand Down Expand Up @@ -773,11 +824,35 @@ static void apiRestart()
rebootAtMs = millis() + 1000;
}

// The page's ETag: its build date (13 Nov 2026, as the Teensy page). The page is served with "Cache-Control:
// no-cache", so the browser keeps it but asks every time ("If-None-Match"); the same build answers "304 Not
// Modified" (a few hundred bytes), a new build sends the page. So the 53 kB page only travels over the WiFi once
// per build and browser, not at every visit.
static char pageEtag[40];
static void pageEtagSetup()
{
const char* b = __DATE__ " " __TIME__; // e.g. Nov 3 2026 14:20:11 -> "Nov-3-2026-14:20:11" (no spaces in an ETag)
size_t k = 0;
pageEtag[k++] = '"';
for (const char* p = b; *p && k < sizeof(pageEtag) - 2; p++)
{
if (*p == ' ') { if (pageEtag[k - 1] != '-') pageEtag[k++] = '-'; }
else pageEtag[k++] = *p;
}
pageEtag[k++] = '"';
pageEtag[k] = '\0';
}

static void webSetup()
{
pageEtagSetup();
static const char* keep[] = {"If-None-Match"};
server.collectHeaders(keep, 1);
server.on("/", HTTP_GET, []() {
if (captiveRedirect()) return;
server.sendHeader("Cache-Control", "no-store");
server.sendHeader("ETag", pageEtag);
server.sendHeader("Cache-Control", "no-cache");
if (server.header("If-None-Match").indexOf(pageEtag) >= 0) { server.send(304); return; }
server.send_P(200, "text/html; charset=utf-8", XIAO_PAGE);
});
server.on("/api/state", HTTP_GET, apiState);
Expand Down
2 changes: 1 addition & 1 deletion Firmware/v4.5_AiO_WEB/v4.5_XIAO_ESP32_bin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ The same program (`v4.5_XIAO_ESP32`, for the Teensy firmware v4.5_AiO_WEB), comp

The ESP32C3 and the ESP32S3 are different chips: each needs its own file.

**Built:** 11 Nov 2026 (C3 09:34, S3 09:35), from the program as it is after the pull request that makes the page check it belongs to the program running. Compiled in the cloud with the ESP32 core 3.3.12 (arduino-cli, the board's default settings, the same as in the program's README).
**Built:** 13 Nov 2026 (C3 13:25, S3 13:25), from the program as it is after the pull request that keeps the link to the Teensy going while the page loads on a weak WiFi link. Compiled in the cloud with the ESP32 core 3.3.12 (arduino-cli, the board's default settings, the same as in the program's README).
When the program in `v4.5_XIAO_ESP32` changes, compile these files again and upload them over the old ones, with the same names. **Install from GitHub** on the XIAO page offers a file when it was compiled later than the program running on the XIAO (the date is read from the file).

| File | What for |
Expand Down
Binary file modified Firmware/v4.5_AiO_WEB/v4.5_XIAO_ESP32_bin/v4.5_XIAO_ESP32C3.bin
Binary file not shown.
Binary file not shown.
Binary file modified Firmware/v4.5_AiO_WEB/v4.5_XIAO_ESP32_bin/v4.5_XIAO_ESP32S3.bin
Binary file not shown.
Binary file not shown.