#include <Arduino.h>
#include <tinycbor.h>
#include <string.h>
uint8_t encode_buffer[1024];
void setup() {
Serial.begin(115200);
Serial.println("--- CBOR Encode Example ---");
// Initialize TinyCBOR library.
TinyCBOR.init();
// Assign buffer to encoder.
TinyCBOR.Encoder.init(encode_buffer, sizeof(encode_buffer));
// Start CBOR encoding
TinyCBOR.Encoder.create_map(2);
// Encode temperature
TinyCBOR.Encoder.encode_text_string("temperature", 11);
TinyCBOR.Encoder.encode_int(25);
// Encode location
TinyCBOR.Encoder.encode_text_string("location", 8);
TinyCBOR.Encoder.encode_text_string("Living Room", 11);
// Close the map
TinyCBOR.Encoder.close_container();
// Retrieve encoded data
uint8_t* cborData = TinyCBOR.Encoder.get_buffer();
size_t cborDataSize = TinyCBOR.Encoder.get_buffer_size();
// Print the CBOR-encoded data
Serial.println("CBOR-encoded data:");
for (size_t i = 0; i < cborDataSize; i++) {
Serial.print("0x");
if (cborData[i] < 0x10) Serial.print("0");
Serial.print(cborData[i], HEX);
if (i != cborDataSize - 1) Serial.print(", ");
}
Serial.println();
Serial.println("--- End of CBOR Encode Example ---");
}
void loop() {
// Your loop code here
}