Open om26er opened 6 months ago
#ifndef CBORSerialization_H
#define CBORSerialization_H
#include <tinycbor.h>
#include <Arduino.h>
class CBORSerialization {
public:
CBORSerialization();
size_t encodeCBOR(uint8_t* buffer, size_t bufferSize); // Ensure this matches
void decodeCBOR(const uint8_t* cborData, size_t length);
};
#endif
#include "CBORSerialization.h"
#include <tinycbor.h>
// Constructor for CBORSerialization
CBORSerialization::CBORSerialization() {}
// Function to encode CBOR data
size_t CBORSerialization::encodeCBOR(uint8_t* buffer, size_t bufferSize) {
CborEncoder encoder;
cbor_encoder_init(&encoder, buffer, bufferSize, 0);
// Start a new array of size 3
CborEncoder arrayEncoder;
cbor_encoder_create_array(&encoder, &arrayEncoder, 3);
// Example data to encode
int temperature = 25; // Example temperature
int humidity = 60; // Example humidity
const char* unit = "C"; // Example unit
// Add elements to the array
cbor_encode_int(&arrayEncoder, temperature);
cbor_encode_int(&arrayEncoder, humidity);
cbor_encode_text_string(&arrayEncoder, unit, strlen(unit));
// Close the array
cbor_encoder_close_container(&encoder, &arrayEncoder);
// Return the size of the encoded data
return cbor_encoder_get_buffer_size(&encoder, buffer);
}
// Function to decode CBOR data
void CBORSerialization::decodeCBOR(const uint8_t* cborData, size_t length) {
CborParser parser;
CborValue it;
// Initialize the CBOR parser
cbor_parser_init(cborData, length, 0, &parser, &it);
// Check if it's an array
if (cbor_value_is_array(&it)) {
CborValue arrayValue;
// Get the array iterator
cbor_value_enter_container(&it, &arrayValue);
// Read elements from the array
int temperature, humidity;
char unit[10]; // Buffer for the unit string
size_t unitLength = sizeof(unit);
// First element: temperature
cbor_value_get_int(&arrayValue, &temperature);
cbor_value_advance(&arrayValue);
// Second element: humidity
cbor_value_get_int(&arrayValue, &humidity);
cbor_value_advance(&arrayValue);
// Third element: unit of measurement
cbor_value_copy_text_string(&arrayValue, unit, &unitLength, NULL);
cbor_value_advance(&arrayValue);
// Print the values
Serial.println(temperature);
Serial.println(humidity);
Serial.println(unit);
}
}
#include <Arduino.h>
#include "CBORSerialization.h"
// Buffer size for CBOR encoding
#define BUFFER_SIZE 128
uint8_t buffer[BUFFER_SIZE];
CBORSerialization serializer; // Use the correct class name
void setup() {
Serial.begin(115200);
}
void loop() {
delay(500);
// Encode CBOR data
size_t cborLength = serializer.encodeCBOR(buffer, BUFFER_SIZE);
// Print the encoded CBOR data in HEX
Serial.print("Encoded CBOR data: ");
for (size_t i = 0; i < cborLength; i++) {
Serial.print(buffer[i], HEX);
}
Serial.println();
// Decode the CBOR data
serializer.decodeCBOR(buffer, cborLength); // Call decodeCBOR on the correct class instance
}
Create two files in your Arduino project: CBORSerialization.h and CBORSerialization.cpp. Paste the class definitions into these files. Include the Class:
In your main sketch (CBORSerialization.ino), include the class by adding #include "CBORSerialization.h".
Upload the sketch to your Arduino board, Initialize the encoder and decoder, printing the result in HEX format.
works as expected
Need to check CBOR support