I am trying to build a system that will turn indicator lights on/off based on incoming OSC messages. I am using an Arduino Uno R4 Minima and an Ethernet Shield 2 (W5500).
Using the CNMAT OSC library, I've successfully tested some simple code (based on the UDPSendMessage example) to send OSC messages from the Arduino to a computer on my network. But I haven't been able to get the Arduino to receive messages. Or if they are being received, I can't get the code to do anything with the incoming messages.
First, this is the code from my successful test to send messages from the Arduino. It sends the integer argument 1 to the address "/switch/2" when a switch on pin 2 is pressed. When the switch is open, it sends the integer argument 0:
/*
Make an OSC message and send it over UDP
Adrian Freed
*/
#include <OSCMessage.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <SPI.h>
#include <OSCMessage.h>
EthernetUDP Udp;
IPAddress ip(192, 168, 1, 171); // the Arduino's IP
IPAddress outIp(192, 168, 1, 110); //destination IP
const unsigned int outPort = 9999;
const int switchPin = 2;
const int ledPin = 8;
int switchState = 0;
byte mac[] = {}; // Ethernet shield's MAC address (omitted here, but included in the code I ran)
void setup() {
Ethernet.begin(mac,ip);
Udp.begin(8888);
pinMode(switchPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
}
void loop(){
//an LED to let me know the switch is working
switchState = digitalRead(switchPin);
if (switchState == LOW) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
OSCMessage msg("/switch/2"); //the message wants an OSC address as first argument
msg.add((int32_t)digitalRead(switchPin));
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
delay(20);
}
Next I tried using the UDPEcho example to verify messages received by the Arduino. The only things I changed from the example were my Arduino's IP and MAC addresses:
/*
Leverage the UDP source IP and port calls to
return OSC information back
This example can be extended to build routers and forwarders of OSC packets
*/
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <SPI.h>
#include <OSCBundle.h>
EthernetUDP Udp;
IPAddress ip(192.168.1.171); // the Arduino's IP
//port numbers
const unsigned int inPort = 8888;
const unsigned int outPort = 9999;
void read_mac() {}
byte mac[] = {}; // Ethernet shield's MAC address (omitted here, but included in the code I ran)
void setup() {
Ethernet.begin(mac,ip);
Udp.begin(inPort);
}
void loop(){
OSCBundle bndl;
int size;
//receive a bundle
if( (size = Udp.parsePacket())>0)
{
// unsigned int outPort = Udp.remotePort();
while(size--)
bndl.fill(Udp.read());
if(!bndl.hasError())
{
//and echo it back
if(bndl.size() > 0)
{
static int32_t sequencenumber=0;
// we can sneak an addition onto the end of the bundle
bndl.add("/micros").add((int32_t)micros()); // (int32_t) is the type of OSC Integers
bndl.add("/sequencenumber").add(sequencenumber++);
Udp.beginPacket(Udp.remoteIP(), outPort);
bndl.send(Udp);
Udp.endPacket();
}
}
}
}
When I sent OSC messages to the Arduino's IP and port, I received no messages back (tried monitoring using Protokol and OSC DataMonitor). The only vague indication I have that the message is being received is that the yellow LED on the Ethernet connector seems to blink once every time I send an OSC message.
I hope this info is all detailed enough to see what I'm missing. OSC and UDP are both fairly new to me, and my programming skills are definitely beginner, so I've got a lot of learning still to do, but I've been poring over examples and forums for the past couple of days with no luck. Appreciate any help!