0
0
IOT Protocolsdevops~10 mins

CBOR for constrained devices in IOT Protocols - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - CBOR for constrained devices
Start: Data to send
Encode data using CBOR
Transmit encoded data
Receive encoded data
Decode CBOR data
Use original data
Data is encoded into CBOR format, sent over the network, then decoded back to original data on the receiving device.
Execution Sample
IOT Protocols
import cbor2
data = {"temp": 22, "unit": "C"}
cbor_data = cbor2.dumps(data)
print(cbor_data)
decoded = cbor2.loads(cbor_data)
print(decoded)
Encode a simple temperature dictionary into CBOR bytes, then decode it back to dictionary.
Process Table
StepActionInputOutputNotes
1Prepare data{"temp": 22, "unit": "C"}{"temp": 22, "unit": "C"}Original data dictionary
2Encode data{"temp": 22, "unit": "C"}b'\xa2dtemp\x16dunit\x61C'CBOR binary encoding of data
3Transmit datab'\xa2dtemp\x16dunit\x61C'Sent over networkData sent as bytes
4Receive datab'\xa2dtemp\x16dunit\x61C'b'\xa2dtemp\x16dunit\x61C'Bytes received intact
5Decode datab'\xa2dtemp\x16dunit\x61C'{"temp": 22, "unit": "C"}CBOR bytes decoded back to dictionary
6Use data{"temp": 22, "unit": "C"}Display or process temperatureOriginal data restored for use
7EndProcess complete
💡 Data successfully encoded, transmitted, decoded, and ready for use on constrained device
Status Tracker
VariableStartAfter EncodingAfter TransmissionAfter DecodingFinal
data{"temp": 22, "unit": "C"}{"temp": 22, "unit": "C"}{"temp": 22, "unit": "C"}{"temp": 22, "unit": "C"}{"temp": 22, "unit": "C"}
cbor_dataNoneb'\xa2dtemp\x16dunit\x61C'b'\xa2dtemp\x16dunit\x61C'b'\xa2dtemp\x16dunit\x61C'None
decodedNoneNoneNone{"temp": 22, "unit": "C"}{"temp": 22, "unit": "C"}
Key Moments - 3 Insights
Why do we encode data into CBOR before sending it?
Encoding converts data into a compact binary form suitable for constrained devices and networks, as shown in execution_table step 2 where the dictionary becomes a small byte string.
What happens if the data is not decoded after receiving?
Without decoding (step 5), the device cannot use the raw bytes as meaningful data. Decoding restores the original dictionary for use, as seen in step 6.
Is the CBOR data human-readable during transmission?
No, CBOR data is binary and not human-readable, unlike the original dictionary. This is clear in step 2 and 3 where data is shown as bytes.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the output of encoding?
AA compact binary byte string
BA JSON string
CThe original dictionary
DAn error message
💡 Hint
Check the Output column at step 2 in execution_table
At which step does the data get restored to its original dictionary form?
AStep 3
BStep 5
CStep 1
DStep 4
💡 Hint
Look for decoding action and dictionary output in execution_table
If the data was not encoded before sending, what would change in the execution_table?
AStep 3 would send bytes anyway
BStep 5 would fail to decode
CStep 2 output would be the original dictionary, not bytes
DNo change at all
💡 Hint
Consider what encoding does to data before transmission in execution_table step 2
Concept Snapshot
CBOR (Concise Binary Object Representation) encodes data into compact binary form.
Used in constrained devices for efficient transmission.
Encode data before sending, decode after receiving.
Saves bandwidth and processing power compared to text formats.
Supports common data types like maps, arrays, strings, numbers.
Full Transcript
CBOR is a way to turn data into a small binary format. This helps devices with little memory or slow networks send data quickly. First, data like a temperature reading is encoded into CBOR bytes. These bytes are sent over the network. The receiving device gets the bytes and decodes them back into the original data. This process is shown step-by-step in the execution table. Encoding makes data smaller and easier to send. Decoding restores it for use. Without encoding, data would be larger and slower to send. Without decoding, the device cannot understand the received bytes. CBOR is great for IoT devices that need to save space and power.