gate: replace JSON with nanopb (issue #8) #92

Merged
joaj merged 12 commits from gate-nanopb/gate-nanopb into master 2026-09-27 10:52:54 +00:00
Owner

Replaces the newline-delimited JSON gate protocol with length-prefixed binary protobuf (nanopb on the firmware, betterproto on the server). Closes #8.

Replaces the newline-delimited JSON gate protocol with length-prefixed binary protobuf (nanopb on the firmware, betterproto on the server). Closes #8.
joaj added 11 commits 2026-09-15 21:20:43 +00:00
firmware: reference canonical gate.proto in libs/ for nanopb
All checks were successful
ci/woodpecker/pr/main Pipeline was successful
7452641281
The firmware previously kept its own copy of gate.proto for nanopb
codegen while betterproto generated Python bindings from
libs/timekeeping_protocol/gate.proto. Drift between the two copies
could silently desynchronise the wire format.

Remove the duplicate and point the nanopb generator at the canonical
schema in libs/ in place.
joaj merged commit 7e7e21c0a2 into master 2026-09-27 10:52:54 +00:00
joaj deleted branch gate-nanopb/gate-nanopb 2026-09-27 10:52:54 +00:00
Author
Owner

Bandwidth comparison: JSON → protobuf

Byte-for-byte comparison of identical payloads, old encoding replicated exactly from the pre-protobuf firmware snprintf payloads (4c6a673~1), new encoding from the nanopb firmware / betterproto server.

Per-message size (same data, both encodings)

Message JSON (old) Protobuf (new) Saving
Trigger (GPS-verified, seq 57) 129 B 23 B 82.2%
Heartbeat (full telemetry: GPS + SD + battery) 302 B 63 B 79.1%
Log line I/NET (32-char msg) 79 B 42 B 46.8%
log_line (embedded 151-char JSONL trigger) 181 B 152 B 16.0%

Bitrate impact

Steady-state race model — 2 gates, each with 1 Hz heartbeat + 1 log line/s + 2 triggers/min:

  • JSON: 6.2 kbit/s → Protobuf: 1.7 kbit/s — 72.5% reduction (~24 MB/day less across both gates)

Trigger burst (worst case for timing precision, e.g. 6 vehicles/min through the track): each trigger event drops from 130 B to 23 B.

Where the savings come from

  1. No JSON key names on the wire — "gate_id":"timestamp"... field labels become 1–2 byte protobuf tag prefixes
  2. Binary integers — a 16-digit µs timestamp is 8–9 bytes instead of 16, and uint32 values like battery_mv are 2 bytes instead of 4 ASCII digits
  3. Bools are 1 byte (true → 1 byte) instead of 4–5 ASCII chars
  4. The main remaining cost is the heartbeat's fixed strings (fw_version, git_sha) and the embedded JSONL in log_line — those are incompressible in both formats.

Framing overhead is comparable (2-byte LE prefix vs 1-byte newline delimiter), so it doesn't change the picture.

Bottom line: ~72–80% bandwidth reduction on the dominant message types, with the biggest absolute win on heartbeats (239 B saved per gate per second) since those are sent most frequently.

## Bandwidth comparison: JSON → protobuf Byte-for-byte comparison of identical payloads, old encoding replicated exactly from the pre-protobuf firmware `snprintf` payloads (4c6a673~1), new encoding from the nanopb firmware / betterproto server. ### Per-message size (same data, both encodings) | Message | JSON (old) | Protobuf (new) | Saving | |---|---:|---:|---:| | Trigger (GPS-verified, seq 57) | 129 B | 23 B | **82.2%** | | Heartbeat (full telemetry: GPS + SD + battery) | 302 B | 63 B | **79.1%** | | Log line `I/NET` (32-char msg) | 79 B | 42 B | 46.8% | | `log_line` (embedded 151-char JSONL trigger) | 181 B | 152 B | 16.0% | ### Bitrate impact Steady-state race model — 2 gates, each with 1 Hz heartbeat + 1 log line/s + 2 triggers/min: - **JSON: 6.2 kbit/s → Protobuf: 1.7 kbit/s** — **72.5% reduction** (~24 MB/day less across both gates) Trigger burst (worst case for timing precision, e.g. 6 vehicles/min through the track): each trigger event drops from 130 B to 23 B. ### Where the savings come from 1. **No JSON key names on the wire** — `"gate_id":"timestamp"...` field labels become 1–2 byte protobuf tag prefixes 2. **Binary integers** — a 16-digit µs timestamp is 8–9 bytes instead of 16, and `uint32` values like `battery_mv` are 2 bytes instead of 4 ASCII digits 3. **Bools are 1 byte** (`true` → 1 byte) instead of 4–5 ASCII chars 4. The main *remaining* cost is the heartbeat's fixed strings (`fw_version`, `git_sha`) and the embedded JSONL in `log_line` — those are incompressible in both formats. Framing overhead is comparable (2-byte LE prefix vs 1-byte newline delimiter), so it doesn't change the picture. **Bottom line: ~72–80% bandwidth reduction on the dominant message types, with the biggest absolute win on heartbeats (239 B saved per gate per second) since those are sent most frequently.**
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
fspt/timekeeping!92
No description provided.