4

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:

#BOOL#INT#INT#FLOAT#BOOL#STRING#

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!

7

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? – Mikael Falkvidd Sep 4 '17 at 21:02
2

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 '16 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. – Ignacio Vazquez-Abrams Oct 30 '16 at 18:00
0

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, privacy policy and cookie policy

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