Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nopnop2002/esp-idf-protocol-buffer
Example of Google Protocol Buffers Serialize and Deserialize with ESP-IDF
https://github.com/nopnop2002/esp-idf-protocol-buffer
esp-idf esp32 protobuf
Last synced: 3 months ago
JSON representation
Example of Google Protocol Buffers Serialize and Deserialize with ESP-IDF
- Host: GitHub
- URL: https://github.com/nopnop2002/esp-idf-protocol-buffer
- Owner: nopnop2002
- License: mit
- Created: 2022-06-11T01:44:05.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-08-26T06:28:38.000Z (over 1 year ago)
- Last Synced: 2024-10-11T13:45:03.440Z (4 months ago)
- Topics: esp-idf, esp32, protobuf
- Language: C
- Homepage:
- Size: 8.79 KB
- Stars: 30
- Watchers: 2
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# esp-idf-protocol-buffer
Example of Google Protocol Buffers Serialize and Deserialize with ESP-IDF.In MQTT for IoT devices, JSON is used as the serialization format of the message.
However, for low-performance hardware, JSON parsing in C is a heavy load.
So when I was looking for a good serialized format that could be implemented in C, I found protobuf.ESP-IDF includes [this](https://developers.google.com/protocol-buffers/) Google Protocol Buffers library.
You can use Protocol Buffers components as standard.
But there is no example code in ESP-IDF.I ported from [this](https://github.com/protobuf-c/protobuf-c/wiki/Examples).
# Software requirements
esp-idf v4.4/v5.x.# Installation
```
git clone https://github.com/nopnop2002/esp-idf-protocol-buffer
cd esp-idf-protocol-buffer
idf.py flash monitor
```- Serialize
```
I (328) MAIN: Writing 4 serialized bytes
I (338) MAIN: 0x3ffaff78 08 0a 10 02 |....|
```
- DeSerialize
```
I (338) MAIN: deserialize: a=10
I (348) MAIN: deserialize: b=2
```# How to use Protocol Buffers
### Install protobuf-c-compiler and c-library in ubuntu
```
$ sudo apt install protobuf-c-compiler libprotobuf-c-dev
$ protoc-c --version
protobuf-c 1.3.3
libprotoc 3.6.1
```### Make message define file
```
$ cat amessage.proto
syntax = "proto2";message AMessage {
required int32 a=1;
optional int32 b=2;
}
```### Compile message define file
protoc-c generates header files and source code.
```
$ protoc-c --c_out=. amessage.proto$ ls amessage.*
amessage.pb-c.c amessage.pb-c.h amessage.proto
```### Copy header files and source code to your project
```
$ cp amessage.pb-c.* /your_project/main/
```### Include header files to your source
```
#include "amessage.pb-c.h"
```### Add source file to your CMakeLists.txt
```
set(component_srcs main.c amessage.pb-c.c)
```# About proto3 description format
___proto3 is not supported.___
An error occurs while building the firmware.
```
../main/main.c:23:12: error: 'AMessage' {aka 'struct _AMessage'} has no member named 'has_b'
23 | msg.has_b = 1;
| ^
../main/main.c:41:17: error: 'AMessage' {aka 'struct _AMessage'} has no member named 'has_b'
41 | if (msg2->has_b) // handle optional field
```