Hi,
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:
// Check if new UDP packet arrived
int packetSize = Udp.parsePacket();
if(packetSize)
{
Serial.print("[DEBUG] Received packet: [");
Serial.print(packetSize);
Serial.print("] From: ");
IPAddress remote = Udp.remoteIP();
for (int i =0; i < 4; i++)
{
Serial.print(remote[i], DEC);
if (i < 3)
{
Serial.print(".");
}
}
Serial.print(" port:");
Serial.print(Udp.remotePort());
Udp.read(packetBuffer,packetSize);
Serial.print(" Content:");
Serial.println(packetBuffer);
// 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;
}
else
{
param[i-3] = packetBuffer[i];
param[i-2] = NULL;
}
}
Serial.print("Command:");
Serial.println(command);
Serial.print("Param:");
Serial.println(param);
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