I use Protoson (search github for thinger-io/Protoson ) to encode and decode payload (bytes) of iot devices. It’s much faster and lighter than JSON.
Would it be feasible to use Protoson instead of strutc to perform the payload and send it through the E220?
Protoson seems to be a very interesting option to use with E32, E22 and E220.
Protoson is a library for encoding and decoding unstructured data in a binary format. Similar to JSON but fast and small. Protoson is based on some Protocol Buffers encoding techniques, but it is not compatible over the wire. However you can embed the protoson output in any Protocol Buffers bytes field. Actually you can transmit the raw bytes as you like, as it does not require Protocol Buffers itself.
Design goals
Small compiled code size. This library has been mainly designed for microcontrollers or devices with very limited resources. The whole library with encoding and decoding support takes less than 3.5KB on an Arduino. It does not require the Standard Template Library (STL).
Custom memory allocators. Protoson C++ can use different memory allocations approaches. Currently there are implemented a circular, and a dynamic memory allocator. Choose your allocation scheme according to your needs.
Small output. The output size is comparable to the well-known MessagePack. Depending on the encoded data it can be even smaller.
More than JSON. Protoson can encode binary data and other data types in the root, without explicitly requiring a parent object or array.
Integration
The single required source, pson.h file is in the src directory. All you need to do is add this file and define the memory allocator. The memory allocator must be defined only once in a single .cpp file.
Serialization/Deserialization
Serialization and deserialization is done over some virtual classes that provide raw methods for writing and reading bytes. In this way you can create your own wrappers to serialize/deserialize to/from multiple data sources, like memory, socket, file, etc. In the following there are two classes that allows serialization and deserialization to a memory buffer.
// Helper class for encoding to memory
// You can create your own encoders writing to file, socket, etc)
class memory_writer : public pson_encoder {
private:
char* buffer_;
public:
memory_writer(char *buffer) : buffer_(buffer) {
}
protected:
virtual void write(const void *buffer, size_t size) {
memcpy(&buffer_[written_], buffer, size);
pson_encoder::write(buffer, size);
}
};
// Helper class for decoding from memory
class memory_reader : public pson_decoder {
private:
char* buffer_;
public:
memory_reader(char *buffer) : buffer_(buffer) {
}
protected:
virtual bool read(void *buffer, size_t size) {
memcpy(buffer, &buffer_[read_], size);
return pson_decoder::read(buffer, size);
}
};
// Now you can use these custom classes in the following way:
// Reserve memory buffer
char memory_buffer[512];
// Create a sample object
pson object;
object["hello"] = "world";
object["value"] = 336;
// Encode
memory_writer writer(memory_buffer);
writer.encode(object);
// Decode
memory_reader reader(memory_buffer);
pson decoded_object;
reader.decode(decoded_object);
// Use the values
const char* str = decoded_object["hello"];
int value = decoded_object["value"];
Memory Allocators
In some environments with limited memory or without dynamic memory allocation can be useful to define custom memory allocators. Protoson requires memory for storing the data structure in memory, i.e., when your are building a object, or decoding it from some source. Encoding and Decoding part does not require memory itself.
Currently you can switch between two different memory allocators: circular_memory_allocator and dynamic_memory_allocator:
Use a dynamic_memory_allocator if you want to/can use dynamic memory. Internally, the dynamic memory allocator uses malloc and free.
Use a circular_memory_allocator if you want to define a static memory buffer. In this case you need to specify a buffer size that will depend on your maximum message length. Notice that the required buffer size can vary between platforms for storing the same message, depending on the pointer size (16, 32, or 64 bits), memory alignment, padding, etc.
Maintaining a repository (or site or forum) is a lot like tending to a garden - it requires constant care and attention to keep it thriving. If you're a skilled gardener (or coder!) and want to help keep our repository blooming, we'd love to have you on board! We're also looking for talented writers and forum moderators to help us grow our community. Interested in joining our team? Don't hesitate to reach out and let us know how you can contribute!
Are you a fan of electronics or programming? Share your knowledge with others, write a simple tutorial or how to make a great project Contact me: share_your_ideas@mischianti.org
The content displayed on this website is protected under a CC BY-NC-ND license. Visitors are prohibited from using, redistributing, or altering any content from this website for commercial purposes, including generating revenue through advertising. Any unauthorized use is a violation of the license terms and legal action may be taken against individuals or entities found to be in violation.
You must also provide the link to the source.
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional
Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes.The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional
Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes.The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.