OSC in Processing: Unterschied zwischen den Versionen
Zur Navigation springen
Zur Suche springen
Keine Bearbeitungszusammenfassung |
|||
Zeile 3: | Zeile 3: | ||
Sketch --> Import Library --> Add Library --> oscp5 | Sketch --> Import Library --> Add Library --> oscp5 | ||
== | ==Code Example== | ||
Here are two code sketches for Arduino and Processing that send OSC messages to eachother. | |||
===Processing Code=== | |||
<source lang="js" line start="2" highlight="4-6"> | <source lang="js" line start="2" highlight="4-6"> | ||
/////////////////////////////////////////////////////////////////////////////////// | |||
// Code for sending and receiving OSC messages to/from an ESP32 | |||
/////////////////////////////////////////////////////////////////////////////////// | |||
// Example for sending & receiving open sound control (OSC) messages on the ESP32. | |||
// Every time you click the mouse in the window you send a '0' or '1' to the ESP. | |||
// Every time an analog sensor goes bellow threshold, you receive it's value. | |||
/////////////////////////////////////////////////////////////////////////////////// | |||
// based on this example: oscP5sendreceive by andreas schlegel | |||
// oscP5 website at http://www.sojamo.de/oscP5 | |||
/////////////////////////////////////////////////////////////////////////////////// | |||
import oscP5.*; | import oscP5.*; | ||
import netP5.*; | import netP5.*; | ||
OscP5 oscP5; | OscP5 oscP5; | ||
NetAddress myRemoteLocation; | NetAddress myRemoteLocation; | ||
void setup() { | void setup() { | ||
size(400,400); | size(400, 400); | ||
frameRate(25); | frameRate(25); | ||
/* start oscP5, listening for incoming messages at port 12000 */ | /* start oscP5, listening for incoming messages at port 12000 */ | ||
oscP5 = new OscP5(this, | oscP5 = new OscP5(this, 9999); | ||
/* myRemoteLocation is a NetAddress. a NetAddress takes 2 parameters, | /* myRemoteLocation is a NetAddress. a NetAddress takes 2 parameters, | ||
* an ip address and a port number. myRemoteLocation is used as parameter in | * an ip address and a port number. myRemoteLocation is used as parameter in | ||
Zeile 33: | Zeile 39: | ||
* send messages back to this sketch. | * send messages back to this sketch. | ||
*/ | */ | ||
myRemoteLocation = new NetAddress(" | myRemoteLocation = new NetAddress("192.168.0.11", 8888); | ||
} | } | ||
float backgroundShade = 0; | |||
void draw() { | void draw() { | ||
background( | background(backgroundShade); | ||
} | } | ||
int onOff = 0; | |||
void mousePressed() { | void mousePressed() { | ||
OscMessage myMessage = new OscMessage("/led"); // create osc message | |||
OscMessage myMessage = new OscMessage("/ | |||
if (onOff == 0) { | |||
onOff = 1; | |||
} else onOff = 0; | |||
/* | myMessage.add(onOff); /* add an int to the osc message */ | ||
oscP5.send(myMessage, myRemoteLocation); | oscP5.send(myMessage, myRemoteLocation); /* send the message */ | ||
} | } | ||
Zeile 58: | Zeile 68: | ||
print(" addrpattern: "+theOscMessage.addrPattern()); | print(" addrpattern: "+theOscMessage.addrPattern()); | ||
print(" typetag: "+theOscMessage.typetag()); | print(" typetag: "+theOscMessage.typetag()); | ||
/* read the contents of the OSCmessage as float and print it */ | /* read the contents of the OSCmessage as float and print it */ | ||
float messageValue = theOscMessage.get(0).floatValue(); | float messageValue = theOscMessage.get(0).floatValue(); | ||
println(" message: "+messageValue); | println(" message: "+messageValue); | ||
backgroundShade = map(messageValue, 0, 4000, 0, 255); | |||
} | } | ||
</source> | |||
===Arduino Code=== | |||
<source lang="js" line start="2" highlight="4-6"> | |||
/////////////////////////////////////////////////////////////////////////////////// | |||
// Code for ESP32 DEV MODULE 38pin version | |||
/////////////////////////////////////////////////////////////////////////////////// | |||
// Example for sending & receiving open sound control (OSC) messages on the ESP32 | |||
// Send integers '0' or '1' to the address "/led" to turn on/off an LED on pin14. | |||
// Every time the analog sensor value on pin36 goes bellow the "threshold" value | |||
// a message is sent to the address "/sensor" with the current sensor value. | |||
/////////////////////////////////////////////////////////////////////////////////// | |||
// based on this example: ESP8266ReceiveMessage from the OSCuino library | |||
/////////////////////////////////////////////////////////////////////////////////// | |||
// when uploading code: sometimes necessairy | |||
// to presss&hold the "BOOT" button | |||
// when you see the following lines: | |||
// "Connecting........_____....._____....." | |||
///////////////////////////////////////////////// | |||
#include <WiFi.h> | |||
#include <WiFiUdp.h> | |||
#include <OSCMessage.h> | |||
#include <OSCBundle.h> | |||
#include <OSCData.h> | |||
char ssid[] = "xxx"; // your network SSID (name) | |||
char pass[] = "xxx"; // your network password | |||
// A UDP instance to let us send and receive packets over UDP | |||
WiFiUDP Udp; | |||
const IPAddress outIp(192, 168, 0, 5); // remote IP (not needed for receive) | |||
const unsigned int outPort = 9999; // remote port (not needed for receive) | |||
const unsigned int localPort = 8888; // local port to listen for UDP packets (here's where we send the packets) | |||
OSCErrorCode error; | |||
#define LED_PIN 27 | |||
#define SENSOR_PIN 36 | |||
unsigned int ledState = LOW; | |||
int sensorValue = 0; | |||
int thresholdValue = 4000; | |||
void setup() { | |||
pinMode(LED_PIN, OUTPUT); | |||
pinMode(SENSOR_PIN, INPUT); | |||
digitalWrite(LED_PIN, ledState); // turn *on* led | |||
Serial.begin(115200); | |||
// Connect to WiFi network | |||
Serial.println(); | |||
Serial.println(); | |||
Serial.print("Connecting to "); | |||
Serial.println(ssid); | |||
WiFi.begin(ssid, pass); | |||
while (WiFi.status() != WL_CONNECTED) { | |||
delay(500); | |||
Serial.print("."); | |||
} | |||
Serial.println(""); | |||
Serial.println("WiFi connected"); | |||
Serial.println("IP address: "); | |||
Serial.println(WiFi.localIP()); | |||
Serial.println("Starting UDP"); | |||
Udp.begin(localPort); | |||
Serial.print("Local port: "); | |||
#ifdef ESP32 | |||
Serial.println(localPort); | |||
#else | |||
Serial.println(Udp.localPort()); | |||
#endif | |||
} | |||
// function for turning on/off the LED | |||
// depending on if one recives 1/0: | |||
void led(OSCMessage &msg) { | |||
ledState = msg.getInt(0); | |||
digitalWrite(LED_PIN, ledState); | |||
Serial.print("/led: "); | |||
Serial.println(ledState); | |||
} | |||
void loop() { | |||
/////////////////////////////////////////////////////////// | |||
// Receiving OSC message and turning LED on/off: | |||
/////////////////////////////////////////////////////////// | |||
OSCMessage msg; // create an OSC message object called msg | |||
int size = Udp.parsePacket(); // parse incoming OSC packet | |||
// if message received, unpack and store in msg | |||
if (size > 0) { | |||
while (size--) { | |||
msg.fill(Udp.read()); | |||
} | |||
if (!msg.hasError()) { | |||
msg.dispatch("/led", led); | |||
} else { | |||
error = msg.getError(); | |||
Serial.print("error: "); | |||
Serial.println(error); | |||
} | |||
} | |||
/////////////////////////////////////////////////////////// | |||
// Reading sensor value and sending OSC message: | |||
/////////////////////////////////////////////////////////// | |||
sensorValue = analogRead(SENSOR_PIN); | |||
if (sensorValue < thresholdValue) { | |||
Serial.println(sensorValue); | |||
// publish sensor value over OSC: | |||
if (WiFi.status() == WL_CONNECTED) | |||
{ | |||
OSCMessage msg("/sensor"); | |||
float toSend = sensorValue; | |||
msg.add(toSend); | |||
Udp.beginPacket(outIp, outPort); | |||
msg.send(Udp); | |||
Udp.endPacket(); | |||
msg.empty(); | |||
} | |||
} | |||
} | |||
</source> | </source> |
Version vom 5. Mai 2022, 12:52 Uhr
Install oscp5 Library
Sketch --> Import Library --> Add Library --> oscp5
Code Example
Here are two code sketches for Arduino and Processing that send OSC messages to eachother.
Processing Code
///////////////////////////////////////////////////////////////////////////////////
// Code for sending and receiving OSC messages to/from an ESP32
///////////////////////////////////////////////////////////////////////////////////
// Example for sending & receiving open sound control (OSC) messages on the ESP32.
// Every time you click the mouse in the window you send a '0' or '1' to the ESP.
// Every time an analog sensor goes bellow threshold, you receive it's value.
///////////////////////////////////////////////////////////////////////////////////
// based on this example: oscP5sendreceive by andreas schlegel
// oscP5 website at http://www.sojamo.de/oscP5
///////////////////////////////////////////////////////////////////////////////////
import oscP5.*;
import netP5.*;
OscP5 oscP5;
NetAddress myRemoteLocation;
void setup() {
size(400, 400);
frameRate(25);
/* start oscP5, listening for incoming messages at port 12000 */
oscP5 = new OscP5(this, 9999);
/* myRemoteLocation is a NetAddress. a NetAddress takes 2 parameters,
* an ip address and a port number. myRemoteLocation is used as parameter in
* oscP5.send() when sending osc packets to another computer, device,
* application. usage see below. for testing purposes the listening port
* and the port of the remote location address are the same, hence you will
* send messages back to this sketch.
*/
myRemoteLocation = new NetAddress("192.168.0.11", 8888);
}
float backgroundShade = 0;
void draw() {
background(backgroundShade);
}
int onOff = 0;
void mousePressed() {
OscMessage myMessage = new OscMessage("/led"); // create osc message
if (onOff == 0) {
onOff = 1;
} else onOff = 0;
myMessage.add(onOff); /* add an int to the osc message */
oscP5.send(myMessage, myRemoteLocation); /* send the message */
}
/* incoming osc message are forwarded to the oscEvent method. */
void oscEvent(OscMessage theOscMessage) {
/* print the address pattern and the typetag of the received OscMessage */
print("### received an osc message.");
print(" addrpattern: "+theOscMessage.addrPattern());
print(" typetag: "+theOscMessage.typetag());
/* read the contents of the OSCmessage as float and print it */
float messageValue = theOscMessage.get(0).floatValue();
println(" message: "+messageValue);
backgroundShade = map(messageValue, 0, 4000, 0, 255);
}
Arduino Code
///////////////////////////////////////////////////////////////////////////////////
// Code for ESP32 DEV MODULE 38pin version
///////////////////////////////////////////////////////////////////////////////////
// Example for sending & receiving open sound control (OSC) messages on the ESP32
// Send integers '0' or '1' to the address "/led" to turn on/off an LED on pin14.
// Every time the analog sensor value on pin36 goes bellow the "threshold" value
// a message is sent to the address "/sensor" with the current sensor value.
///////////////////////////////////////////////////////////////////////////////////
// based on this example: ESP8266ReceiveMessage from the OSCuino library
///////////////////////////////////////////////////////////////////////////////////
// when uploading code: sometimes necessairy
// to presss&hold the "BOOT" button
// when you see the following lines:
// "Connecting........_____....._____....."
/////////////////////////////////////////////////
#include <WiFi.h>
#include <WiFiUdp.h>
#include <OSCMessage.h>
#include <OSCBundle.h>
#include <OSCData.h>
char ssid[] = "xxx"; // your network SSID (name)
char pass[] = "xxx"; // your network password
// A UDP instance to let us send and receive packets over UDP
WiFiUDP Udp;
const IPAddress outIp(192, 168, 0, 5); // remote IP (not needed for receive)
const unsigned int outPort = 9999; // remote port (not needed for receive)
const unsigned int localPort = 8888; // local port to listen for UDP packets (here's where we send the packets)
OSCErrorCode error;
#define LED_PIN 27
#define SENSOR_PIN 36
unsigned int ledState = LOW;
int sensorValue = 0;
int thresholdValue = 4000;
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(SENSOR_PIN, INPUT);
digitalWrite(LED_PIN, ledState); // turn *on* led
Serial.begin(115200);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.println("Starting UDP");
Udp.begin(localPort);
Serial.print("Local port: ");
#ifdef ESP32
Serial.println(localPort);
#else
Serial.println(Udp.localPort());
#endif
}
// function for turning on/off the LED
// depending on if one recives 1/0:
void led(OSCMessage &msg) {
ledState = msg.getInt(0);
digitalWrite(LED_PIN, ledState);
Serial.print("/led: ");
Serial.println(ledState);
}
void loop() {
///////////////////////////////////////////////////////////
// Receiving OSC message and turning LED on/off:
///////////////////////////////////////////////////////////
OSCMessage msg; // create an OSC message object called msg
int size = Udp.parsePacket(); // parse incoming OSC packet
// if message received, unpack and store in msg
if (size > 0) {
while (size--) {
msg.fill(Udp.read());
}
if (!msg.hasError()) {
msg.dispatch("/led", led);
} else {
error = msg.getError();
Serial.print("error: ");
Serial.println(error);
}
}
///////////////////////////////////////////////////////////
// Reading sensor value and sending OSC message:
///////////////////////////////////////////////////////////
sensorValue = analogRead(SENSOR_PIN);
if (sensorValue < thresholdValue) {
Serial.println(sensorValue);
// publish sensor value over OSC:
if (WiFi.status() == WL_CONNECTED)
{
OSCMessage msg("/sensor");
float toSend = sensorValue;
msg.add(toSend);
Udp.beginPacket(outIp, outPort);
msg.send(Udp);
Udp.endPacket();
msg.empty();
}
}
}