Connection & poll-cycle flow
Companion to Protocol messages, which documents each request/response pair's wire format in detail, and to Server behavior, which explains the rules behind each step.
Device registration, end to end
Two independent conversations that never touch each other, joined by one shared key. The app talks to the logger over BLE and to the backend over WebSocket; the logger talks to the backend over TCP. Neither ever tells the other who it is — they each tell the server, and the server matches.
The shared key is the logger's part number: printed on its QR label (which the app scans) and stored in its firmware (which it reports on request). The two sources spell it in different cases, so both are converted to one canonical lowercase spelling the moment they enter the system.
Full connection lifecycle
Identity comes first, and that ordering is the point. AT+DTUPN? is
answered by the DTU itself and needs no Modbus profile, so asking who this is
costs one round trip and then unlocks everything already known about the
device — which profile it speaks, and its Modbus batch limit. Both were
previously rediscovered on every single connection.
A refused connection is not closed. Closing would only make the DTU dial straight back in, so it is held open as unidentified, exactly like an unregistered one, and nothing about the device row is modified.
The unidentified path still heartbeats because AT+HTBT is the DTU
firmware's own keep-alive: without it the device drops and reconnects, which
costs ~950ms of device round trips against ~33ms for a heartbeat. What it
skips — batch-limit discovery, telemetry, presence — would all be work for
readings no one could receive, since they would carry an IP:port key no
client can name.
What a reconnect actually costs
Long-lived TCP connections mean every deploy reconnects devices, so the connect path's cost is multiplied by the fleet.
| Before | Now | |
|---|---|---|
| Profile detection | 1-2 round trips, every time | 0 — remembered model, 1 health-check read |
| Logger identity | 1 | 1 |
| Inverter serial | 1 | 1 |
| Batch-limit discovery | ~7 (binary search) | 0 — remembered |
| Device round trips | ~10-11 | ~3 |
| Database | 4 round trips (BEGIN/SELECT/UPDATE/COMMIT), always a write | 2 reads (lookup, then resolve), and no write at all when nothing changed |
Measured on real hardware, the old path took 2.0s (Aotai) to 7.9s (IP20) before the first telemetry cycle.
Only the profile cache corrects itself: a remembered model is health-checked and confirmed against the bound inverter serial before it is trusted, and a profile only replaces it by reading that serial (see Profile confirmation). A remembered batch limit that has gone stale makes batch reads fail and the connection close, but nothing clears the stored value, so the next connection repeats the failure until it is cleared by hand — see Server behavior.
The largest lever is not in this diagram. How much of the fleet
reconnects at once is a deployment setting: minHealthyPercent on the ECS
service (see cdk/lib/tcp-ingest-stack.ts) bounds the share of tasks that
can be stopped together. At 90, a tenth of the fleet cycles at a time
instead of a third. No amount of per-connection tuning matters if the whole
fleet is dropped simultaneously. A single-task environment cannot keep any
floor — the current dev deployment (pilot-ecs-ingest-stack.ts, one task)
runs at 0, so every deploy there reconnects every device at once.
:::note Who speaks first
The DTU opens the TCP connection — it is behind NAT and nothing can reach it
from outside — and then says nothing. Every exchange after that is
started by the server, including the AT+HTBT? keep-alive: per the vendor's
own guide, the cloud server must send it every ~30s or the DTU decides the
connection is dead and reconnects. The DTU's watchdog watches us, not the
other way round.
:::
Per-request round trip
Every request/response pair above — AT+DTUPN, health check, serial read,
heartbeat, batch discovery, telemetry batch, register write and read — goes
through this same round trip and correlation logic. This is what stops a
late or partial answer to a previous, already-abandoned request from being
misattributed to a different one.
Reading a complete frame matters because the transparent serial-to-Ethernet
bridge forwards RS-485 bytes as they trickle in: a large batch response
arrives over several TCP segments, and taking whatever one Read returned
used to truncate it. The drain is armed on every read failure, not only
timeouts, because a truncated frame's tail left in the socket would
otherwise be read as the next request's response — the start of a desync
that never recovered, which is what surfaced as a Modbus frame being parsed
as an AT+HTBT reply.
Only a socket-level failure (EOF, reset, broken pipe) aborts a telemetry cycle outright; a timeout or bad frame on one batch marks that batch's registers failed and the cycle continues.
Heartbeat retry
A heartbeat tick gets two AT+HTBT exchanges before the connection
is closed (Session.HeartbeatWithRetry). A dead socket (reset, EOF) is not
retried.
On 2026-09-29/30 an Aotai (116.90.113.79) and an IP20 (58.84.28.34) closed 26 connections after a single failed exchange:
- 20 were read timeouts.
- 6 were
expected an AT response, got byte 0x01 — connection is out of sync. The0x01is a late Modbus answer to an earlier timed-out read, arriving while the heartbeat waits for itsATreply.
In 25 of the 26 cases the logger dialled back within 1–58 s, so it had not
gone away. Two further drops were connection reset by peer, which a retry
cannot help and which still close at once.
The retry is sent after the drain above has discarded the late bytes, and well inside the DTU's ~30 s watchdog. When a retry recovers, the drain is armed again: the failed attempt's own reply may still arrive, and would otherwise be read as the next Modbus response.
The retry should cure the out-of-sync drops. For timeouts it helps only when the link stall is short. On 30 Sep 13:10–13:19 the Aotai timed out every 1–2 minutes, which a single retry may not ride out. If drops like that persist, the next lever is the 5 s read timeout for high-latency links.
Log lines: heartbeat attempt failed, retrying and heartbeat recovered on retry (Info). heartbeat failed, closing connection now means two
exchanges in a row failed.
Batches stay inside one vendor sheet
Telemetry is read in batches: neighbouring registers are merged into one
Modbus read of up to the device's batch limit, gaps included. For the
Yorker that is only safe within one sheet of the vendor's
register map (WF_BAT, WF_BMS, WF_PV, WF_NTC, …). Its firmware serves each
sheet as a separate table and answers any read that crosses into the next
sheet with Modbus exception 0x04, although every register in it reads fine
on its own. This was confirmed on the bench on 2026-10-06: 0x0180+35
(WF_BAT into WF_BMS), 0x0240+47 (WF_OP into WF_PV) and 0x0320+23 (WF_FAN
into WF_NTC) were refused, and the same registers read one by one
answered (temperatures, fan speed, PV voltage, BMS block). The profile's
batch_within_sheet: true makes batching split at every sheet boundary
(modbus.Profile.Coalesce). That took the bench unit from 293 to 353
decoded registers per cycle. The Aotai profile does not set it, because
its devices accept spans across sheets and splitting would only add round
trips.
Suspending batches that always fail
A telemetry read batch is suspended when it has never answered on this device and has failed 3 cycles in a row, in cycles where other batches of the same device succeeded. It is not requested again for an hour, then gets one re-probe. A failed re-probe suspends it again; a successful one restores it for good.
Two rules keep this from hiding data a device does report:
- Never answered. A batch that has succeeded even once on the device is never suspended, however often it later fails. A working range on a flaky link is not an unsupported range.
- Others succeeded. A cycle in which every batch failed counts against none of them. A device that stops answering must not lose its whole register map.
The state is kept in tcpserver memory per device key, so it survives the
device's reconnects. Only devices that have failed a never-answered batch
are remembered; a device whose batches all answer costs nothing. A
restarted tcpserver relearns in three cycles. One gap follows from that: if
a working batch fails its first three cycles right after a restart, before
it has answered once, it is suspended for up to an hour. Code:
internal/server/batch_health.go.
Why: the IP20 failed the same three batches (addresses 384+35, 576+47 and
800+23: the BMS, phase-C/total-PV and temperature/fan blocks) on 86–90 of
its 90 cycles on 29 Sep. That was first put down to the unit not
implementing them. It does: each of those batches crossed from one vendor
sheet into the next, which the IP20 firmware refuses (see
Batches stay inside one vendor sheet).
They took about 7 s of each ~11 s cycle; the Aotai's whole cycle is about
3 s. Their late replies caused 5 of the IP20's 14 heartbeat drops, and they
made every IP20 row _valid: false. Suspension is still what handles a
range a device really lacks, such as the IP20's WF_PARA block (0x0480+) on
a unit that is not in parallel.
Registers in a suspended batch are left out of the cycle and of
failed_registers. So are registers in a batch the device has never
answered and has refused with an explicit Modbus exception: such a refusal
says "not implemented here", not "the reading went missing". So _valid
is true once the only failures are ranges the device does not implement,
from the first cycle after a restart. Timeouts and corrupted replies still
count. The telemetry snapshot log line reports
them as skipped_registers. Log lines: telemetry batch keeps failing on this device; suspending it and suspended telemetry batch answered its re-probe; reading it again (Info, once per change, not per cycle).
Want to see the request/response bytes for yourself? The Sandbox builds a real request frame from a chosen register and shows a simulated response going through the same validation.