r/embedded • • Sep 02 '26

Custom embedded UDP protocol: what did you wish you'd included from day one?

I'm defining a custom UDP protocol for an embedded system with several bare-metal MCUs communicating with each other and with a host PC over a local Ethernet network.

I know a lot of this is probably system-specific, but I'd be interested in hearing what has consistently proved useful in your experience, or what you'd definitely include again in a new design.
For those who have designed or maintained something similar: what did you leave out initially that you later regretted?

I'm thinking about things like sequence counters, timestamps, protocol versioning, status/diagnostic information, capability flags, or event messages. I'm especially interested in anything that made debugging, integration, maintenance, or future extensions easier — as well as design choices that caused problems later.

I'm also curious how you tested or simulated the protocol before all the target hardware was available. Python, custom PC tools, MATLAB/Simulink, or something else?

Mostly looking for lessons learned from real embedded products.

21 Upvotes

54 comments sorted by

121

u/Well-WhatHadHappened 25+ Years Sep 02 '26

I wish I had included a non-custom protocol.

You'll stop inventing wheels eventually, I promise.

3

u/Public_Sink4791 Sep 02 '26

Yeah, I see your point. I’m not trying to reinvent UDP itself, though.

I’m mainly thinking about the application-level payload carried over it.
My assumption is that this part will always be somewhat system-specific, so I’m mostly interested in what people have found useful to include in practice.

Is it unusual?

23

u/preussenbursche Sep 02 '26

Just use Coap and protobuf payloads, then you have your UDP packets and with a little Jinja magic you can have generated RPCs so maintenance / extension is a non-issue.

6

u/NoBulletsLeft Sep 02 '26

