Go Down

Topic: Sending commads and parameters via UDP (Read 963 times) previous topic - next topic


    I'm trying to figure out how to implement a pretty simple protocol for sending commands + parameters to an arduino board via UDP datagrams.

The part where I send UDP packets is working fine ... My arduino receive the data... But where I need help is to interpret these commands+params.

At the moment my protocol is simple and may become more complex in the future as I wand to be able to add checksum and stuff like that.

For the moment it's like that:

first 3 character is the function number
All the other character excluding the 3 first ones is the parameter

simple example:

001test    will print test to the LCD ...

This 'sort of protocol' works well when I have  a command and 1 parameter.. but if I want to send more parameter for the same command or if I would like to add checksumming it start to be more difficult..

here is my actual code:

Code: [Select]
// Check if new UDP packet arrived

  int packetSize = Udp.parsePacket();
    Serial.print("[DEBUG] Received packet: [");
    Serial.print("] From: ");
    IPAddress remote = Udp.remoteIP();
    for (int i =0; i < 4; i++)
      Serial.print(remote[i], DEC);
      if (i < 3)
    Serial.print("  port:");


    Serial.print(" Content:");

  // read packet
    Serial.println("[DEBUG] Looping packet start");
    char command[4];
    char param[packetSize -2];
    int index=0;
    for (int i = 0; i < packetSize; i++)
      if (i < 3)
        command[i] = packetBuffer[i];
        command[i+1] = NULL;
        param[i-3] = packetBuffer[i];
        param[i-2] = NULL;
   Serial.println("[DEBUG] Looping packet end");

I would like to have more comments before I continue just to see if there is better way to do it...

I thought about using the cmdMessenger library but I never been able to make it work with UDP messages.

All idea are welcome !

Thanks all


My first thought is that when I type a response, it does not look like this:

So, then I wonder why you jam stuff together. Much simpler to parse things when there are delimiters.

If you were sending stuff like:
001 test string for the LCD
you would easily parse the arguments. That's what the strtok() function is made for.
The art of getting good answers lies in asking good questions.


Thanks PaulS,

I didn't know about the strtok() function, I'm going to read about it right now and see how it could help me.

I'll post my findings when I've mastered strtok function :-)

Thanks for your quick answer

Go Up