Layer: L6 Presentation ยท Build flags: PC_ENABLE_CBOR
What this example teaches
CBOR (RFC 8949) encodes the same data as JSON in a fraction of the bytes, which matters for high-rate telemetry or constrained uplinks. This serves a small {heap, uptime, rssi} map as application/cbor using the zero-heap CBOR writer, streamed through the binary-safe chunked writer.
Encoding with CborWriter. You initialize the writer over a stack buffer, declare the map size, then emit key/value pairs; pc_cbor_ok() reports overflow and pc_cbor_len() gives the encoded length:
uint8_t buf[64];
CborWriter w;
pc_cbor_init(&w, buf, sizeof(buf));
pc_cbor_map(&w, 3);
pc_cbor_text(&w, "heap"); pc_cbor_uint(&w, ESP.getFreeHeap());
pc_cbor_text(&w, "uptime"); pc_cbor_uint(&w, millis() / 1000);
pc_cbor_text(&w,
"rssi"); pc_cbor_int(&w,
pc_net_rssi());
ctx.len = pc_cbor_ok(&w) ? pc_cbor_len(&w) : 0;
int8_t pc_net_rssi(void)
Station link RSSI in dBm, or 0 if not associated (and on host builds).
Why send_chunked(). The response is binary, so it is sent with the binary-safe send_chunked() rather than the C-string send(). send_chunked() pulls the body from a ChunkSource generator, which hands back the encoded bytes a slice at a time (here the whole small map in one go) and returns 0 to finish:
struct CborCtx { uint8_t buf[64]; size_t len, off; };
static size_t pc_cbor_source(uint8_t *out, size_t cap, void *vctx) {
CborCtx *c = (CborCtx *)vctx;
if (c->off >= c->len) return 0;
size_t n = c->len - c->off; if (n > cap) n = cap;
memcpy(out, c->buf + c->off, n); c->off += n; return n;
}
...
static CborCtx ctx;
server.send_chunked(id, 200, "application/cbor", pc_cbor_source, &ctx);
For a text-based compact binary alternative, see MsgPack.
Build and run
pio ci --board=esp32dev --project-option="framework=arduino" \
--project-option="build_flags=-DPC_ENABLE_CBOR=1" \
--lib="." examples/L6-Presentation/Cbor/Cbor.ino
curl -s http://<ip>/telemetry.cbor | xxd # inspect the compact binary
Annotated source
The complete sketch (Cbor.ino), reproduced verbatim with added explanatory comments:
#define PC_ENABLE_CBOR 1
static const char *SSID = "YOUR_SSID";
static const char *PASSWORD = "YOUR_PASSWORD";
struct CborCtx
{
uint8_t buf[64];
size_t len, off;
};
static size_t pc_cbor_source(uint8_t *out, size_t cap, void *vctx)
{
CborCtx *c = (CborCtx *)vctx;
if (c->off >= c->len)
return 0;
size_t n = c->len - c->off;
if (n > cap)
n = cap;
memcpy(out, c->buf + c->off, n);
c->off += n;
return n;
}
void setup()
{
Serial.begin(115200);
Serial.print("Connecting to WiFi");
{
delay(250);
Serial.print('.');
}
Serial.printf("IP: %u.%u.%u.%u\n", (unsigned)(ip & 0xFF), (unsigned)((ip >> 8) & 0xFF),
(unsigned)((ip >> 16) & 0xFF), (unsigned)((ip >> 24) & 0xFF));
static CborCtx ctx;
CborWriter w;
pc_cbor_init(&w, ctx.buf, sizeof(ctx.buf));
pc_cbor_map(&w, 3);
pc_cbor_text(&w, "heap");
pc_cbor_uint(&w, ESP.getFreeHeap());
pc_cbor_text(&w, "uptime");
pc_cbor_uint(&w, millis() / 1000);
pc_cbor_text(&w, "rssi");
ctx.len = pc_cbor_ok(&w) ? pc_cbor_len(&w) : 0;
ctx.off = 0;
server.
send_chunked(
id, 200,
"application/cbor", pc_cbor_source, &ctx);
});
}
void loop()
{
}
Layer 6 (Presentation) - zero-heap CBOR (RFC 8949) encoder.
Single-port HTTP server with deterministic, zero-allocation execution.
void send_chunked(uint8_t slot_id, int code, const char *content_type, ChunkSource source, void *ctx=nullptr)
Stream a response body of unknown length via chunked transfer.
int32_t begin(const WebServerConfig *cfg=nullptr)
Initialize all connection slots and open all registered listeners.
void on(const char *path, HttpMethod method, Handler callback)
Register a route handler.
void handle()
Drive the server - call every Arduino loop() iteration.
bool init_wifi_physical(const char *, const char *)
Connect to a WiFi access point.
uint32_t pc_net_egress_ip(void)
IPv4 (network byte order) of the current egress interface, or 0 if none.
bool wifi_ready()
True if the WiFi station link is up (associated + an IP is assigned).
Layer 1 (Physical) - link bring-up and live egress-interface reporting.
Layer 7 (Application) - public HTTP routing API.
@ HTTP_GET
Safe, idempotent read.
Fully-parsed HTTP/1.1 request.