Cbor - compact binary telemetry with CBOR
Layer: L6 Presentation ยท Build flags: PROTOCORE_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 mmgr_span. You initialize the writer over a stack buffer, declare the map size, then emit key/value pairs; w.overflow reports overflow and w.pos gives the encoded length:
uint8_t buf[64];
mmgr_span w;
const SpatiumCfg w_args = {.buf = buf, .cap = sizeof(buf)};
w = spat.from(&w_args);
Cbor.put_map(&w, 3);
Cbor.put_str(&w, "heap"); Cbor.put_uint(&w, ESP.getFreeHeap());
Cbor.put_str(&w, "uptime"); Cbor.put_uint(&w, millis() / 1000);
Cbor.put_str(&w, "rssi"); Cbor.put_int(&w, Physical.wifi->rssi());
ctx.len = w.overflow ? 0 : w.pos;
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 protocore_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", protocore_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=-DPROTOCORE_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 PROTOCORE_ENABLE_CBOR 1
#include "protocore.h"
static const char *SSID = "YOUR_SSID";
static const char *PASSWORD = "YOUR_PASSWORD";
PC server;
struct CborCtx
{
uint8_t buf[64];
size_t len, off;
};
static size_t protocore_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;
mmgr_span w;
const SpatiumCfg w_args = {.buf = ctx.buf, .cap = sizeof(ctx.buf)};
w = spat.from(&w_args);
Cbor.put_map(&w, 3);
Cbor.put_str(&w, "heap");
Cbor.put_uint(&w, ESP.getFreeHeap());
Cbor.put_str(&w, "uptime");
Cbor.put_uint(&w, millis() / 1000);
Cbor.put_str(&w, "rssi");
Cbor.put_int(&w, Physical.wifi->rssi());
ctx.len = w.overflow ? 0 : w.pos;
ctx.off = 0;
server.send_chunked(id, 200, "application/cbor", protocore_cbor_source, &ctx);
});
server.begin(80);
}
void loop()
{
server.handle();
}
Layer 6 (Presentation) - zero-heap CBOR (RFC 8949) encoder.
@ HTTP_GET
Safe, idempotent read.
Layer 1 (Physical) - link bring-up, the interface registry, and live egress reporting.
EMBED_INLINE uint32_t protocore_physical_egress_ipv4(void)
Physical.egress_ip as an expression: the address a sketch prints once its link is up.
EMBED_INLINE embed_bool protocore_physical_wifi_up(void)
Physical.wifi_ready as an expression, for a bring-up loop to wait on.
PhysicalVars PhysicalV
The operands and the outcome.
uint8_t * protocore_physical_span(void)
The PROTOCORE_PHYSICAL_BORROW bytes this module's state lives in.