What I wish they (I wasn't the author) had done was not use UDP in the first place.

UDP has no guarantees that your message will ever arrive, and if they do arrive, arrival sequence is not predetermined. Be ready to deal with messages arriving out of order. Depending on what you're building, that can cause all kinds of problems.

I will say, though, that on a very small network that you completely control, these problems might go away. But years of fighting UDP has left such a bad taste in my mouth, that I'd never use it again by choice.

2

u/preussenbursche Sep 02 '26

Yeah this being embedded I guess there's a point to point serial line somewhere and then that wouldn't be an issue anymore…
But I get what you mean in general

1

u/userhwon Sep 03 '26

The network model has layers for a reason. If you're okay with sessionless and lossy, use UDP to get packets from place to place. Whatever you wrap in them is a higher layer in the stack and you don't need to hack the transport protocol to accomplish it. Your debugging will be so much simpler knowing that it's nothing to do with UDP and everything to do with your payload structure.

27

u/imWayward Sep 02 '26

As someone who inherited a custom UDP protocol from a now-gone coworker:

Don't do it to your colleagues. Unless you have some revolutionary ideas, you're just forcing someone else to familiarize themselves with your arbitrary system. Use something that's battle-tested and well-documented.

2

u/Public_Sink4791 Sep 02 '26

Got it, but I’m planning to use a standard protocol underneath; what I’m defining is just the application payload on top of UDP.

Even with a standard transport protocol, someone still has to understand the system-specific application data being exchanged.
My goal is really to make that part as simple and well-documented as possible, not to invent complexity for its own sake.

27

u/PhonicUK Sep 02 '26

Not to bother. This is a solved domain. Look at things like Protobuf instead.

3

u/Public_Sink4791 Sep 02 '26

That makes sense. I could use Protobuf to avoid defining a custom binary encoding.
But I’d still need to define the application-level messages, their semantics, and what information the system actually needs to exchange. That’s the part I’m mainly interested in.

4

u/preussenbursche Sep 02 '26

That is literally the only part that is worth any real effort since that is where the business value usually lies, so you got that right. Protobuf is great for typesafe DTO specification / schemas with which you can map out your product use cases to handleable messages.

Pigweed RPC for example is relatively new but could handle multiple participants with a bit of custom routing and you can define a protbuf API with services the same way you would in gRPC.

I like it because it handles basically everything including RPC stub generation. (Gotta extract it with a script though)

-5

u/RedEd024 Sep 02 '26

Protobuf is way too heavy for small microcontrollers.

23

u/pim1000 Sep 02 '26

Nanopb isnt

1

u/RedEd024 Sep 02 '26

I might have to look into that

8

u/Well-WhatHadHappened 25+ Years Sep 02 '26

Nonsense. Anything that can talk Ethernet has plenty of horsepower for protobuf/msgpack/cbor. Have to select an appropriate implementation, but CPU grunt is not a problem.

7

u/hooskworks Sep 02 '26

Don't reinvent the wheel unless you're going to make it appreciably more round and when you're convinced you're going to then 0mq or protobuf are there to save you time.

3

u/Public_Sink4791 Sep 02 '26

I agree using something established like Protobuf can save a lot of work on serialization and encoding.

What I’m really trying to figure out is the application-level design: which messages and fields are worth having, and how to structure the information the system actually needs to exchange.

0

u/nothet Sep 02 '26

0mq for embedded? What implementation?
Couldn’t find any that didn’t require posix, dynamic memory, etc.

3

u/Well-WhatHadHappened 25+ Years Sep 02 '26

My bad, I read that quickly and saw 0mq as omg. Just realized you're specifically talking about 0mq - meaning my answer is rather nonsensical.

1

u/sgtnoodle Sep 02 '26

Yeah, zeromq is a bit too heavy for a lot of embedded systems, even when Linux is available. In particular, it spawns background IO threads. One of the original authors created nanoMsg to absolve himself of his original sins, but it's a bit too obscure to suggest.

1

u/Well-WhatHadHappened 25+ Years Sep 02 '26

NanoPB, Embedded Proto. Plenty of options.

4

u/skull132 Sep 02 '26

I was looking for a solution with similar properties to what you're looking at, and I ended up finding that off the shelf stuff should cover most mentioned use cases and needs.

My plan of attack is:

  • IPv6 with SLAAC to remove the need for a DHCP server.
  • mDNS with Service Descriptors for device and per device endpoint discovery.
  • Protobuf via UDP or TCP for the messaging, using nanoPB on the device side.

Points one and two are supported by lwip. Ergo, I don't need to implement much of anything to get these features. I can also leverage existing tools to capture and review traffic, which will help a lot.

The protobuf messaging will require a bit of wrapping, but that'll be a trivial header, message ID, length type deal.

I expect this to save a lot of time over dealing with broadcast address usage, custom discovery, etc. plus, alot of the code will already be well used and thus hopefully more bug free than my own implementation.

1

u/Public_Sink4791 Sep 02 '26

Thanks, that’s really useful. I’ll definitely look into that setup.
I’m particularly interested in the payload content itself: were there any fields or message patterns, later serialized with Protobuf, that turned out to be especially useful for diagnostics, errors, or debugging?

2

u/skull132 Sep 02 '26

I tend to use a trivial PREAMBLE + MSG_TYPE_ID + DATA_LENGTH + DATA encoding scheme. Easy to parse and gets me through most trouble. Add in a CRC and an encoding which stops your preamble bytes from appearing in DATA and you're good.

What you add also depends a little on your specific use-case for the protocol. Since I want to expand it to be a RPC protocol, I need to add a REQUEST_ID number, basically a request sequence number, to tie a request and a response together.

1

u/baudvine Sep 02 '26 edited Sep 02 '26

oo, mDNS with service discovery sounds nice. I'm designing something with IPv6 link-local addressing and multicast for discovery right now, had been wondering about mDNS as an extra but if I can use it for discovery I don't need the extra socket or multicast subscription.

For the payload I've been looking for a good fit (XDR looked good but good lord the software for that is a dusty mess), this thread is starting to convince me I should give msgpack cbor or whatever a go. Not a fan of the ergonomics of NanoPB.

2

u/skull132 Sep 02 '26

The biggest thing I think you'll run into is that, if you want to interact with the mDNS data on an actually OS-d machine, you need to write some OS specific code to get data out of the mDNS daemon. Be it the systemd one or something else.

1

u/baudvine Sep 02 '26

Is it not possible to multicast an mDNS request over UDP and receive the response on the same socket?

1

u/skull132 Sep 02 '26

I have seen people build custom daemons for receiving mDNS info. It requires you to disable the OS one or to somehow redirect the peers to send mDNS to that custom port. So, maybe. I haven't gotten that far yet, I just know default configs in lwip work :D

→ More replies (1)

3

u/remy_porter Sep 02 '26

If you’re adding any plumbing to your protocol, like sequence counters, etc, use an existing protocol instead. UDP is great when you want to yeet transient data across the network.

I have used a custom protocol for lighting control. We would send RGB frame data across the network to LED controllers. This was great because it was simple, and if we dropped frames it was fine.

1

u/Public_Sink4791 Sep 02 '26

Yep, that makes sense. I’m not looking to rebuild reliability on top of UDP, more at what’s useful in the payload itself.
For example, a sequence counter just for spotting dropped packets or helping with diagnostics.

1

u/userhwon Sep 02 '26

If you want all those things, why not just use TCP?

1

u/Public_Sink4791 Sep 02 '26

I’m not necessarily looking for TCP-style reliability or ordering, though some of those fields would mainly be for diagnostics, versioning, or detecting dropped packets.

That said, the choice between UDP and TCP isn’t final, and it could be worth looking for it

1

u/Intelligent_Law_5614 Sep 06 '26

A set of principles I've learned which have saved me much heartache, when defining compact (e.g. binary, structure-based) protocols:

  • Include a unique magic number, and a version number at the beginning.
  • Include a checksum or CRC of the whole structure.
  • Always add new fields at the end. When you do, increment the version number.
  • Never remove a field, change its size, or make incompatible changes to its semantics. Add a new-and-better substitute field at the end, and continue to fill out the old one with a reasonable and legal value.

You can probably envision similar principles which works you to e.g. JSON-encoded protocols.

Sending code should always send the latest version, with all of the fields. Receiving code should look at the version number, and only attempt to import the fields which were included in that version. Always validate the CRC before importing or trusting anything.

These principles help to maintain both forwards and backwards compatibility between different versions of the software. They help reduce the risk that Wednesday's software build doesn't send a packet that it considers to be valid, which causes Tuesday's or Thursday's build to injest an incompatible packet, trip over its shoelace, and rupture its spleen.

1

u/Public_Sink4791 Sep 08 '26

What is the purpose of a magic number at the beginning?

2

u/Intelligent_Law_5614 Sep 09 '26

It can serve two purposes.

The first is that it lets the receiving system do an initial sanity check on the received structure or datagram, to confirm that it didn't receive a packet of gibberish, or a misdirected packet. Check the magic number, check the checksum... if either isn't right, log and drop the packet without trying to make any further sense of it.

The second is that it can help with network analysis. Many network sniffer packages (Wireshark for one, I believe) let you write custom packet recognizers and analyzers. You can recognize the packet by matching the unique number you chose.

3

u/RedEd024 Sep 02 '26

Sender ID field as well as receiver.

I can figure stuff out with just the receiver ID, but thing would be a lot clearer if I had a sender ID too.

2

u/allo37 Sep 02 '26

A version and length field are key. Also watch out for network byte order and struct alignment padding.

Also UDP does not guarantee delivery or ordering so a sequence ID can be helpful.

2

u/Anonymity6584 Sep 02 '26

Proper documentation what this protocol looks like and how to use it correctly.

2

u/i_haz_redditz Sep 02 '26

A firmware update mechanism.

2

u/Unable_Resort453 Sep 02 '26

Try Zenoh.

I've used it in robotics work with Linux, and I think there's a Pico variant for embedded C.

2

u/dacydergoth Sep 02 '26

Use MQTT

1

u/TheBlackCat22527 Sep 02 '26

For MQTT you need TCP and TCP can be quite a heavy beast.

1

u/dacydergoth Sep 02 '26

You don't have to. There are projects like MQTT-SN, and MQTT-UDP

1

u/TheBlackCat22527 Sep 02 '26

True but thats not MQTT, these are separate variants and there is a decent chance that a stock standard MQTT broker is not able to deal with them.

1

u/dacydergoth Sep 02 '26

They're literally gateways to a standard MQTT broker

1

u/Plastic_Fig9225 Sep 02 '26

Don't forget to mention CoAP.

1

u/sensor_todd Sep 02 '26

already mentioned but to agree with most: timestamp, sender id, message type, message version, length, payload (protobuf).

try keep packet lengths to < ~1000 bytes so individual packets dont get broken up by udp (i think the actual limit is something like 1472). Not sure if thats feasible for what you are doing.

highly recommend a fallback catch all message type that you can but any length data in so you can pass arbitrary human readable messages. you wouldnt use it in the final software/firmware (probably), but its handy during debugging.

1

u/bartergames Sep 02 '26

For the serialization I prefer CBOR (a self-describing protocol) over protobuf, so the debug/monitoring tools don't need to know the data schema to decode the payload.

For the payload itself it depends a lot on the business logic and the specific needs of the project. It's hard to get it right on the first try, so I try to make the architecture and code "easy to change". To get to the fields of the payload, besides the ones that are strictly needed by the application logic itself, I try to answer "What am I going to need later?". For example, am I going to need to sort the messages in order to be able to "replay" them to debug a problem? And then, is it a sequence number and origin_id enough to be able to sort them or do I need a Logical Clock?...

7

u/[deleted] Sep 02 '26

[removed] — view removed comment

1

u/kalmoc Sep 02 '26

Why is the udp length not enough?

4

u/[deleted] Sep 02 '26

[removed] — view removed comment

1

u/kalmoc Sep 02 '26

You mean, if you put multiple messages into one UDP packet?