I am currently developing an IoT device with arduino and i want to minimise the data payload. First i used json which is not very efficent in fixed messages (the format will always be the same). Then i found something like messagepack which also sounds nice but i think you could do even better with just binary. I hope to find something like that:

I have a message for example:


The position of this variables never change so i could represent the message in binary and i could save a lot of bytes. Is there any library which can de/encode such formats and generate messages from given values?

If not i think it would be very useful in further IoT devices!

4 Answers 4


Passing messages across computers and/or microcontrollers with different architectures can be a real pain (endianness...). So do not fall into NIH / reinventing the wheel traps but keep standing on the shoulders of giants! Several message serialization protocols exist.

Several implementations of such protocols exist (even for Arduino).

I suggest to have a look in a search engine for such protocols and add Arduino.

I've found:

PS: You might read https://scottlocklin.wordpress.com/2017/04/02/please-stop-writing-new-serialization-protocols/ and have a look at https://xkcd.com/927/ Just choose a giant compatible with your requirements ;-)

https://en.wikipedia.org/wiki/Comparison_of_data_serialization_formats may help

  • 1
    Brilliant. Maybe add something about how bad sending structs between avr, arm and x86 works? Sep 4, 2017 at 21:02

No such library is needed, since basic C++ can handle this on its own. Create a packed struct with the fields, fill the fields with assignment or memory copying, and cast to a char* in order to send. Reverse the order of operations in order to receive. Beware the string though, since they need to/should specify a length somewhere.

  • is it also possible to terminate the string instead of using a fixed length?
    – perotom
    Oct 30, 2016 at 17:57
  • There is no difference between terminating a string and specifying the length other than the fact that the terminator cannot be used in the string. Oct 30, 2016 at 18:00

A good choice to send sensor data is CayenneLPP. LPP stands for "Low Power Payload". It is a sensor type aware encoding that uses as few bytes as possible.

A key feature is that recipient must not be aware of content as it is encoded into message, so you can build a generic gateway using that.

You can read more about CayenneLPP here: https://developers.mydevices.com/cayenne/docs/lora/#lora-cayenne-low-power-payload

It is recommended for using it on TTN to minimize payload weigth.

There is an Arduino library for enconding and decoding. For decoding it outputs JSON object so it is ready to be used in platforms like NodeRed or any python or web based application.

Check it here: https://github.com/ElectronicCats/CayenneLPP


use a struct organize the data and when transmit the struct, use a char pointer pointing to it so everything is sent in binary format.

you will need to come up with a way to determine when the pointer ends.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.