OSC in Arduino

Aus hyperdramatik
Zur Navigation springen Zur Suche springen

Installing the OSCuino Library

Download Library

Download OSCuino library from github: https://github.com/CNMAT/OSC Code –> Download ZIP

Install Library

Arduino Menu: Sketch –> Include Library –> Add .ZIP Library select .ZIP file

Code Examples

send OSC message

Example for sending data from one device to another in the same network. Could be from an ESP to a computer or from and ESP to another ESP.

/////////////////////////////////////////////////
// Code for
// ESP32 DEVKIT V1 38pin version
/////////////////////////////////////////////////
// reads an analog value and sends it as OSC message
/////////////////////////////////////////////////
// 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 <OSCBundle.h>

#define SENSOR_PIN_1 36


// OSC-Stuff ----------------------------------------------------------

///////////////////// VARIABLES TO EDIT /////////////////////////////
IPAddress outIp = {192, 168, 236, 90};  // IP Adress of Receiver PC
const char *ssid = "Ladenlokal";  // wifi name
const char *password = "puppe2010";  // wifi password (min. 8 characters)
const char *hostName = "myName";  // choose a name for your device
const char *oscAddress = "/myAddress/mySubAddress";  // choose an OSC address for sending your messages to
int sendFrequencyMillis = 40; // send new OSC message every somany milliseconds
/////////////////////////////////////////////////////////////////////

WiFiUDP Udp;
const unsigned int localPort = 9000; // not needed
const unsigned int destPort = 6448;

float s1; // variablee for storing sensor value
unsigned long millisOld;  // variable for keeping track of time passed


void setup() {
  Serial.begin(115200); // begin serial connection

  WiFi.setHostname(hostName);
  WiFi.mode(WIFI_STA); // WiFi mode station (connect to wifi router only)
  WiFi.begin(ssid, password); // begin wifi connection

  // Wait for connection:
  uint16_t timeOut = 0;
  while ((WiFi.status() != WL_CONNECTED) && (timeOut < 10)) {
    delay(500);
    timeOut++;
    Serial.println(timeOut);
  }

  Serial.print("local IP address: ");
  Serial.println(WiFi.localIP());

  Udp.begin(localPort);
  
  Serial.print("Listening at port: ");
  Serial.println(localPort);
}


void loop() {
  s1 = analogRead(SENSOR_PIN_1);  // read sensor value

  if (millis() >= (millisOld + sendFrequencyMillis)) {
    millisOld = millis();  // update millisOld to be able to keep track of time
    
    Serial.println(s1);  // print sensor value

    // publish sensor value over OSC:
    if (WiFi.status() == WL_CONNECTED)
    {
      OSCMessage msg(oscAddress);
      msg.add(s1);

      Udp.beginPacket(outIp, destPort);
      msg.send(Udp);
      Udp.endPacket();
      msg.empty();
    }
  }
}

receive OSC message