UDP messages - best practice

Hello everyone,

I'm using Arduino Nano 33 Iot to send data through WiFi with UDP protocol. I'm using the OSC library by Adrian Freed.
It works, but I have to send multiple messages and I would to ask what is the best way to do this, in particular if I have to use one big UDP packet for all messages or use multiple packets for every message.

Here an example of multiple packets for msg1 and 2

Udp.beginPacket(outIp, outPort);
msg1.send(Udp);                     
Udp.endPacket();                    // mark the end of the OSC Packet

Udp.beginPacket(outIp, outPort);
msg2.send(Udp);                    
Udp.endPacket();                    // mark the end of the OSC Packet

Thanks in advance!
R.

I would tend to use a seperate packet for each message
howver, remember that UDP is an unreliable protocol so the Arduino will not know if messages arrive sucessfully
you can add a sequence number to messages so at least the receiver is aware if messages are lost
if you require a reliable communications protocol use TCP - see TCP-vs-UDP

Ok! I will take a look.

Thanks so much!

It rather depends how much data you're sending, how often and how important it is that it be received.

I see thank you!
I have to use this communication to send data that controls audio stuff in real-time.

I'd guess that you don't want to drop any data then, so as suggested, use TCP instead.

Perfect thank you! Can I ask you if you have any library to suggest?

I think you will find this useful:

Thanks so much!

what is 'real time'? every millisecond, every 100milliseconds, every second???
do you read some control parameters and transmit them?
do you poll controls, e.g. read a potentiometer using an ADC, or use events, e.g. push buttons?
think we need more information regarding the application

I read IMU sensor value over WiFi to control some parameter of audio synthesis. Of course I need this thing as fast as possible.

sounds like you are polling the sensor(s) in a loop transmitting values to the audio system
how large is the sensor packet in bytes?
you could have a simple loop reading sensor and transmitting the packet
can the receiver keep up or would have to add a deley to loop?
are you implementing the receiver code or using an existing system?

I suggest you don't need it as fast as possible, you need it as fast as necessary. You need to find out how fast is fast enough.

1 Like

@PerryBebbington Yes of course! You're right

@horace I will use an existing system for the receiver. Anyway I tried something this afternoon and it seems to work well!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.