Sending OSCMessage over UDP with long variable

Hello All.

I need some help please. I'm working on a project with an Arduino Uno and an encoder plus an Ethernet Shield. The idea is to send encoder position information via OSC to a lighting console in a format that it can read and then take triggers from. I've got most of it working all the way to delivering the message to the console. The problem is that it's arriving in an incorrect format.

The console needs to see:
/eos/sc/encoder/pos/(long posSend)
the "/eos/sc/" part essentially just tells it that it's OSC.
What it's seeing is:
/eos/sc/encoder/pos/ without the posSend.

I've tried various different iterations to try and get it to work but I'm not winning. The piece of code is below. Any help would be hugely appreciated.

void loop() {
  long newPosition = myEnc.read() / 5.771;
  if (newPosition != oldPosition) {
    oldPosition = newPosition;
    Serial.println(newPosition);
    lcd.setCursor(0, 1);
    lcd.print(newPosition);
  }

  posSend = newPosition;
  //the message wants an OSC address as first argument
  OSCMessage msg("/eos/sc/encoder/pos/");
  msg.add(posSend);
  //msg.add("#");

  Udp.beginPacket(outIp, outPort);
  msg.send(Udp);    // send the bytes to the SLIP stream
  Udp.endPacket();  // mark the end of the OSC Packet
  msg.empty();      // free space occupied by message

It isn't the problem but I would write the sending packet inside the if condition. Now you are sending the packet continuosly even if newPosition doesn't change.
I think your problem is that OSC message needs a space character before the parameter. I added it in OSCMessage msg("/eos/sc/encoder/pos ") and deleted last "/";. It could be so, try it:

void loop() {
  long newPosition = myEnc.read() / 5.771;
  if (newPosition != oldPosition) {
    oldPosition = newPosition;
    Serial.println(newPosition);
    lcd.setCursor(0, 1);
    lcd.print(newPosition);
  
   posSend = newPosition;
   //the message wants an OSC address as first argument
   OSCMessage msg("/eos/sc/encoder/pos ");
   msg.add(posSend);
   //msg.add("#");

   Udp.beginPacket(outIp, outPort);
   msg.send(Udp);    // send the bytes to the SLIP stream
   Udp.endPacket();  // mark the end of the OSC Packet
   msg.empty();      // free space occupied by message
 }

msg.add () adds a parameter, it does not simply add to the path.

If you need the value of posSend to be part of the path, you need to do something like:

  // the message wants an OSC address as first argument
  String path = "/eos/sc/encoder/pos/" + String(posSend);
    
  OSCMessage msg(path.c_str());

  Udp.beginPacket(outIp, outPort);
  msg.send(Udp);    // send the bytes to the SLIP stream
  Udp.endPacket();  // mark the end of the OSC Packet
  msg.empty();      // free space occupied by message

I think that is what the console is looking for.

This was literally going to be the next thing I was going to try and wrap my head around. Thank You!!!

Perfect thank you. It's doing exactly what I need now.

i send ASCII strings as broadcast UDP mesages on a model RR with several nodes. the received string can be as easily decoded as a string received from the serial monitor

Perhaps the solution isn't very clear. I tried it and didn't run ok.
I did tests and investigated about OSC messages.
First I was wrong with a space character between address and parameter. This is only how softwares show the path. A OSC path has a OSC address and any parameters. Between them there is additional characters (OSC regulations). So that it's necessary to join address with parameters through msg.add().
Now the problem is msg.add() doesn't support any data type as long or int, etc. I saw msg.add() need a char. So the solution is to convert long to char before include it in msg.add(). I did it with sprintf.

void loop() {
  long newPosition = myEnc.read() / 5.771;
  if (newPosition != oldPosition) {
    oldPosition = newPosition;
    Serial.println(newPosition);
    lcd.setCursor(0, 1);
    lcd.print(newPosition);
  
   posSend = newPosition;
   //the message wants an OSC address as first argument
   OSCMessage msg("/eos/sc/encoder/pos");
   char myPosition[20];
   sprintf(myPosition, "%ld", posSend);
   msg.add(myPosition);
   //msg.add("#");

   Udp.beginPacket(outIp, outPort);
   msg.send(Udp);    // send the bytes to the SLIP stream
   Udp.endPacket();  // mark the end of the OSC Packet
   msg.empty();      // free space occupied by message
 }

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