Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejs
const port = 8025;
const osc = new OSC();

setup() {
// setup OSC receiver
  osc.on('/text', msg => {
    handleMessage(msg);
  }
  );

  try {
    osc.open({
      port:
        port
    }
    );
  }
  catch (e) {
    console.log("Could not connect: " + e);
  }
}


function handleMessage(msg) {
 console.log((msg.args[0]); // print the 1st message item out to console
 console.log((msg.args[1]); // print the 2nd message item out to console
}


Arduino 

OSC can also be used on Arduino. Just make sure you're wifi (or ethernet) connected Arduino device is on the same network as your receiver/sender application. This example is complatable with the Arduino Uno Wifi, and the MKR1010 IOT. 

Code Block
languagec#
#include <SPI.h>
#include <WiFiNINA.h>
#include <WiFiUdp.h>
#include <OSCMessage.h>

// WiFi stuff
const char* ssid = "<Your network name>";
const char* pwd = "<Your networks secret password>";

WiFiUDP Udp;                 // instance of UDP library
const int remotePort = 8000; // port to which you want to send

//fill in the IP address you want to send to:
char remoteAddress[] = "10.0.1.3";  // you will need to enter the IP address of the router here. 


void setup() {
  Serial.begin(9600);
  //   while you're not connected to a WiFi AP,
  while ( WiFi.status() != WL_CONNECTED) {
    Serial.print("Attempting to connect to Network named: ");
    Serial.println (SECRET_SSID);           // print the network name (SSID)
    WiFi.begin(SECRET_SSID, SECRET_PASS);   // try to connect
    delay(2000);
  }

  // When you're connected, print out the device's network status:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
  Udp.begin(remotePort);
}

void loop() {
    int sensor = analogRead(A0);
    //the message wants an OSC address as first argument
    OSCMessage msg("/sensor/");
    msg.add(sensor);   
    Udp.beginPacket(remoteAddress, remotePort);
    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(100);
}