ssilverman / libCBOR

Library for processing CBOR-encoded data (RFC 7049).
BSD 3-Clause "New" or "Revised" License
4 stars 3 forks source link

Logging to file #7

Closed theelims closed 11 months ago

theelims commented 11 months ago

Is it possible to use libCBOR to log sensor data onto the flash of an ESP32? I want to record sensor data in a file on my LitteFS partition. The file will grow to 1.5MB within one hour, so this is too much to hold the full CBOR binary in memory. File itself inherits from Stream. Is it possible to use the Bytestream or Byteprint classes somehow for this?

Kind regards

theelims commented 11 months ago

I found a solution to the problem. Here is an example that works:

constexpr size_t kBytesSize = 256;
uint8_t bytes[kBytesSize]{0};

cbor::BytesPrint bp{bytes, sizeof(bytes)};
bytes.reset();

// add data to bp as in the example

File logFile = FS.open("datalog.dat", "w", true);
size_t length = bp.getWriteSize();
logFile.write(reinterpret_cast<uint8_t *>(&bytes), length);
logFile.close();