This page demonstrates how to use TinyProto on Arduino boards using the C++ API.
Light Protocol Examples
The Light protocol (Tiny light protocol API functions) provides simple SLIP-like framing with optional CRC and very low memory consumption (~800 bytes of flash). It does not provide acknowledgments or retransmission — use it when simplicity and low overhead are more important than guaranteed delivery.
Initialization
{
Serial.begin(115200);
proto.beginToSerial();
}
Sending and Receiving Data
Raw Buffers
uint8_t rx_buffer[64];
{
Serial.begin(115200);
proto.beginToSerial();
}
void loop()
{
uint8_t tx_data[] = {0x01, 0x02, 0x03};
proto.
write((
char *)tx_data,
sizeof(tx_data));
int len = proto.
read((
char *)rx_buffer,
sizeof(rx_buffer));
if (len > 0) {
}
}
Using Packets
tinyproto::Packet<64> rx_packet;
{
Serial.begin(115200);
proto.beginToSerial();
}
void loop()
{
tinyproto::Packet<64> tx_packet;
tx_packet.put("Hello");
int len = proto.
read(rx_packet);
if (len > 0) {
const char *msg = (const char *)rx_packet.data();
}
}
Stopping Communication
Full-Duplex Protocol Examples
The Full-Duplex protocol (Tiny Full Duplex API functions) provides reliable communication with automatic retransmission, sliding window, and CRC error detection. It supports both ABM (peer-to-peer) and NRM (primary/secondary) modes.
ABM Mode (Peer-to-Peer)
{
}
{
Serial.begin(115200);
}
void loop()
{
if (Serial.available()) {
uint8_t byte = Serial.read();
}
uint8_t tx_byte;
if (proto.
run_tx(&tx_byte, 1) == 1) {
Serial.write(tx_byte);
}
}
To send application data:
void sendMessage()
{
const char *msg = "Hello from Arduino!";
proto.
write(msg, strlen(msg) + 1);
}
Dynamic Buffer (Powerful MCUs)
On boards with more RAM (Due, Zero, ESP32, Teensy), you can use dynamic allocation:
Connection Events
You can monitor connection and disconnection events:
void onConnect(void *udata, uint8_t addr, bool connected)
{
if (connected)
Serial.println("Connected!");
else
Serial.println("Disconnected!");
}
{
}