So here is a little more complete code sample to also get the Bonjour part to work.
//DHCP/Bonjour-based OSC server test code
//for use with IDE 1.0.x
//for use with W5100 or W5200 based ethernet shields
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetBonjour.h>
#include <ArdOSC.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
//create instances of OSC server and client objects.
OSCServer server;
OSCClient client;
int serverPort = 8000; //TouchOSC (incoming port)
int destPort = 9000; //TouchOSC (outgoing port)
int ledPin = 6; //pin 13 on Arduino Uno. Pin 6 on a Teensy++2
int flag=0;
void setup(){
Serial.begin(115200);
Serial.println("DHCP-Bonjour-based OSC server test 12/28/12");
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
while(true);
}
// print your local IP address:
Serial.print("Arduino IP address: ");
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(".");
}
Serial.println();
Serial.println();
// Initialize the Bonjour/MDNS library. You can now reach or ping this
// Arduino via the host name "arduino.local", provided that your operating
// system is Bonjour-enabled (such as MacOS X).
// Always call this before any other method!
if (EthernetBonjour.begin("arduino")){
Serial.println("Setting Bonjour Name successful");
}
else{
Serial.println("Bonjour Name could not be set");
}
// Now let's register the service we're offering (a web service) via Bonjour!
// To do so, we call the addServiceRecord() method. The first argument is the
// name of our service instance and its type, separated by a dot. In this
// case, the service type is _osc. There are many other service types, use
// google to look up some common ones, but you can also invent your own
// service type, like _mycoolservice - As long as your clients know what to
// look for, you're good to go.
// The second argument is the port on which the service is running. This could be
// port 80 here, the standard HTTP port.
// The last argument is the protocol type of the service, either TCP or UDP.
// Of course, our OSC service is a UDPservice.
// With the service registered, it will show up in the Bonjour Browser on a Mac or
// in the Connections settings in TouchOSC
if(
EthernetBonjour.addServiceRecord("Arduino OSC Server._osc",
8000,
MDNSServiceUDP)){
Serial.println("Bonjour Service Record added successfully");
}
else{
Serial.println("Bonjour Service could not be added");
}
//Start the OSC server
server.begin(serverPort);
//Add callback functions for TouchOSC control element
//One callback function is needed for each independent TouchOSC control element.
server.addCallback("/OnOff/toggle1", &funcOnOff);
server.addCallback("/Fader/Value", &funcValue);
}
void loop(){
// This actually runs the Bonjour module. YOU HAVE TO CALL THIS PERIODICALLY,
// OR NOTHING WILL WORK! Preferably, call it once per loop().
EthernetBonjour.run();
if(server.aviableCheck()>0){
Serial.println("alive! ");
}
}
void funcOnOff(OSCMessage *_mes){
float value = _mes->getArgFloat(0);
//create new osc message
OSCMessage newMes;
//set destination ip address & port no
newMes.setAddress(_mes->getIpAddress(),destPort);
newMes.beginMessage("/OnOff/toggle1");
if(flag==1){
flag=0;
Serial.println(value);
digitalWrite(ledPin, LOW);
}
else{
flag=1;
Serial.println(value);
digitalWrite(ledPin, HIGH);
}
newMes.addArgFloat(flag);
//send osc message back to controll object in TouchOSC
//Local feedback is turned off in the TouchOSC interface.
//The button is turned on in TouchOSC interface whe the conrol receives this message.
client.send(&newMes);
}
void funcValue(OSCMessage *_mes){
long value = (int) _mes->getArgFloat(0);
Serial.print("Value = : ");
Serial.println(value);
//create new osc message
OSCMessage newMes;
//set destination ip address & port no
newMes.setAddress(_mes->getIpAddress(),destPort);
newMes.beginMessage("/Fader/Value");
newMes.addArgFloat(value);
//send osc message
client.send(&newMes);
}]//DHCP/Bonjour-based OSC server test code
//for use with IDE 1.0.x
//for use with W5100 or W5200 based ethernet shields
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetBonjour.h>
#include <ArdOSC.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
//create instances of OSC server and client objects.
OSCServer server;
OSCClient client;
int serverPort = 8000; //TouchOSC (incoming port)
int destPort = 9000; //TouchOSC (outgoing port)
int ledPin = 6; //pin 13 on Arduino Uno. Pin 6 on a Teensy++2
int flag=0;
void setup(){
Serial.begin(115200);
Serial.println("DHCP-Bonjour-based OSC server test 12/28/12");
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
while(true);
}
// print your local IP address:
Serial.print("Arduino IP address: ");
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(".");
}
Serial.println();
Serial.println();
// Initialize the Bonjour/MDNS library. You can now reach or ping this
// Arduino via the host name "arduino.local", provided that your operating
// system is Bonjour-enabled (such as MacOS X).
// Always call this before any other method!
if (EthernetBonjour.begin("arduino")){
Serial.println("Setting Bonjour Name successful");
}
else{
Serial.println("Bonjour Name could not be set");
}
// Now let's register the service we're offering (a web service) via Bonjour!
// To do so, we call the addServiceRecord() method. The first argument is the
// name of our service instance and its type, separated by a dot. In this
// case, the service type is _osc. There are many other service types, use
// google to look up some common ones, but you can also invent your own
// service type, like _mycoolservice - As long as your clients know what to
// look for, you're good to go.
// The second argument is the port on which the service is running. This could be
// port 80 here, the standard HTTP port.
// The last argument is the protocol type of the service, either TCP or UDP.
// Of course, our OSC service is a UDPservice.
// With the service registered, it will show up in the Bonjour Browser on a Mac or
// in the Connections settings in TouchOSC
if(
EthernetBonjour.addServiceRecord("Arduino OSC Server._osc",
8000,
MDNSServiceUDP)){
Serial.println("Bonjour Service Record added successfully");
}
else{
Serial.println("Bonjour Service could not be added");
}
//Start the OSC server
server.begin(serverPort);
//Add callback functions for TouchOSC control element
//One callback function is needed for each independent TouchOSC control element.
server.addCallback("/OnOff/toggle1", &funcOnOff);
server.addCallback("/Fader/Value", &funcValue);
}
void loop(){
// This actually runs the Bonjour module. YOU HAVE TO CALL THIS PERIODICALLY,
// OR NOTHING WILL WORK! Preferably, call it once per loop().
EthernetBonjour.run();
if(server.aviableCheck()>0){
Serial.println("alive! ");
}
}
void funcOnOff(OSCMessage *_mes){
float value = _mes->getArgFloat(0);
//create new osc message
OSCMessage newMes;
//set destination ip address & port no
newMes.setAddress(_mes->getIpAddress(),destPort);
newMes.beginMessage("/OnOff/toggle1");
if(flag==1){
flag=0;
Serial.println(value);
digitalWrite(ledPin, LOW);
}
else{
flag=1;
Serial.println(value);
digitalWrite(ledPin, HIGH);
}
newMes.addArgFloat(flag);
//send osc message back to controll object in TouchOSC
//Local feedback is turned off in the TouchOSC interface.
//The button is turned on in TouchOSC interface whe the conrol receives this message.
client.send(&newMes);
}
void funcValue(OSCMessage *_mes){
long value = (int) _mes->getArgFloat(0);
Serial.print("Value = : ");
Serial.println(value);
//create new osc message
OSCMessage newMes;
//set destination ip address & port no
newMes.setAddress(_mes->getIpAddress(),destPort);
newMes.beginMessage("/Fader/Value");
newMes.addArgFloat(value);
//send osc message
client.send(&newMes);
}
EthernetBonjour.zip (35.1 KB)
Arduino_TouchOSC.touchosc (530 Bytes)
W5200 Ethernet Library.zip (10.5 KB)