import cbor2 encoded = cbor2.dumps(10) print(encoded.hex())
CBOR encodes small positive integers (0-23) as a single byte where the first 3 bits are the major type (0 for unsigned integers) and the last 5 bits are the value. For 10, this is 0x0a.
CBOR is a compact binary format designed to minimize data size and processing overhead, which is ideal for devices with limited resources.
The byte sequence 'bf61616161626162ff' starts an indefinite-length map ('bf'). It defines key "a" (6161) with value "b" (6162), then another key "b" (6162), but lacks a value for the second key before the break ('ff'), making the map incomplete and causing the decoding error.
First, collect sensor data, then organize it into a structure, encode it with CBOR, and finally transmit the encoded bytes.
Streaming encoding allows encoding data piece by piece, reducing peak memory usage, which is critical for constrained devices.